购物车加载动画

想大多app中有做把商品加入到购物车中的一个简单的抛物动画 接下来就给大家做个示范,

ps:动画是我从网上找到修改的 这里只不过给大家做了个整理,有两种效果一个是小球抛到购物车还有就是获取图片的btm然后做了个压缩 以动画的行事抛到购物车或者指定地点

Handler h = null;
private long lastonclickTime = 0;//全局变量
private URL bmp;
public ImageView img1;
private int[] startLocation;

   @Override
    public void onClick(View v) {
       //这里做的防止重复点击,因为有些图片内存比较大 如果图片在没压缩之前再次操作会堵塞线程 
        long time = SystemClock.uptimeMillis();//局部变量
        if (time - lastonclickTime <= 300) {
            KLog.e("TAG", "时间未到");
            return;
        }
        startLocation = new int[2];
        imgurl.getLocationInWindow(startLocation);
        lastonclickTime = time;

        new Thread(new Runnable() {
            @Override
            public void run() {
                img1 = new ImageView(mContext);
                try {
                    bmp = new URL(list.get(position).getSquareimgurl());
                    pngBM = BitmapFactory.decodeStream(bmp.openStream());//图片转成btm
                } catch (MalformedURLException e) {
                    e.printStackTrace();
                } catch (IOException e) {
                    e.printStackTrace();
                }
                Message msg = new Message();
                msg.what = 0;
                handle.sendMessage(msg);
            }
        }).start();
    }
});
 
 
private Handler handle = new Handler() {

    public void handleMessage(Message msg) {
        switch (msg.what) {
            case 0:
                    DisplayMetrics dm2 = mContext.getResources().getDisplayMetrics();
//这里我的图片是从列表中拿的 因为是个两列的列表 所以把图片的宽是屏幕的一半-10 第二个是高度
                    img1.setImageBitmap(zoomImg(pngBM, pm_width / 2 - 10, dm2.heightPixels / 3 - 90));
                    showAnimation(mContext, startLocation, "down", img1);
                long time = SystemClock.uptimeMillis();//局部变量
                    KLog.e("AAA","开始动画"+time);
                break;
        }
    };
};
//压缩图片
public Bitmap zoomImg(Bitmap bm, int newWidth ,int newHeight){
        // 获得图片的宽高
        int width = bm.getWidth();
        int height = bm.getHeight();
        // 计算缩放比例
        float scaleWidth = ((float) newWidth) / width;
        float scaleHeight = ((float) newHeight) / height;
        // 取得想要缩放的matrix参数
        Matrix matrix = new Matrix();
        matrix.postScale(scaleWidth, scaleHeight);
        // 得到新的图片
        Bitmap newbm = Bitmap.createBitmap(bm, 0, 0, width, height, matrix, true);
        return newbm;
    }
//下面是复制过来的 具体在哪我忘记了 这里就是经过我修改的图片到购物车的动画,有个旋转加缩小的效果 

import android.app.Activity;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Handler;
import android.os.Message;
import android.view.View;
import android.view.ViewGroup;
import android.view.animation.AccelerateInterpolator;
import android.view.animation.Animation;
import android.view.animation.Animation.AnimationListener;
import android.view.animation.AnimationSet;
import android.view.animation.LinearInterpolator;
import android.view.animation.RotateAnimation;
import android.view.animation.ScaleAnimation;
import android.view.animation.TranslateAnimation;
import android.widget.ImageView;
import android.widget.LinearLayout;

import com.mykar.framework.KLog.KLog;

import java.io.IOException;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.ArrayList;
import java.util.List;

import cc.zompen.yungou.R;


//添加到购物车动画
public class ShopCartAnimation {
    private static ShopCartAnimation shopCartAnimation = null;
    private static Context app;
    private static ImageView ball;
    private static int type = 0;
    ViewGroup anim_mask_layout;



    public ShopCartAnimation(Context context) {
        super();
        app = context;
    }


    public static void showAnimation(Context mContext, int[] ints, String down, ImageView imageView) {


//    int[] startLocation = new int[2];
//    v.getLocationInWindow(startLocation);
        int[] endLocation0 = new int[2];
        int[] endLocation00 = new int[2];
//
toolbar_tabfour_img 这个就是你想吧图片移动到那个位置的view地址 可以写项目任意布局的viewid
ImageView shopCart0 = (ImageView) ((Activity) mContext) .findViewById(R.id. toolbar_tabfour_img); // ImageView shopCart00 = (ImageView) ((Activity) mContext) // .findViewById(R.id.toolbar_tabthree_img); shopCart0.getLocationInWindow(endLocation0); //shopCart00.getLocationInWindow(endLocation00); endLocation0[ 0] = (endLocation0[ 0] + endLocation00[ 0]) / 2; endLocation00[ 1] = (endLocation0[ 1] + endLocation00[ 1]) / 2; ball = new ImageView(mContext); ball.setImageResource(R.mipmap. sign); imageView.setMaxWidth( 10); imageView.setMaxHeight( 10); shopCartAnimation = ShopCartAnimation . getInstance(mContext);
//向上的动画 
        if (down.equals("up")) {
            // shopCartAnimation.setAnim(ball, endLocation0,startLocation);
        } else if (down.equals("down")) {
            shopCartAnimation.setAnim(imageView, ints, endLocation0);
        }
    }

    public static ShopCartAnimation getInstance(Context context) {
        app = context;
        if (shopCartAnimation == null) {
            shopCartAnimation = new ShopCartAnimation(context);
        }
        return shopCartAnimation;
    }

    //动画层                 v动画的图片  anim_mask_layout获取窗体
    public void setAnim(final View v, int[] startLocation, int[] endLocation) {
        anim_mask_layout = null;
        anim_mask_layout = createAnimLayout();
//    v.setMinimumHeight(10);
//    v.width = 30;
        ViewGroup parent = (ViewGroup) v.getParent();

        if (parent != null) {

          //  parent.removeAllViews();
            return;

        }
        anim_mask_layout.addView(v);//把动画小球添加到动画层

        final View view = addViewToAnimLayout(anim_mask_layout, v,
                startLocation);
//    int[] endLocation = new int[2];// 存储动画结束位置的X、Y坐标
//    shopCart.getLocationInWindow(endLocation);// shopCart是那个购物车

        // 计算位移
//    int endX = 0 - startLocation[0] + 40;// 动画位移的X坐标
        int endX = endLocation[0] - startLocation[0];// 动画位移的X坐标
//    endX=Math.abs(endX);
        int endY = endLocation[1] - startLocation[1];// 动画位移的y坐标
//    endY=Math.abs(endY);
        TranslateAnimation translateAnimationX = new TranslateAnimation(0,
                endX, 0, 0);//endX前加-号
        translateAnimationX.setInterpolator(new LinearInterpolator());
        translateAnimationX.setRepeatCount(0);// 动画重复执行的次数
        translateAnimationX.setFillAfter(true);

        TranslateAnimation translateAnimationY = new TranslateAnimation(0, 0,
                0, endY);//endY前加-号
        translateAnimationY.setInterpolator(new AccelerateInterpolator());
        translateAnimationY.setRepeatCount(0);// 动画重复执行的次数
        translateAnimationX.setFillAfter(true);

        AnimationSet set = new AnimationSet(false);

        set.setFillAfter(false);
        ScaleAnimation scale = new ScaleAnimation(1f, 0.2f, 1f, 0.2f,
                Animation.RELATIVE_TO_SELF, 0.5f,
                Animation.RELATIVE_TO_SELF, 0.5f);

        RotateAnimation rotateAnim = new RotateAnimation(0, 360, Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF, 0.5f);
        rotateAnim.setDuration(2000);//动画持续时间
        rotateAnim.setFillAfter(true);//保持动画完成后的效果

        set.addAnimation(rotateAnim);
        set.addAnimation(scale);
        set.addAnimation(translateAnimationY);
        set.addAnimation(translateAnimationX);

        set.setDuration(800);// 动画的执行时间
        view.startAnimation(set);
        // 动画监听事件
        set.setAnimationListener(new AnimationListener() {
            // 动画的开始
            @Override
            public void onAnimationStart(Animation animation) {
                if(type==1){
                    return;
                }
                type = 1;
                if(v.getVisibility()==View.VISIBLE){
                    v.setVisibility(View.GONE);
                }
                v.setVisibility(View.VISIBLE);
            }

            @Override
            public void onAnimationRepeat(Animation animation) {
                // TODO Auto-generated method stub
            }

            // 动画的结束
            @Override
            public void onAnimationEnd(Animation animation) {
                v.setVisibility(View.GONE);
                type = 0;
//          buyNum++;//让购买数量加1
//          buyNumView.setText(buyNum + "");//
//          buyNumView.setBadgePosition(BadgeView.POSITION_TOP_RIGHT);
//          buyNumView.show();
            }
        });

    }

    //创建动画层
    private ViewGroup createAnimLayout() {
        ViewGroup rootView = (ViewGroup) ((Activity) app).getWindow().getDecorView();
        LinearLayout animLayout = new LinearLayout(app);
        LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams(
                LinearLayout.LayoutParams.MATCH_PARENT,
                LinearLayout.LayoutParams.MATCH_PARENT);
        animLayout.setLayoutParams(lp);
        animLayout.setId(Integer.MAX_VALUE);
        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;
    }

}

至于回调什么的啦 就自己写吧 代码比较简单 懒得做dome  如果你们需要 评论 我写个dome上去



猜你喜欢

转载自blog.csdn.net/qq_34468274/article/details/80828024