继续浏览精彩内容
慕课网APP
程序员的梦工厂
打开
继续
感谢您的支持,我会继续努力的
赞赏金额会直接到老师账户
将二维码发送给自己后长按识别
微信支付
支付宝支付

Android实现炫酷的购物车动画

弑天下
关注TA
已关注
手记 141
粉丝 12
获赞 51

5bb6e9d000014af502800466.jpg效果图


废话不多说,直接上代码:

[代码]java代码:

?

01

02

03

04

05

06

07

08

09

10

11

12

13

14

15

16

17

tv_shop_add.setOnClickListener(new OnClickListener() {

 

                @Override

                public void onClick(View v) {

                    //   一个整型数组,用来存储按钮的在屏幕的X、Y坐标

                    int[]   start_location = new int[2];

                    //   这是获取购买按钮的在屏幕的X、Y坐标(这也是动画开始的坐标)

                    v.getLocationInWindow(start_location);

                    buyImg   = new ImageView(context);

                    //   设置buyImg的图片

                    buyImg.setImageBitmap(getAddDrawBitMap());

                    //   开始执行动画

                    setAnim(buyImg,   start_location);

 

                    //   iscontans(arrayList.get(position));

                }

            });

[代码]java代码:

?

001

002

003

004

005

006

007

008

009

010

011

012

013

014

015

016

017

018

019

020

021

022

023

024

025

026

027

028

029

030

031

032

033

034

035

036

037

038

039

040

041

042

043

044

045

046

047

048

049

050

051

052

053

054

055

056

057

058

059

060

061

062

063

064

065

066

067

068

069

070

071

072

073

074

075

076

077

078

079

080

081

082

083

084

085

086

087

088

089

090

091

092

093

094

095

096

097

098

099

100

101

102

103

104

105

106

107

108

109

/**

 * 运动的控件

 *

 * @return

 */

public Bitmap getAddDrawBitMap() {

    ImageView text = new ImageView(context);

    // 运动的控件,样式可以自定义

    text.setBackgroundResource(R.drawable.xiaohongdian_shangping);

    return convertViewToBitmap(text);

}

 

/**

 * 创建动画层

 */

private ViewGroup createAnimLayout() {

    ViewGroup   rootView = (ViewGroup) ((Activity) context).getWindow().getDecorView();

    LinearLayout   animLayout = new LinearLayout(context);

    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 vg, 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;

}

 

/**

 * // 开始执行动画

 * @param v

 * @param start_location

 */

private void setAnim(final View v, int[] start_location) {

    anim_mask_layout   = null;

    anim_mask_layout   = createAnimLayout();

    // 把动画小球添加到动画层

    anim_mask_layout.addView(v);

    final View view =   addViewToAnimLayout(anim_mask_layout, v, start_location);

    // 这是用来存储动画结束位置的X、Y坐标

    int[]   end_location = new int[2];

    // rl_gouwuche是小球运动的终点 一般是购物车图标

    rl_gouwuche.getLocationInWindow(end_location);

 

    // 计算位移

    int endX = 0 - start_location[0] + 40;// 动画位移的X坐标

    int endY = end_location[1] - start_location[1];//   动画位移的y坐标

    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);

 

    AnimationSet set   = new AnimationSet(false);

    set.setFillAfter(false);

    set.addAnimation(translateAnimationY);

    set.addAnimation(translateAnimationX);

    set.setDuration(800);//   动画的执行时间

    view.startAnimation(set);

    // 动画监听事件

    set.setAnimationListener(new AnimationListener() {

        //   动画的开始

        @Override

        public void onAnimationStart(Animation animation) {

            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);

        }

    });

 

}

 

/**

 * 将定义的view装换成   bitmap格式

 *

 * @param view

 * @return

 */

public Bitmap convertViewToBitmap(View view) {

    view.measure(MeasureSpec.makeMeasureSpec(0,   MeasureSpec.UNSPECIFIED),

            MeasureSpec.makeMeasureSpec(0,   MeasureSpec.UNSPECIFIED));

    view.layout(0, 0,   view.getMeasuredWidth(), view.getMeasuredHeight());

    view.buildDrawingCache();

    Bitmap bitmap =   view.getDrawingCache();

    return bitmap;

}

原文链接:http://www.apkbus.com/blog-625356-61674.html

打开App,阅读手记
0人推荐
发表评论
随时随地看视频慕课网APP