android 点赞动画 自定义UI实现
先看效果
FlowLikeView自定义UI
/**
* 点赞自定义控件
*/
public class FlowLikeView extends RelativeLayout {
private List<Drawable> mLikeDrawables; // 图片的集合
private LayoutParams mLayoutParams; // 用于设置动画对象的位置参数
private Random mRandom; // 用于产生随机数,如生成随机图片
private int mViewWidth; // 控件的宽度
private int mViewHeight; // 控件的高度
private int mPicWidth; // 图片的宽度
private int mPicHeight; // 图片的高度
private int mChildViewHeight; // 在 XML 布局文件中添加的子View的总高度
public FlowLikeView(Context context) {
this(context, null);
}
public FlowLikeView(Context context, AttributeSet attrs) {
this(context, attrs, 0);
}
public FlowLikeView(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
initParams();
}
private void initParams() {
mLikeDrawables = new ArrayList<>();
mLikeDrawables.add(generateDrawable(R.drawable.icon_video_play_zaned));
// 获取图片的宽高, 由于图片大小一致,故直接获取第一张图片的宽高
mPicWidth = mLikeDrawables.get(0).getIntrinsicWidth();
mPicHeight = mLikeDrawables.get(0).getIntrinsicHeight();
// 初始化布局参数
mLayoutParams = new RelativeLayout.LayoutParams(mPicWidth, mPicHeight);
mLayoutParams.addRule(CENTER_HORIZONTAL);
mLayoutParams.addRule(ALIGN_PARENT_BOTTOM);
mRandom = new Random();
}
private Drawable generateDrawable(int resID) {
return ContextCompat.getDrawable(getContext(), resID);
}
@Override