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="<"
/>
<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="<"
/>
</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学习之旅(三十三)的更多相关文章
- Android的View类介绍-android的学习之旅(十三)
view概述 android绝大部分UI组件都放在android.view和android.widght包中,android的虽有UI组件都继承了View类. View类还有一个非常重要的子类:Vie ...
- Android的ViewAnimator及其子类ViewSwitcher-android学习之旅(三十三)
ViewAnimator继承了FrameLayout,多个组件重合在一起,可以加入多个组件,然后切换的时候会有动画. ViewAnimator及其子类的继承关系 ViewAnimator常用属性 Vi ...
- 二、Android应用的界面编程(七)ViewAnimator及其子类[ ViewSwitcher、ImageSwitcher、TextSwitcher、ViewFlipper ]
ViewAnimator是一个基类,它继承了FrameLayout.因此它表现出FrameLayout的特征,可以将多个View组“叠”在一起. ViewAnimator可以在View切换时表现出动画 ...
- Android开发自学笔记(Android Studio)—4.5 ProgressBar及其子类
一.前言 ProgressBar本身代表了进度条组件,它还派生出了两个常用的组件:SeekBar和RatingBar,他们的使用方法类似,只是显示界面有一定的区别.我们看一下API文档中的说明: 从图 ...
- Android开发自学笔记(Android Studio)—4.4 AdapterView及其子类
一.引言 AdapterView本身是一个抽象类,而它派生的子类在用法上也基本相似,只是在显示上有一定区别,因此把他们也归为一类. AdapterView具有如下特征: Ada ...
- Android用户界面 UI组件--AdapterView及其子类(一) ListView及各种Adapter详解
ListView就是列表组件,一般通过继承ListActivity使用系统提供的ListView. 所有的AdapterView组件都需要有一个对应的Adapter作为适配器来显示列表中元素的布局方式 ...
- Android用户界面 UI组件--TextView及其子类(二) Button,selector选择器,sharp属性
1.XML文件中的OnClick 属性可以指定在Activity中处理点击事件的方法,Activity中必须定义该属性指定的值作为方法的名字且有一个View类型的参数,表示此物件被点击. 2.使用se ...
- Android用户界面 UI组件--TextView及其子类(一) TextView
1.TextView android:autoLink设置是否当文本为URL链接/email/电话号码/map时,文本显示为可点击的链接.可选值(none /web/email/phone/map/a ...
- Android用户界面UI组件--AdapterView及其子类(三) ExpandableListView
ExpandableListView: List中的每一项可以展开收缩. 一种伸缩式的ListView. android:cacheColorHint="#00000000" 这个 ...
随机推荐
- sql中的CHARINDEX和暂时表
update #temp set #temp.Recycle=case when UnionA.num>0 then 1 else 0 end from (select GradeID,sum( ...
- ffmpeg 频中分离 video audio 截取片断
1.获取视频的信息 ffmpeg -i video.avi 2,将图片序列分解合成视频 ffmpeg -i src.mpg image%d.jpg ffmpeg -f image2 -i ...
- Object.Instantiate 实例
static function Instantiate (original : Object, position : Vector3, rotation : Quaternion) : Object ...
- <xliff:g>标签
摘要: 这是Android4.3Mms源代码中的strings.xml的一段代码: <!--Settings item desciption for integer auto-delete sm ...
- sql使用存储过程和交易
在过去的一年.学习数据库的时候学校有存储过程.永远只是知道一些理论,我不知道怎么用.时隔一年,最终找到怎样使用存储过程了. 在机房收费系统中.有些操作.须要多次运行sql语句,多次运行完毕才算是完毕这 ...
- 从零开始,创建GitHub团队开发环境
从零开始,创建GitHub团队开发环境 GitHub提供免费的团队环境,不过免费仓库容量是300MB,请大家注意. 申请GitHub个人账号 1. 使用浏览器访问GitHub主页.如果使用IE,尽量不 ...
- js快速分享代码
这是一款简单易用的文章分享工具,您只需将下面的html代码拷贝到模板中就可以实现文章快速分享功能.如果您想分享你的博客.个人网站或者企业网站等等,下面是两款不错的分享工具,值得拥有! 1. <d ...
- red hat Linux 使用CentOS yum源更新
red hat linux是商业版软件,没有经过注册是无法使用红帽 yum源更新软件的,使用CentOS源更新操作如下: 1.删除red hat linux 原有的yum 源 rpm -aq | gr ...
- WPF案例(二)模拟Apple OS 界面前后180度反转
原文:WPF案例(二)模拟Apple OS 界面前后180度反转 我们在设计应用程序界面的时候,为了充分利用界面空间,住住需要灵活的界面布局方式,比如可以在界面正面空间上定义一个Chart,背面空间上 ...
- SharePoint 2013的HTML5特性之响应式布局
今天偶然看到一本书<Pro SharePoint 2013 Branding and Responsive Web Development>,看到SharePoint 2013基于HTML ...