android添加购物车动画实现

商品列表Adapter

 
 
import android.content.Context;
import android.graphics.drawable.Drawable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;
/**
 * 商品列表Adapter
 * @author antkingwei
 *
 */
public class GoodAdapter extends BaseAdapter{
    private Context mContext;
    private LayoutInflater layoutInflater;
    private HolderClickListener mHolderClickListener;
    final class ViewHolder {
        ImageView imgview;
        Button button;
    }
    public GoodAdapter(Context context){
        this.mContext = context;
        layoutInflater = LayoutInflater.from(mContext);
    }
 
     
    @Override
    public int getCount() {
        // TODO Auto-generated method stub
        return 16;
    }
 
    @Override
    public Object getItem(int position) {
        // TODO Auto-generated method stub
        return null;
    }
 
    @Override
    public long getItemId(int position) {
        // TODO Auto-generated method stub
        return position;
    }
 
    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        // TODO Auto-generated method stub
        final int selectedId = position;
        final ViewHolder viewHolder;
        if(convertView ==null){
            viewHolder = new ViewHolder();
            convertView = layoutInflater.inflate(R.layout.adapter_listview, null);
            viewHolder.imgview = (ImageView)convertView.findViewById(R.id.item_img);
            viewHolder.button = (Button)convertView.findViewById(R.id.item_button);
            convertView.setTag(viewHolder);
        }else{
            viewHolder =(ViewHolder)convertView.getTag();
        }
        viewHolder.button.setOnClickListener(new View.OnClickListener() {
             
            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub
                if(mHolderClickListener!=null){
                    int[] start_location = new int[2];
                    viewHolder.imgview.getLocationInWindow(start_location);//获取点击商品图片的位置
                    Drawable drawable = viewHolder.imgview.getDrawable();//复制一个新的商品图标
                    mHolderClickListener.onHolderClick(drawable,start_location);
                }
            }
        });
        return convertView;
    }
    public void SetOnSetHolderClickListener(HolderClickListener holderClickListener){
        this.mHolderClickListener = holderClickListener;
    }
    public interface HolderClickListener{
        public void onHolderClick(Drawable drawable,int[] start_location);
    }
 
}

活动类 

package com.example.addshopcart;
import com.example.addshopcart.GoodAdapter.HolderClickListener;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.app.Activity;
import android.content.Context;
import android.graphics.drawable.Drawable;
import android.view.Menu;
import android.view.View;
import android.view.ViewGroup;
import android.view.animation.Animation;
import android.view.animation.Animation.AnimationListener;
import android.view.animation.AnimationSet;
import android.view.animation.RotateAnimation;
import android.view.animation.ScaleAnimation;
import android.view.animation.TranslateAnimation;
import android.widget.Button;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.ListView;
/**
 * 
 * @author antkingwei
 *
 */
public class MainActivity extends Activity {
    private ListView listView;
    private Button cart_btn;
    private GoodAdapter goodAdapter;
    //动画时间
    private int AnimationDuration = 1000;
    //正在执行的动画数量
    private int number = 0;
    //是否完成清理
    private boolean isClean = false;
    private FrameLayout animation_viewGroup;
    private Handler myHandler = new Handler(){
      public void handleMessage(Message msg){
          switch(msg.what){
          case 0:
              //用来清除动画后留下的垃圾
              try{
                  animation_viewGroup.removeAllViews();
                  }catch(Exception e){
                       
                  }
                         
                  isClean = false;
                   
              break;
           default:
                  break;
          }
      }
    };
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        listView = (ListView)this.findViewById(R.id.listview);
        cart_btn = (Button)this.findViewById(R.id.button);
        animation_viewGroup = createAnimLayout();
        goodAdapter = new GoodAdapter(this);
        goodAdapter.SetOnSetHolderClickListener(new HolderClickListener(){
 
            @Override
            public void onHolderClick(Drawable drawable,int[] start_location) {
                // TODO Auto-generated method stub
                 doAnim(drawable,start_location);
                       
                 
            }
             
        });
        listView.setAdapter(goodAdapter);
    }
     
    private void doAnim(Drawable drawable,int[] start_location){
        if(!isClean){
            setAnim(drawable,start_location);
        }else{
            try{
              animation_viewGroup.removeAllViews();
              isClean = false;
              setAnim(drawable,start_location);
            }catch(Exception e){
                e.printStackTrace();
            }
            finally{
                isClean = true;
            }
        }
    }
    /**
     * @Description: 创建动画层
     * @param
     * @return void
     * @throws
     */
    private FrameLayout createAnimLayout(){
        ViewGroup rootView = (ViewGroup)this.getWindow().getDecorView();
        FrameLayout animLayout = new FrameLayout(this);
        FrameLayout.LayoutParams lp = new FrameLayout.LayoutParams(FrameLayout.LayoutParams.MATCH_PARENT,FrameLayout.LayoutParams.MATCH_PARENT);
        animLayout.setLayoutParams(lp);
        animLayout.setBackgroundResource(android.R.color.transparent);
        rootView.addView(animLayout);
        return animLayout;
         
    }
 
    /**
     * @deprecated 将要执行动画的view 添加到动画层
     * @param vg
     *        动画运行的层 这里是frameLayout
     * @param view
     *        要运行动画的View
     * @param location
     *        动画的起始位置
     * @return
     */
    private View addViewToAnimLayout(ViewGroup vg,View view,int[] location){
        int x = location[0];
        int y = location[1];
        vg.addView(view);
        FrameLayout.LayoutParams lp = new FrameLayout.LayoutParams(
                dip2px(this,90),dip2px(this,90));
        lp.leftMargin = x;
        lp.topMargin = y;
        view.setPadding(5, 5, 5, 5);
        view.setLayoutParams(lp);
         
        return view;
    }
    /**
     * dip,dp转化成px 用来处理不同分辨路的屏幕
     * @param context
     * @param dpValue
     * @return
     */
    private int dip2px(Context context,float dpValue){
        float scale = context.getResources().getDisplayMetrics().density;
        return (int)(dpValue*scale +0.5f);
    }
     
   /**
    * 动画效果设置
    * @param drawable
    *       将要加入购物车的商品
    * @param start_location
    *        起始位置
    */
   private void setAnim(Drawable drawable,int[] start_location){
       
       
       Animation mScaleAnimation = new ScaleAnimation(1.5f,0.0f,1.5f,0.0f,Animation.RELATIVE_TO_SELF,0.1f,Animation.RELATIVE_TO_SELF,0.1f);
       mScaleAnimation.setDuration(AnimationDuration);
       mScaleAnimation.setFillAfter(true);
        
 
       final ImageView iview = new ImageView(this);
       iview.setImageDrawable(drawable);
       final View view = addViewToAnimLayout(animation_viewGroup,iview,start_location);
       view.setAlpha(0.6f);
        
       int[] end_location = new int[2];
       cart_btn.getLocationInWindow(end_location);
       int endX = end_location[0];
       int endY = end_location[1]-start_location[1];
        
       Animation mTranslateAnimation = new TranslateAnimation(0,endX,0,endY);
       Animation mRotateAnimation = new RotateAnimation(0, 180, Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF, 0.5f);
       mRotateAnimation.setDuration(AnimationDuration);
       mTranslateAnimation.setDuration(AnimationDuration);
       AnimationSet mAnimationSet = new AnimationSet(true);
 
       mAnimationSet.setFillAfter(true);
       mAnimationSet.addAnimation(mRotateAnimation);
       mAnimationSet.addAnimation(mScaleAnimation);
       mAnimationSet.addAnimation(mTranslateAnimation);
        
       mAnimationSet.setAnimationListener(new AnimationListener(){
         
        @Override
        public void onAnimationStart(Animation animation) {
            // TODO Auto-generated method stub
            number++;
        }
 
        @Override
        public void onAnimationEnd(Animation animation) {
            // TODO Auto-generated method stub
           
            number--;
            if(number==0){
                isClean = true;
                myHandler.sendEmptyMessage(0);
            }
             
        }
 
        @Override
        public void onAnimationRepeat(Animation animation) {
            // TODO Auto-generated method stub
             
        }
            
       });
       view.startAnimation(mAnimationSet);
       
   }
   /**
    * 内存过低时及时处理动画产生的未处理冗余
    */
    @Override
   public void onLowMemory() {
    // TODO Auto-generated method stub
        isClean = true;
        try{
            animation_viewGroup.removeAllViews();
        }catch(Exception e){
            e.printStackTrace();
        }
        isClean = false;
     super.onLowMemory();
   }
     
    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.main, menu);
        return true;
    }
 
}

activity_main.xml

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context=".MainActivity" >
 
    <ListView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:id="@+id/listview"
        ></ListView>
    <Button
        android:layout_alignBottom="@+id/listview"
        android:layout_centerHorizontal="true"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/button"
        android:text="购物车"
        />
 
</RelativeLayout>

AdapterItem布局

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
     >
    <ImageView
        android:id="@+id/item_img"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:src="@drawable/ic_launcher"
        android:layout_alignParentLeft="true"
        />
    <Button
       android:id="@+id/item_button"
       android:layout_width="wrap_content"
       android:layout_height="wrap_content"
       android:layout_alignParentRight="true"
       android:text="添加"
       >
    </Button>
 
</RelativeLayout>

转自:http://www.oschina.net/code/snippet_203635_27426


  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值