• Android的ViewAnimator而它的子类ViewSwitcher-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<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>

    版权声明:本文博主原创文章。博客,未经同意不得转载。

  • 相关阅读:
    js数组中indesOf方法的使用
    js之数组排序
    大神的博客地址liferay
    CRM项目总结-封装PortletURLUtil
    有待整理
    摘要JSR168 PORLET标准手册汉化整理
    蓝色表格 -- 材料
    node 项目材料 集合
    一个浮动 css3效果
    代码 工具
  • 原文地址:https://www.cnblogs.com/blfshiye/p/4835537.html
Copyright © 2020-2023  润新知