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. 去掉chrome记住密码后自动填充表单的黄色背景

    chrome表单自动填充后,input文本框的背景会变成黄色的,通过审查元素可以看到这是由于chrome会默认给自动填充的input表单加上input:-webkit-autofill私有属性,然后对 ...

  2. web开发性能优化---用户体验篇

    怎样从技术角度怎样增强用户体验.都是非常多平台都在做的事情,依据个人实际经验碰到几种体验做下总结. 1.降低页面刷新白屏 适当使用ajax技术.改善刷新白屏现象. 2.信息提醒,邮件.站内信.短信在购 ...

  3. c++ stl algorithm: std::fill, std::fill_n

    std::fill 在[first, last)范围内填充值 #include <iostream> #include <vector> #include <algori ...

  4. C编译: makefile基础

    作者:Vamei 出处:http://www.cnblogs.com/vamei 欢迎转载,也请保留这段声明.谢谢! 在编译一个大型项目的时候,往往有很多目标文件.库文件.头文件以及最终的可执行文件. ...

  5. PostgreSQL代码分析,查询优化部分,pull_ands()和pull_ors()

    PostgreSQL代码分析,查询优化部分. 这里把规范谓词表达式的部分就整理完了,阅读的顺序例如以下: 一.PostgreSQL代码分析,查询优化部分,canonicalize_qual 二.Pos ...

  6. Beijing seminar: China shadow banking

    Beijing seminar: China shadow banking-张化桥-财新博客-新世纪的常识传播者-财新网 Beijing seminar: China shadow banking

  7. hdu4714(树形dp)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4714 题意:给你一棵树,,其中每去掉一条边或加一条边的代价均为1,让你求出将其变成一个圆的最小代价. ...

  8. Java8高中并发

    Java8中学并发 本文翻译自:http://jaxenter.com/lean-concurrency-in-java-8-49924.html 转载请注明出处:http://blog.csdn.n ...

  9. HDU 5066 Harry And Physical Teacher(物理题)

    HDU 5066 Harry And Physical Teacher 思路:利用物理里面的动量守恒公式.因为保证小车质量远大于小球.所以能够把小车质量当成无穷大带进去,得到答案为2 * v0 - v ...

  10. Spring MVC中一般 普通类调用service

    在Spring MVC中,Controller中使用service只需使用注解@Resource就行,但是一般类(即不使用@Controller注解的类)要用到service时,可用如下方法: 1.S ...