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. [Android]图片资源管理学习

    一.概念 几个概念:1.像素:像素是组成图像的最基本单位:点该点自身有大小,其中保存了颜色值 2.屏幕尺寸:screen size手机屏幕的物理尺寸.单位:inch(英寸)ex:4.0英寸 3.8英寸 ...

  2. GNU C的使用

    基本语法 gcc [options] [filenames]  说明:  在gcc后面可以有多个编译选项,同时进行多个编译操作.很多 的gcc选项包括一个以上的字符.因此你必须为每个选项指定各 自 ...

  3. 你的App为什么上不了TOP10?

     App市场风起云涌.但是,却仅仅有少数几个App能成为"暴发户",很多其它的则沉淀在应用商店中无人问津. 在移动互联网时代.智能手机成为了中心. 手机之所以智能.就在于手机上 ...

  4. Java并发专题 带返回结果的批量任务运行 CompletionService ExecutorService.invokeAll

    转载请标明出处:http://blog.csdn.net/lmj623565791/article/details/27250059 普通情况下,我们使用Runnable作为主要的任务表示形式,可是R ...

  5. Codeforces Round #269 (Div. 2) A B C

    先说C 题目链接:http://codeforces.com/problemset/problem/471/C 题目意思:有 n 张卡,问能做成多少种不同楼层(floor)的 house.注意这 n ...

  6. 纯CSS实现各类气球泡泡对话框效果

    原文 纯CSS实现各类气球泡泡对话框效果 一.关于纯CSS实现气泡对话框 首先,来张大图: 上边这张黄黄的,大大的,圆圆的,有个小尾巴,文字内容有些YY的图片,就是使用纯CSS实现的气泡对话框效果,一 ...

  7. C#多线程问题整合

    一.跨进程访问组件 错误:线程间操作无效: 从不是创建控件“XXX”的线程访问它 解决方法: 1:把CheckForIllegalCrossThreadCalls设置为false 这个方法只是不去捕获 ...

  8. awk使用的实例

    1.使用split函数 name.url的内容: 上海    http://trip.elong.com/shanghai/jingdian elong   destination 云南    htt ...

  9. java中线程机制

    java中线程机制,一开始我们都用的单线程.现在接触到多线程了. 多线性首先要解决的问题是:创建线程,怎么创建线程的问题: 1.线程的创建: 四种常用的实现方法 1.继承Thread. Thread是 ...

  10. 第二期“晋IT”分享成长沙龙

    本期主题:微信.打造品牌个体 报名方式:关注微信.回复"我要成长" "晋IT"沙龙费用:全程免费 "晋IT"沙龙文化:共通 共融 合作共赢 ...