实现购物车点击添加的抛物线动画效果
2017-03-22 20:26
746 查看
public class MainActivity extends Activity { private ImageView top; private ImageView bottom; private ImageView animImageView; private ViewGroup anim_mask_layout;// 动画层 @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); top = (ImageView) findViewById(R.id.top); bottom = (ImageView) findViewById(R.id.bottom); } //点击添加按钮开始动画 public void startAnim(View view) { // 记录开始的位置 // 一个整型数组,用来存储按钮的在屏幕的X、Y坐标 int[] startLocation = new int[2]; // 这是获取购买按钮的在屏幕的X、Y坐标(这也是动画开始的坐标) top.getLocationInWindow(startLocation); // 创建要做动画的ImageView animImageView = new ImageView(this); // 设置animImageView的背景 Drawable background = top.getBackground(); //得到指定缩放大小的图片 Drawable zoomDrawable = zoomDrawable(background, dip2Px(this, 200), dip2Px(this, 200)); animImageView.setBackgroundDrawable(zoomDrawable); // 开始执行动画 setAnim(animImageView, startLocation, top); } /** * 设置动画 * * @param v 要做动画效果的图片 * @param startLocation 点击的X,Y轴位置 * @param view 被点击的图片 */ private void setAnim(final View v, int[] startLocation, final View view) { anim_mask_layout = null; anim_mask_layout = createAnimLayout(); anim_mask_layout.addView(v);// 把动画小球添加到动画层 final View viewa = addViewToAnimLayout(anim_mask_layout, v, startLocation); int[] endLocation = new int[2];// 存储动画结束位置的X、Y坐标 bottom.getLocationInWindow(endLocation);// shopCart是那个购物车 // 计算位移 int endX = endLocation[0] - startLocation[0];// 动画位移的X坐标 int endY = endLocation[1] - startLocation[1];// 动画位移的y坐标 //如何实现抛物线效果? //在X轴上保持匀速,在Y轴上加速就实现抛物线运动 /* * AccelerateDecelerateInterpolator 在动画开始与结束的地方速率改变比较慢,在中间的时候加速 AccelerateInterpolator 在动画开始的地方速率改变比较慢,然后开始加速 AnticipateInterpolator 开始的时候向后然后向前甩 AnticipateOvershootInterpolator 开始的时候向后然后向前甩一定值后返回最后的值 BounceInterpolator 动画结束的时候弹起 CycleInterpolator 动画循环播放特定的次数,速率改变沿着正弦曲线 DecelerateInterpolator 在动画开始的地方快然后慢 LinearInterpolator 以常量速率改变 OvershootInterpolator 向前甩一定值后再回到原来位置 * */ TranslateAnimation translateAnimationX = new TranslateAnimation(0, endX, 0, 0); translateAnimationX.setInterpolator(new LinearInterpolator()); translateAnimationX.setRepeatCount(0);// 动画重复执行的次数 translateAnimationX.setFillAfter(true); TranslateAnimation translateAnimationY = new TranslateAnimation(0, 0, 0, endY); translateAnimationY.setInterpolator(new AccelerateInterpolator()); translateAnimationY.setRepeatCount(0);// 动画重复执行的次数 translateAnimationX.setFillAfter(true); ScaleAnimation scaleAnimation = new ScaleAnimation(1, 0.2f, 1, 0.2f); scaleAnimation.setInterpolator(new AccelerateInterpolator()); scaleAnimation.setRepeatCount(0);// 动画重复执行的次数 scaleAnimation.setFillAfter(true); AnimationSet set = new AnimationSet(false); set.setFillAfter(false); set.addAnimation(scaleAnimation); set.addAnimation(translateAnimationY); set.addAnimation(translateAnimationX); set.setDuration(600);// 动画的执行时间 viewa.startAnimation(set); // 动画监听事件 set.setAnimationListener(new Animation.AnimationListener() { // 动画的开始 @Override public void onAnimationStart(Animation animation) { v.setVisibility(View.VISIBLE); } @Override public void onAnimationRepeat(Animation animation) { } // 动画的结束 @Override public void onAnimationEnd(Animation animation) { v.setVisibility(View.GONE); } }); } /** * @param * @return void * @throws * @Description: 创建动画层 */ private ViewGroup createAnimLayout() { ViewGroup rootView = (ViewGroup) ((Activity) this).getWindow().getDecorView(); LinearLayout animLayout = new LinearLayout(this); LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams( LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.MATCH_PARENT); animLayout.setLayoutParams(lp); animLayout.setBackgroundResource(android.R.color.transparent); rootView.addView(animLayout); return animLayout; } private View addViewToAnimLayout(final ViewGroup parent, final View view, int[] location) { int x = location[0]; int y = location[1]; LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams( LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT); lp.leftMargin = x; lp.topMargin = y; view.setLayoutParams(lp); return view; } /** * 将drawable对象进行指定大小的缩放 * * @param drawable 原图 * @param w 图片缩放后的宽 * @param h 图片缩放后的高 * @return */ public Drawable zoomDrawable(Drawable drawable, int w, int h) { int width = drawable.getIntrinsicWidth(); int height = drawable.getIntrinsicHeight(); Bitmap oldbmp = drawableToBitmap(drawable); // drawable 转换成 bitmap Matrix matrix = new Matrix(); // 创建操作图片用的 Matrix 对象 float scaleWidth = ((float) w / width); // 计算缩放比例 float scaleHeight = ((float) h / height); matrix.postScale(scaleWidth, scaleHeight); // 设置缩放比例 // 建立新的 bitmap ,其内容是对原 bitmap 的缩放后的图 Bitmap newbmp = Bitmap.createBitmap(oldbmp, 0, 0, width, height, matrix, true); return new BitmapDrawable(newbmp); // 把 bitmap 转换成 drawable 并返回 } /** * 将drawable 转换成 bitmap * * @param drawable * @return */ public Bitmap drawableToBitmap(Drawable drawable) { int width = drawable.getIntrinsicWidth(); // 取 drawable 的长宽 int height = drawable.getIntrinsicHeight(); Bitmap.Config config = drawable.getOpacity() != PixelFormat.OPAQUE ? Bitmap.Config.ARGB_8888 : Bitmap.Config.RGB_565; // 取 drawable 的颜色格式 Bitmap bitmap = Bitmap.createBitmap(width, height, config); // 建立对应 // bitmap Canvas canvas = new Canvas(bitmap); // 建立对应 bitmap 的画布 drawable.setBounds(0, 0, width, height); drawable.draw(canvas); // 把 drawable 内容画到画布中 return bitmap; } // dp转换为像素px public static int dip2Px(Context context, float dp) { final float scale = context.getResources().getDisplayMetrics().density; return (int) (dp * scale + 0.5f); } }
相关文章推荐
- 购物车特效-贝塞尔曲线动画(点击添加按钮,产生抛物线动画效果)
- 贝塞尔曲线实现的购物车添加商品动画效果
- jquery.fly.js实现添加购物车效果、实现抛物线运动
- 基于jquery fly插件实现加入购物车抛物线动画效果
- jQuery.fly插件实现添加购物车抛物线效果
- iOS开发——仿淘宝添加到购物车的动画效果实现
- 商品添加购物车实现动画效果
- 基于jquery fly插件实现加入购物车抛物线动画效果
- Android实现添加商品到购物车动画效果
- 基于jquery fly插件实现加入购物车抛物线动画效果,jquery.fly.js
- 详解Android实现购物车页面及购物车效果(点击动画)
- Fly to basket 点击添加到右侧购物车,动画效果
- iOS开发仿淘宝添加到购物车的动画效果实现
- Android实现购物车页面及购物车效果(点击动画)
- Android实现购物车添加物品的动画效果
- 基于jquery fly插件实现加入购物车抛物线动画效果
- 基于jquery fly插件实现加入购物车抛物线动画效果
- Android 实现蘑菇街购物车动画效果
- 【转】Android 实现蘑菇街购物车动画效果
- 仿天猫加入购物车抛物线动画效果