1. 程式人生 > >ListView GridView中item載入顯示的動畫效果

ListView GridView中item載入顯示的動畫效果

首先看一下效果圖,ListView和GridView中item顯示動畫

這裡寫圖片描述](https://img-blog.csdn.net/20151110160058272 ![這裡寫圖片描述這裡寫圖片描述這裡寫圖片描述

1.ListView中的item載入顯示動畫
1)直接給ListView設定動畫

 private void initView() {
        setContentView(R.layout.activity_listview);
        ListView lv = (ListView) findViewById(R.id.activity_listview_lv);
        lv.setLayoutAnimation(getAnimationController());
//        ListViewAdapter  adapter = new ListViewAdapter();
mStrings = new String[]{"A", "S", "D", "F", "G", "H", "J", "Q", "W", "E", "R", "T"}; ArrayAdapter<String> arrayAdapter = new ArrayAdapter<String>(this, R.layout.item_listview_text, R.id.item_listview_tv, mStrings); lv.setAdapter(arrayAdapter); } /** * Layout動畫 即item家在現實的動畫,可以更改設定為各種樣式 * * @return
*/
protected LayoutAnimationController getAnimationController() { int duration = 300; AnimationSet set = new AnimationSet(true); Animation animation = new AlphaAnimation(0.0f, 1.0f); animation.setDuration(duration); set.addAnimation(animation); animation = new
TranslateAnimation(Animation.RELATIVE_TO_SELF, 0.0f, Animation.RELATIVE_TO_SELF, 0.0f, Animation.RELATIVE_TO_SELF, -1.0f, Animation.RELATIVE_TO_SELF, 0.0f); animation.setDuration(duration); set.addAnimation(animation); LayoutAnimationController controller = new LayoutAnimationController(set, 0.5f); controller.setOrder(LayoutAnimationController.ORDER_NORMAL); return controller; }

2)也可以分別給不同的item設定動畫

class ListViewAdapter extends BaseAdapter {

        private int duration =2000;
        private Animation push_left_in, push_right_in;
        private Animation slide_top_to_bottom, slide_bottom_to_top;

        public ListViewAdapter() {
            push_left_in = AnimationUtils.loadAnimation(ListViewActivity.this, R.anim.push_left_in);
            push_right_in = AnimationUtils.loadAnimation(ListViewActivity.this, R.anim.push_right_in);
            slide_top_to_bottom = AnimationUtils.loadAnimation(ListViewActivity.this, R.anim.slide_top_to_bottom);
            slide_bottom_to_top = AnimationUtils.loadAnimation(ListViewActivity.this, R.anim.slide_bottom_to_top);
        }

        @Override
        public int getCount() {
            return mStrings.length;
        }

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

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

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            ViewHolder holder = null;
            if (convertView == null) {
                holder = new ViewHolder();
                convertView = View.inflate(ListViewActivity.this,
                        R.layout.item_listview_text, null);
                holder.tv = (TextView) convertView.findViewById(R.id.item_listview_tv);
                convertView.setTag(holder);
                if (position % 2 == 0) {
                    push_left_in.setDuration(duration);
                    convertView.setAnimation(push_left_in);
                } else {
                    push_right_in.setDuration(duration);
                    convertView.setAnimation(push_right_in);
                }

//                if(position==0){
//                    slide_bottom_to_top.setDuration(duration);
//                    convertView.setAnimation(slide_bottom_to_top);
//                }
//                else{
//                    slide_top_to_bottom.setDuration(duration);
//                    convertView.setAnimation(slide_top_to_bottom);
//                }

            } else {
                holder = (ViewHolder) convertView.getTag();
            }
            holder.tv.setText(mStrings[position]);
            return convertView;
        }
    }

    class ViewHolder {
        TextView tv;
    }

資原始檔push_left_in.xml

<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android">
    <translate android:fromXDelta="-100%p" android:toXDelta="0" android:duration="300"/>
    <alpha android:fromAlpha="0.0" android:toAlpha="1.0" android:duration="300" />
</set>

pubsh_right_in.xml

<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android">
    <translate android:fromXDelta="100%p" android:toXDelta="0" android:duration="300"/>
    <alpha android:fromAlpha="0.0" android:toAlpha="1.0" android:duration="300" />
</set>

slide_bottom_to_top.xml

<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android" android:interpolator="@android:anim/accelerate_interpolator">
    <translate android:fromYDelta="100%" android:toXDelta="0" android:duration="300" />
    <alpha android:fromAlpha="0.0" android:toAlpha="1.0" android:duration="300" />
</set>

slide_top_to_bottom.xml

<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android" android:interpolator="@android:anim/accelerate_interpolator">
    <translate android:fromYDelta="-100%" android:toXDelta="0" android:duration="300" />
    <alpha android:fromAlpha="0.0" android:toAlpha="1.0" android:duration="300" />
</set>

這兩種方式的效果是一樣的

2.再看一下GridView的動畫效果,為了顯示更炫酷的效果,這裡直接用第二種方式顯示動畫


import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.view.animation.Animation;
import android.view.animation.TranslateAnimation;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.GridView;
import android.widget.ImageView;

import com.zf.svg.R;

import java.util.Random;

public class ZdeomActivity extends Activity {

    private GridView gv;
    private Button btn;
    private TranslateAnimation taLeft, taRight, taTop, taBlow;
    private int[] imgList = new int[100];
    private MyAdapter mAdapter;
    private LayoutInflater mInflater;
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_grid);
        this.InitView();
        this.InitAnima();
        this.InitData();
    }
    private void InitAnima() {
        // TODO Auto-generated method stub
        taLeft = new TranslateAnimation(Animation.RELATIVE_TO_PARENT, 1.0f,
                Animation.RELATIVE_TO_PARENT, 0.0f,
                Animation.RELATIVE_TO_PARENT, 0.0f,
                Animation.RELATIVE_TO_PARENT, 0.0f);
        taRight = new TranslateAnimation(Animation.RELATIVE_TO_PARENT, -1.0f,
                Animation.RELATIVE_TO_PARENT, 0.0f,
                Animation.RELATIVE_TO_PARENT, 0.0f,
                Animation.RELATIVE_TO_PARENT, 0.0f);
        taTop = new TranslateAnimation(Animation.RELATIVE_TO_PARENT, 0.0f,
                Animation.RELATIVE_TO_PARENT, 0.0f,
                Animation.RELATIVE_TO_PARENT, 1.0f,
                Animation.RELATIVE_TO_PARENT, 0.0f);
        taBlow = new TranslateAnimation(Animation.RELATIVE_TO_PARENT, 0.0f,
                Animation.RELATIVE_TO_PARENT, 0.0f,
                Animation.RELATIVE_TO_PARENT, -1.0f,
                Animation.RELATIVE_TO_PARENT, 0.0f);
        taLeft.setDuration(1000);
        taRight.setDuration(1000);
        taTop.setDuration(1000);
        taBlow.setDuration(1000);
    }
    private void InitData() {
        // TODO Auto-generated method stub
        for (int i = 0; i <100; i++) {
            imgList[i] = R.mipmap.ic_launcher;
        }
        mAdapter = new MyAdapter();
        gv.setAdapter(mAdapter);
    }
    private void InitView() {
        // TODO Auto-generated method stub
        gv = (GridView) findViewById(R.id.activity_grid_gv);
        btn = (Button) findViewById(R.id.activity_grid_bt);
        btn.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub
                mAdapter = null;
                mAdapter = new MyAdapter();
                gv.setAdapter(mAdapter);
                mAdapter.notifyDataSetChanged();
            }
        });
        mInflater = (LayoutInflater) this
                .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    }
    private class MyAdapter extends BaseAdapter {
        @Override
        public int getCount() {
            // TODO Auto-generated method stub
            return imgList.length;
        }
        @Override
        public Object getItem(int position) {
            // TODO Auto-generated method stub
            return imgList[position];
        }
        @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
            ViewHolder holder = null;
            if (convertView == null) {
                convertView = mInflater.inflate(R.layout.item_grid_iv, null);
                holder = new ViewHolder();
                holder.image = (ImageView) convertView
                        .findViewById(R.id.item_grid_iv);
                convertView.setTag(holder);
            } else {
                holder = (ViewHolder) convertView.getTag();
            }
            int imgID = imgList[position];
            holder.image.setImageResource(imgID);
            Random ran = new Random();
            int rand = ran.nextInt(4);
            switch (rand) {
                case 0:
                    convertView.startAnimation(taLeft);
                    break;
                case 1:
                    convertView.startAnimation(taRight);
                    break;
                case 2:
                    convertView.startAnimation(taTop);
                    break;
                case 3:
                    convertView.startAnimation(taBlow);
                    break;
            }
            return convertView;
        }
        class ViewHolder {
            public ImageView image;
        }
    }
}