android动画特效之解决解决移动后闪烁现象,解决输入法弹出后位置回复原状,解决两个动画叠加

来源:互联网 发布:杭州那个公司淘宝美工 编辑:程序博客网 时间:2024/06/05 19:00

以下代码实现的效果是:

BoundOpenView从居中移动到顶部,移动完后,BoundSendView从隐藏变为显示,并从顶部移动BoundOpenView下方20dp处,同时透明度慢慢增加。


private void BoundOpenViewAnimation() {Log.i(TAG, "BoundOpenViewAnimation");final int top = BoundOpenView.getTop()- DensityUtil.dip2px(mContext, 20);final int height = BoundOpenView.getHeight();TranslateAnimation animation = new TranslateAnimation(0, 0, 0, 0 - top);animation.setDuration(200);// 设置动画持续时间BoundOpenView.clearAnimation();BoundOpenView.setAnimation(animation);animation.setFillAfter(true);// ** 开始动画 *//*animation.setAnimationListener(new AnimationListener() {@Overridepublic void onAnimationEnd(Animation animation) {//BoundOpenView.clearAnimation();// 解决移动后闪烁现象TranslateAnimation anim = new TranslateAnimation(0, 0, 0,0);BoundOpenView.setAnimation(anim);//解决输入法弹出后位置回复原状RelativeLayout.LayoutParams lp = new RelativeLayout.LayoutParams(BoundOpenView.getWidth(),BoundOpenView.getHeight());//lp.topMargin=DensityUtil.dip2px(mContext, 20);lp.addRule(RelativeLayout.ALIGN_PARENT_TOP);BoundOpenView.setLayoutParams(lp);int move = height + DensityUtil.dip2px(mContext, 20);BoundSendViewAnimation(move);}@Overridepublic void onAnimationRepeat(Animation animation) {}@Overridepublic void onAnimationStart(Animation animation) {}});animation.startNow();}private void BoundSendViewAnimation(int move) {Log.i(TAG, "move:"+move);Animation translateanimation = new TranslateAnimation(0, 0, 0, move);translateanimation.setDuration(400);// 设置动画持续时间Animation alphaAnimation=new AlphaAnimation(0.01f,1.0f);alphaAnimation.setDuration(500);// 设置动画持续时间//动画叠加AnimationSet set=new AnimationSet(true);    //创建动画集对象          set.addAnimation(translateanimation);       //添加位置变化动画          set.addAnimation(alphaAnimation);           //添加尺寸变化动画          set.setFillAfter(true);                 //停留在最后的位置          set.setFillEnabled(true);          BoundSendView.clearAnimation();        BoundSendView.setAnimation(set);                    //设置动画BoundSendView.setVisibility(View.VISIBLE);// ** 开始动画 *//*set.setAnimationListener(new AnimationListener() {@Overridepublic void onAnimationEnd(Animation animation) {TranslateAnimation anim = new TranslateAnimation(0, 0, 0,0);BoundSendView.setAnimation(anim);RelativeLayout.LayoutParams lp = new RelativeLayout.LayoutParams(BoundSendView.getWidth(),BoundSendView.getHeight());lp.addRule(RelativeLayout.BELOW,R.id.bound_open_view);lp.topMargin=DensityUtil.dip2px(mContext, 20);BoundSendView.setLayoutParams(lp);}@Overridepublic void onAnimationRepeat(Animation animation) {// TODO Auto-generated method stub}@Overridepublic void onAnimationStart(Animation animation) {// TODO Auto-generated method stub}});set.startNow();                         //启动动画 }
DensityUtil类:
import android.content.Context;    public class DensityUtil {      /**      * 根据手机的分辨率从 dp 的单位 转成为 px(像素)      */      public static int dip2px(Context context, float dpValue) {          final float scale = context.getResources().getDisplayMetrics().density;          return (int) (dpValue * scale + 0.5f);      }        /**      * 根据手机的分辨率从 px(像素) 的单位 转成为 dp      */      public static int px2dip(Context context, float pxValue) {          final float scale = context.getResources().getDisplayMetrics().density;          return (int) (pxValue / scale + 0.5f);      }  }


原创粉丝点击