Android教程網
  1. 首頁
  2. Android 技術
  3. Android 手機
  4. Android 系統教程
  5. Android 游戲
 Android教程網 >> Android技術 >> 關於Android編程 >> Android的ViewAnimator及其子類ViewSwitcher-android學習之旅(三十三)

Android的ViewAnimator及其子類ViewSwitcher-android學習之旅(三十三)

編輯:關於Android編程

ViewAnimator繼承了FrameLayout,多個組件重合在一起,可以加入多個組件,然後切換的時候會有動畫。

ViewAnimator及其子類的繼承關系

這裡寫圖片描述

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 list = new ArrayList();
    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;
        }
    };
}

布局代碼



   



    






    
        
    


    
  1. 上一頁:
  2. 下一頁:
熱門文章
閱讀排行版
Copyright © Android教程網 All Rights Reserved