项目地址:github.com/razerdp/Fri… (能弱弱的求个star或者fork么QAQ)java
上篇连接:一块儿撸个朋友圈吧 (细节篇)- 图片浏览(preview)android
下篇连接:一块儿撸个朋友圈吧 - 图片浏览(中)【图片浏览器】git
终于,这个系列的文章进入了全新的篇章,我们的朋友圈系列进入研磨阶段,目前咱们能够成功展现数据,能够进行点赞或者取消点赞,能够进行评论。github
但完成这些基本功能是不够的。canvas
一款产品之因此深刻人心,是由于 “用着舒服”,说是“用着舒服”倒不如说是 “看着舒服”,而视觉交互,或者说交互动画,正是一款App打动人的最重要的地方。数组
在以前的文章里,我其实一直很注重这些小细节的,而这些小细节,也是微信如今所拥有的,或许你日常没怎么留意,但假若取消掉这些动画,相信你很快就会发觉 “这他喵的这么生硬”浏览器
在本系列以前的文章里,咱们有留意到并实现了如下的动画:缓存
下拉刷新时那个朋友圈的icon随着listview滑动而滑动并自身旋转微信
点赞时,popup的弹出动画app
点赞的时候,点赞的心心放大缩小动画
评论时,当输入法弹上来,输入框自动对齐评论或者动态的底部
点击评论或者点赞时,对应的名字背景色改变
或许您想不到,一个小小的朋友圈看似简单,却有着这么多小动画。
好的,说了这么多东西,除了总结外,实际上就是为了推销文章←_←,我不知道是否真的有人会看,也许会有人仅仅是为了拿源码伸伸手而已。
但,我认为,跟同一圈子的人交流本身的思想,分享本身的经验,不是一件很美妙的事情吗?因此,即便没什么人看,我也会坚持把这个开源项目完成的以及在简书坚持更新全部思路的-V-
正如前言那一堆废话所说,今天咱们要实现的是这么一个效果:
如您所见,当咱们点击图片的时候,你会发现,图片会有一个灰色的蒙层叠加在上面,看起来就像是咱们选中了图片同样。
若是要实现这个效果,按照咱们的平时习惯,确定是“selector走起”,奈何,当咱们真的去试了一下以后,发现,貌似无论用啊0.0
因而百度一番,或者谷歌一番,发现又是设置clickable啊,又是selector什么乱七八糟的顺序问题啊。。。。
与其执着于这些,倒不如我们自定义一个出来以应付一切的imageview。
若是硬要解释这个东东,我想,我应该从新写一篇文章来专门讲解一下这个东东(事实上我也打算这么作)
但在这里我只会简单的说说Selector究竟是如何实现view的视图变化的:
不管是什么Selector,实际上最终都是Drawable,而Drawable能够理解为图片,但若是须要更好的描述它,我以为将其理解为ps可能会更好,由于Drawable是一个抽象类,它提供了**“something that can be drawn”**的方法,其实弄来弄去,都是draw()方法
Selector在java中具体化的说,其实就是StateListDrawable。
Drawable有一个mState数组,它维护了不一样状态下的drawable,当view接收到touch事件,会调用refreshDrawableState来更新状态,通常来讲经过Drawable的isStateful()函数来得知是否与上次的状态不一样,若是是,则进行draw方法来改变view的视图,在咱们的眼中看起来就是颜色的改变。
上面很简单的讲述了selector的实现过程,从中咱们不可贵到如下信息:
梳理了一遍过程以后,咱们只须要对症下药就行了。
因而咱们正式开工:
首先仍是个人习惯,在自定义一个view以前,先配置attrs,在这里咱们就只配置一个属性用来改变前景色。
attrs:
<!--ForceClickImageView-->
<declare-styleable name="ForceClickImageView">
<attr name="foregroundColor" format="reference|color"/>
</declare-styleable>
复制代码
而后新建一个ForceClickImageView类,继承本项目的SuperImageView(实际上就是普通的imageview,封装了glide的加载方法)
/** * Created by 大灯泡 on 2016/4/11. * 朋友圈的imageview,包含点击动做 */
public class ForceClickImageView extends SuperImageView {
//前景层
private Drawable mForegroundDrawable;
private Rect mCachedBounds = new Rect();
public ForceClickImageView(Context context) {
this(context, null);
}
public ForceClickImageView(Context context, AttributeSet attrs) {
this(context, attrs, 0);
}
public ForceClickImageView(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
init(context, attrs);
}
}
复制代码
咱们能够看到,在这里我加了两个成员,一个天然是drawable,另外一个则是一个矩形,这个矩形主要是用来规定咱们的drawable绘制的范围,在这里咱们主要是用来缓存这个view的范围。
接下来在初始化方法里面咱们补充一下代码:
/** * 初始化 */
private void init(Context context, AttributeSet attrs) {
final TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.ForceClickImageView);
mForegroundDrawable = a.getDrawable(R.styleable.ForceClickImageView_foregroundColor);
if (mForegroundDrawable instanceof ColorDrawable) {
int foreGroundColor = a.getColor(R.styleable.ForceClickImageView_foregroundColor, 0x55c6c6c6);
mForegroundDrawable = new StateListDrawable();
ColorDrawable forceDrawable = new ColorDrawable(foreGroundColor);
ColorDrawable normalDrawable = new ColorDrawable(Color.TRANSPARENT);
((StateListDrawable) mForegroundDrawable).addState(new int[] { android.R.attr.state_focused },
forceDrawable);
((StateListDrawable) mForegroundDrawable).addState(new int[] { android.R.attr.state_pressed },
forceDrawable);
((StateListDrawable) mForegroundDrawable).addState(new int[] { android.R.attr.state_enabled },
normalDrawable);
((StateListDrawable) mForegroundDrawable).addState(new int[] {}, normalDrawable);
}
if (mForegroundDrawable != null) mForegroundDrawable.setCallback(this);
a.recycle();
}
复制代码
首先咱们获取到咱们定义的attrs属性集,而后获得drawable,在这里值得注意的是:
咱们的attr容许传入的参数除了reference外,还容许color
因此若是传入的是一个selector,那么getDrawable将会获得StateListDrawable
若是传入的是color值,那么getDrawable将会获得ColorDrawable,而ColorDrawable不管是什么状态,都只会有一个颜色。
因此若是传入的是ColorDrawable,咱们就须要手动new出一个StateListDrawable并设置咱们不一样状态下的drawable了。
在上述代码中,若是咱们获得的是ColorDrawable,咱们就new出StateListDrawable,而后分别对应添加下述状态和对应的drawable
由于咱们也不了解究竟是会触发哪一种状态,因此就直接扔了大概会触发的状态进去,由于在改变的时候,系统会从数组中遍历直到找出与状态符合的drawable为止。
哦,不要忘了setCallback(this),由于View已经实现了该接口,因此咱们把this传入就行了。
最重要的部分完成后后,接下来就是一些方法的覆写了。
@Override
protected void drawableStateChanged() {
super.drawableStateChanged();
if (mForegroundDrawable != null && mForegroundDrawable.isStateful()) {
mForegroundDrawable.setState(getDrawableState());
}
invalidate();
}
@Override
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);
if (mForegroundDrawable != null) {
mForegroundDrawable.setBounds(mCachedBounds);
mForegroundDrawable.draw(canvas);
}
}
@Override
protected void onSizeChanged(int w, int h, int oldw, int oldh) {
super.onSizeChanged(w, h, oldw, oldh);
if (mForegroundDrawable != null) mCachedBounds.set(0, 0, w, h);
}
}
复制代码
首先咱们覆写drawableStateChanged,这个方法在view的状态有发生改变的时候(好比从无焦点->有焦点),就会回调。
在这里,咱们直接给调用 mForegroundDrawable.setState(getDrawableState());
由于getDrawableState()方法在view里面已是封装好的了,它一共执行了两个动做:
在setState以后,咱们直接调用invalidate();要求这个view刷新。
在draw方法里面,父类的draw方法执行以后,也就是图片展现以后,咱们调用drawable的draw方法,此时就会将colorDrawable绘制到图片的上层,表现起来就是有selector的效果了。
最后在xml布局里面添加咱们的参数就能够了:
<razerdp.friendcircle.widget.imageview.ForceClickImageView android:id="@+id/img" android:scaleType="centerCrop" android:layout_width="match_parent" android:layout_height="match_parent" app:foregroundColor="@color/img_foregroundColor" />
复制代码
其中img_foregroundColor的色值为: "#85414141"