以下代码实现的效果是:
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() { @Override public 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); } @Override public void onAnimationRepeat(Animation animation) { } @Override public 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() { @Override public 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); } @Override public void onAnimationRepeat(Animation animation) { // TODO Auto-generated method stub } @Override public 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); } }