【Android应用场景】学习Android动画效果

看了别人写的效果(购物车)后,感觉挺高大上的,自己也就仿照写了个效果,效果图如下:

【Android应用场景】学习Android动画效果_第1张图片

仔细想想其实不难,基本思路是在界面上生成一个图层,并在其上面实现动画,主要运用了缩放、旋转、位移三种效果

相应动画

<span style="font-size:18px;"> 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;
            }
        }
    }</span>

创建显示层

 <strong><span style="font-size:18px;">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;
    }
</span></strong>

紧接着是设置动画,监听动画的启动和结束

 private void setAnim(Drawable drawable,int[] 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 img=new ImageView(this);
        img.setImageDrawable(drawable);
        final View view=addViewToAnimLayout(animation_viewGroup,img,location);
        view.setAlpha(0.6f);//透明度       在界面上创建了一个图层
        //结束位置坐标
        int[] end_location=new int[2];
        txt.getLocationInWindow(end_location);//获取终点坐标
        int end_x=end_location[0];
        int end_y=end_location[1]-location[1];
        Animation mTranslateAnimation = new TranslateAnimation(0,end_x,0,end_y);//移动
        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 Animation.AnimationListener() {
            @Override
            public void onAnimationStart(Animation animation) {
                // TODO Auto-generated method stub
                Anim_num++;//记录动画数,每一个动画启动就触发一次
            }

            @Override
            public void onAnimationEnd(Animation animation) {
                // TODO Auto-generated method stub
                Anim_num--;
                if (Anim_num == 0) {//动画结束后
                    isClean = true;
                    handler.sendEmptyMessage(0);//通知更新UI界面
                }

            }

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

            }

        });

        view.startAnimation(mAnimationSet);
    }
下面是全部的实现过程代码

AnimActivity

package com.example.officer.myapplication;

import android.app.Activity;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.View;
import android.view.ViewGroup;
import android.view.animation.Animation;
import android.view.animation.AnimationSet;
import android.view.animation.RotateAnimation;
import android.view.animation.ScaleAnimation;
import android.view.animation.TranslateAnimation;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import Mygoods.GoodAdapter;


/**
 * Created by officer on 2015/12/7.
 */
public class AnimActivity extends Activity {

    private ListView listView;
    private TextView txt;
    private GoodAdapter goodAdapter;
    private int AnimationDuration=1000;//设置动画时间为1秒
    private int Anim_num=0;//获取正在执行的动画数
    private boolean isClean=false;//标示是否完成清理
    private FrameLayout animation_viewGroup;//存放动画
    private Handler handler=new Handler(){
        @Override
        public void handleMessage(Message msg) {
            switch(msg.what){
                case 0:
                    //用来清除动画后留下的垃圾
                    try{
                        animation_viewGroup.removeAllViews();
                    }catch(Exception e){
                        e.printStackTrace();
                    }

                    isClean = false;

                    break;
                default:
                    break;
            }
            super.handleMessage(msg);
        }
    };
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.lay_anim);
        init();
    }

    private void init(){
        listView=(ListView)findViewById(R.id.list);
        txt=(TextView)findViewById(R.id.txt);
        animation_viewGroup = createAnimLayout();
        Bitmap bt= BitmapFactory.decodeResource(getResources(),R.mipmap.ic_launcher);
        List<Map<String,Object>> list=new ArrayList<Map<String,Object>>();
        Map<String,Object> map=null;
        for(int i=0;i<30;i++){
            map=new HashMap<String,Object>();
            map.put("img",bt);
            list.add(map);
        }
        goodAdapter=new GoodAdapter(this,list);
        goodAdapter.SetOnSetHolderClickListener(new GoodAdapter.HolderClickListener() {
            @Override
            public void onHoldeClick(Drawable drawable, int[] start_location) {
                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;
            }
        }
    }

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

    private void setAnim(Drawable drawable,int[] 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 img=new ImageView(this);
        img.setImageDrawable(drawable);
        final View view=addViewToAnimLayout(animation_viewGroup,img,location);
        view.setAlpha(0.6f);//透明度       在界面上创建了一个图层
        //结束位置坐标
        int[] end_location=new int[2];
        txt.getLocationInWindow(end_location);//获取终点坐标
        int end_x=end_location[0];
        int end_y=end_location[1]-location[1];
        Animation mTranslateAnimation = new TranslateAnimation(0,end_x,0,end_y);//移动
        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 Animation.AnimationListener() {
            @Override
            public void onAnimationStart(Animation animation) {
                // TODO Auto-generated method stub
                Anim_num++;//记录动画数,每一个动画启动就触发一次
            }

            @Override
            public void onAnimationEnd(Animation animation) {
                // TODO Auto-generated method stub
                Anim_num--;
                if (Anim_num == 0) {//动画结束后
                    isClean = true;
                    handler.sendEmptyMessage(0);//通知更新UI界面
                }

            }

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

            }

        });

        view.startAnimation(mAnimationSet);
    }

    private View addViewToAnimLayout(ViewGroup vg,View view,int [] location){
        int x=location[0];
        int y=location[1];
        vg.addView(view);
        FrameLayout.LayoutParams layoutParams=new FrameLayout.LayoutParams(
                dip2px(this,90),dip2px(this,90)); //图层
        layoutParams.leftMargin=x;
        layoutParams.topMargin=y;
        view.setPadding(5,5,5,5);
        view.setLayoutParams(layoutParams);
        return view;
    }

    /**
     * dip转px
     */
    private int dip2px(Context context,float dp){
       float  scale=context.getResources().getDisplayMetrics().density;
        return (int)(dp*scale+0.5f);
    }

    @Override
    public void onLowMemory() {
        //内存过低情况
        isClean=true;
        try{
            animation_viewGroup.removeAllViews();
        }catch (Exception e){
            e.printStackTrace();
        }
        isClean=false;
        super.onLowMemory();
    }
}
接着是数据适配器

GoodAdapter

package Mygoods;

import android.content.Context;
import android.graphics.Bitmap;
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.TextView;

import com.example.officer.myapplication.R;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by officer on 2015/12/7.
 */
public class GoodAdapter extends BaseAdapter{
    private Context mContext;
    private LayoutInflater layoutInflater;
    private List<Map<String,Object>> list=new ArrayList<Map<String,Object>>();
    private HolderClickListener mHolderClickListener;
    public GoodAdapter(Context context,List<Map<String,Object>> list){
        this.mContext=context;
        layoutInflater=LayoutInflater.from(context);
        this.list=list;
    }

    @Override
    public int getCount() {
        return list.size();
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public Object getItem(int position) {
        return null;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        final int selectdId=position;
        final ViewHolder viewHolder;
        if(convertView==null){
            viewHolder=new ViewHolder();
            convertView=layoutInflater.inflate(R.layout.lay_item,null);
            viewHolder.img=(ImageView)convertView.findViewById(R.id.item_img);
            viewHolder.btn=(Button)convertView.findViewById(R.id.item_btn);
            convertView.setTag(viewHolder);
        }else{
            viewHolder=(ViewHolder)convertView.getTag();
        }
        viewHolder.img.setImageBitmap((Bitmap) list.get(position).get("img"));
        viewHolder.btn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                if(mHolderClickListener!=null){
                    int[] start_location=new int[2];
                    viewHolder.img.getLocationInWindow(start_location);
                    Drawable drawable=viewHolder.img.getDrawable();//获取一个新的drawable
                    mHolderClickListener.onHoldeClick(drawable,start_location);
                }
            }
        });
        return convertView;
    }

    public void SetOnSetHolderClickListener(HolderClickListener holderClickListener){
        this.mHolderClickListener=holderClickListener;
    }

    /**
     *事件接口
     */
    public interface HolderClickListener{
        public void onHoldeClick(Drawable drawable,int [] start_location);
    }

    class ViewHolder{
        ImageView img;
        Button btn;
    }
}





你可能感兴趣的:(动画,安卓,界面)