Android的ViewAnimator及其子类ViewSwitcher-android学习之旅(三十三)

ViewAnimator继承了FrameLayout,多个组件重合在一起,可以加入多个组件,然后切换的时候会有动画。

ViewAnimator及其子类的继承关系

Android的ViewAnimator及其子类ViewSwitcher-android学习之旅(三十三)_第1张图片

ViewAnimator常用属性

这里写图片描述

ViewSwitcher的简介

ViewSwitcher继承了ViewAnimator,组件重叠。
setFactory()方法可以设置ViewFactory(ViewSwitcher.ViewFactory),用ViewFactroy来实现View。

仿android系统的Launcher界面

package peng.liu.test;

import android.app.Activity;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.GridView;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.ViewSwitcher;

import java.util.ArrayList;


public class MainActivity extends Activity {
    public static final int NUMBER_PER_SCREEN = 12;
    public static class DataItem{
        public String dataName;
        public Drawable drawable;
    }
    private ArrayList<DataItem> list = new ArrayList<DataItem>();
    private int screenNo = -1;
    private int screenCount;
    ViewSwitcher viewSwitcher;
    LayoutInflater infalter;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        for(int i = 0;i<40;i++){
            String lable = ""+i;
            Drawable drawableTwo = getResources().getDrawable(R.drawable.ic_launcher);
            DataItem item = new DataItem();
            item.dataName = lable;
            item.drawable = drawableTwo;
            list.add(item);
        }
        screenCount = list.size() % NUMBER_PER_SCREEN == 0 ? list.size()/NUMBER_PER_SCREEN:list.size()/NUMBER_PER_SCREEN+1;
        viewSwitcher = (ViewSwitcher) findViewById(R.id.viewSwitcher);
        viewSwitcher.setFactory(new ViewSwitcher.ViewFactory() {
            @Override
            public View makeView() {
                return infalter.inflate(R.layout.slide,null);
            }
        });
        next(null);
    }
    public void next(View view){
        if (screenNo < screenCount - 1){
            screenNo++;
            viewSwitcher.setInAnimation(this,R.anim.slide_in);
            viewSwitcher.setOutAnimation(this,R.anim.slide_out);
            (GridView)(viewSwitcher.getNextView()).setAdapter(adapter);
            viewSwitcher.showNext();
        }
    }
    public void prev(View view){
        if (screenNo > 0){
            screenNo--;
            viewSwitcher.setInAnimation(this,R.anim.slide_in);
            viewSwitcher.setOutAnimation(this,R.anim.slide_out);
            (GridView)(viewSwitcher.getNextView()).setAdapter(adapter);
            viewSwitcher.showNext();
        }
    }
    public BaseAdapter adapter = new BaseAdapter() {
        @Override
        public int getCount() {
            if (screenNo  == screenCount-1&&list.size()%NUMBER_PER_SCREEN != 0 ){
                return list.size()/NUMBER_PER_SCREEN;
            }else{
                return NUMBER_PER_SCREEN;
            }
        }

        @Override
        public DataItem getItem(int i) {
            return list.get(screenNo*NUMBER_PER_SCREEN+i);
        }

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

        @Override
        public View getView(int i, View convertView, ViewGroup viewGroup) {
            View view = convertView;
            if (convertView == null){
                view = infalter.inflate(R.layout.slide,null);
            }
            ImageView imageView = (ImageView) view.findViewById(R.id.imageView);
            imageView.setImageDrawable(getItem(i).drawable);
            TextView text = (TextView) view.findViewById(R.id.textView);
            text.setText(getItem(i).dataName);
            return view;
        }
    };
}

布局代码

<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:paddingLeft="@dimen/activity_horizontal_margin" android:paddingRight="@dimen/activity_horizontal_margin" android:paddingTop="@dimen/activity_vertical_margin" android:paddingBottom="@dimen/activity_vertical_margin" tools:context=".MainActivity" >

   <ViewSwitcher  android:layout_width="fill_parent" android:layout_height="fill_parent" android:id="@+id/viewSwitcher" />
    <Button  android:layout_width="wrap_content" android:layout_height="wrap_content" android:id="@+id/prev" android:layout_alignParentBottom="true" android:layout_alignParentLeft="true" android:onClick="prev" android:text="&lt;" />
    <Button  android:layout_width="wrap_content" android:layout_height="wrap_content" android:id="@+id/next" android:layout_alignParentBottom="true" android:layout_alignParentRight="true" android:onClick="next" android:text="&lt;" />
</RelativeLayout>
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent">
<ImageView  android:layout_width="wrap_content" android:layout_height="wrap_content" android:id="@+id/imageView" />
    <TextView  android:layout_width="wrap_content" android:layout_height="wrap_content" android:id="@+id/textView" android:gravity="center"/>
</LinearLayout>
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent">
<GridView  android:layout_width="wrap_content" android:layout_height="wrap_content" android:id="@+id/grid"/>
</LinearLayout>
<?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="@android:integer/config_mediumAnimTime" />
    </set>
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android">
    <translate  android:fromXDelta="0" android:toXDelta="-100%p" android:duration="@android:integer/config_mediumAnimTime" />
</set>

你可能感兴趣的:(android)