我在前面写过一篇自定义下载飞入动画文章,但是没有做过详细的介绍,最近由于工程需要,做了一个自定义点赞的动画效果。
对于一个自定义View来说,onMeasure只是用来计算View尺寸,onDraw()才是真正执行View的绘制,所以一般我们都需要重写onDraw()函数来绘制我们期望的UI界面。
本文的动画就是将要做动画的view利用android提供的Animation绘制到做动画的view层。
具体实现如下:
<span style="font-family:SimSun;font-size:14px;">public void praiseFlyOutAnimation() {
// 获取要做动画的view图标的位置,相对于window的位置
int[] locationSrc = new int[2];
mPraiseView.getLocationInWindow(locationSrc);
int width = mPraiseView.getWidth();
int height = mPraiseView.getHeight();
//定义要做动画的view的初始大小
FrameLayout.LayoutParams params = new FrameLayout.LayoutParams(width, height);
int[] locationDest = new int[2];
if (mContentContainer != null) {
mContentContainer.getLocationInWindow(locationDest);
//将要做动画的mAnimationImageView加入到动画层mContentContainer,mContentContainer是一个FramLayout
mContentContainer.addView(mAnimationImageView, params);
}
//初始化动画运动的参数
int startX = locationSrc[0];
int startY = locationSrc[1] - locationDest[1] - 48;
int endY = startY - 300;
//定义动画运动的起始和终止点
Animation translateAnimation = new TranslateAnimation(startX, startX, startY, endY);
translateAnimation.setDuration(3000);
// 对做动画的view缩放
Animation scaleAnimation = new ScaleAnimation(2.0f, 2.0f, 2.0f, 2.0f,
Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF, 0.5f);
scaleAnimation.setDuration(3000);
// // 自定义旋转动画效果,含透明效果
// PraiseRotateAnimation rotateAnimation = new PraiseRotateAnimation(-15, 20);
// rotateAnimation.setDuration(3000);
// 系统旋转动画效果
Animation rotateAnimation = new RotateAnimation(-20, 20, Animation.RELATIVE_TO_SELF, 0.5f,
Animation.RELATIVE_TO_SELF, 0.5f);
rotateAnimation.setDuration(3000);
// 动画透明效果
Animation alphaAnimation = new AlphaAnimation(1.0f, 0.0f);
alphaAnimation.setDuration(1000);
//设置动画
AnimationSet animationSet = new AnimationSet(true);
animationSet.setInterpolator(new DecelerateInterpolator(2.0f));
animationSet.addAnimation(alphaAnimation);
animationSet.addAnimation(rotateAnimation);
animationSet.addAnimation(scaleAnimation);
animationSet.addAnimation(translateAnimation);
//设置动画过程中不能再次点击开始动画
mLayoutPraise.setClickable(false);
animationSet.setFillAfter(true);
animationSet.setAnimationListener(new AnimationListener() {
@Override
public void onAnimationEnd(Animation animation) {
//动画完成后移除view
mContentContainer.removeView(mAnimationImageView);
mLayoutPraise.setClickable(true);
}
@Override
public void onAnimationRepeat(Animation animation) {
}
@Override
public void onAnimationStart(Animation animation) {
}
});
mAnimationImageView.startAnimation(animationSet);
mAnimationImageView.setTag(animationSet);
}</span>
Githut下载地址:https://github.com/ZBJDSBJ/MyPriaseAnimation