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>

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

Android的ViewAnimator而它的子类ViewSwitcher-android学习之旅(三十三)的更多相关文章

  1. Android的View类介绍-android的学习之旅(十三)

    view概述 android绝大部分UI组件都放在android.view和android.widght包中,android的虽有UI组件都继承了View类. View类还有一个非常重要的子类:Vie ...

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

    ViewAnimator继承了FrameLayout,多个组件重合在一起,可以加入多个组件,然后切换的时候会有动画. ViewAnimator及其子类的继承关系 ViewAnimator常用属性 Vi ...

  3. 二、Android应用的界面编程(七)ViewAnimator及其子类[ ViewSwitcher、ImageSwitcher、TextSwitcher、ViewFlipper ]

    ViewAnimator是一个基类,它继承了FrameLayout.因此它表现出FrameLayout的特征,可以将多个View组“叠”在一起. ViewAnimator可以在View切换时表现出动画 ...

  4. Android开发自学笔记(Android Studio)—4.5 ProgressBar及其子类

    一.前言 ProgressBar本身代表了进度条组件,它还派生出了两个常用的组件:SeekBar和RatingBar,他们的使用方法类似,只是显示界面有一定的区别.我们看一下API文档中的说明: 从图 ...

  5. Android开发自学笔记(Android Studio)—4.4 AdapterView及其子类

    一.引言       AdapterView本身是一个抽象类,而它派生的子类在用法上也基本相似,只是在显示上有一定区别,因此把他们也归为一类.       AdapterView具有如下特征: Ada ...

  6. Android用户界面 UI组件--AdapterView及其子类(一) ListView及各种Adapter详解

    ListView就是列表组件,一般通过继承ListActivity使用系统提供的ListView. 所有的AdapterView组件都需要有一个对应的Adapter作为适配器来显示列表中元素的布局方式 ...

  7. Android用户界面 UI组件--TextView及其子类(二) Button,selector选择器,sharp属性

    1.XML文件中的OnClick 属性可以指定在Activity中处理点击事件的方法,Activity中必须定义该属性指定的值作为方法的名字且有一个View类型的参数,表示此物件被点击. 2.使用se ...

  8. Android用户界面 UI组件--TextView及其子类(一) TextView

    1.TextView android:autoLink设置是否当文本为URL链接/email/电话号码/map时,文本显示为可点击的链接.可选值(none /web/email/phone/map/a ...

  9. Android用户界面UI组件--AdapterView及其子类(三) ExpandableListView

    ExpandableListView: List中的每一项可以展开收缩. 一种伸缩式的ListView. android:cacheColorHint="#00000000" 这个 ...

随机推荐

  1. UVA 10245 The Closest Pair Problem 最近点问题 分治算法

    题意,给出n个点的坐标,找出两点间最近的距离,如果小于10000就输出INFINITY. 纯暴力是会超时的,所以得另辟蹊径,用分治算法. 递归思路将点按坐标排序后,分成两块处理,最近的距离不是在两块中 ...

  2. 常用PHP中花括号使用规则详解

    转自http://www.cnblogs.com/jayleke/archive/2011/11/08/2241609.html 1.简单句法规则(用花括号界定变量名,适用于PHP所有版本): $a ...

  3. wwwtyro/cellophane

    wwwtyro/cellophane A dead simple web terminal that gets all of the boilerplate out of the way and le ...

  4. ubuntu linux 13.04更新

    首先备份源列表: sudo cp /etc/apt/sources.list /etc/apt/sources.list_backup 而后用gedit或其他编辑器打开: gksu gedit /et ...

  5. php(LAMP)开发环境配置相关问题及解决办法

    相信很多像我一样初次接触到php开发的人,在配置基本的开发环境时都是一头雾水,为此小编特写下自己在安装配置php开发环境过程中遇到的一些问题,及解决办法. 1.LAMP组合,安装centons+apa ...

  6. SWT中的GridLayout(转)例子不错

    GridLayout 是一个非常强大的布局管理器,它可以实现很多复杂的布局,名字中暗示它将所有控件放置在类似网格的布局中.^__^GridLayout 有两个构造函数. GridLayout的构造函数 ...

  7. ubuntu 12.04英文版设置成中文版

    适用于ubuntu 12.04英文版的系统,其他版本号的设置应该是大同小异的. 进入ubuntu系统,在顶部齿状标志找到system... 2.在personal找到Language Support ...

  8. OpenCV 编程简单介绍(矩阵/图像/视频的基本读写操作)

    PS. 因为csdn博客文章长度有限制,本文有部分内容被截掉了.在OpenCV中文站点的wiki上有可读性更好.而且是完整的版本号,欢迎浏览. OpenCV Wiki :<OpenCV 编程简单 ...

  9. .NET开源 FAQ

    Microsoft至2014年11月12日本(PST)公布.NET开源.一个"隐居"商业帝国也迎来"改革开放".. . Q1:为什么要开放源码? Ans:由于. ...

  10. c++ 按行读取txt文本

    CStdioFile 类的声明保存在 afx.h 头文件中. CStdioFile 类继承自 CFile 类, CStdioFile 对象表示一个用运行时的函数 fopen 打开的 c 运行时的流式文 ...