Android ViewSwitcher组件

ViewSwitcher 代表了视图切换组件,本身继承了FrameLayout,可以将多个View叠加在一起,每次只显示一个组件,当从一个View切换到另一个View时,ViewSwitcher可以指定显示的动画。

setFactory主要用来产生需要的组件,这里其实就是一个ImageView。如果返回一个GridView,可以实现类似手机程序翻页的效果。

 

package com.viewswitcherdemo;

import android.app.Activity;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.ViewSwitcher;
import android.widget.ViewSwitcher.ViewFactory;

public class MainActivity extends Activity {

	// LayoutInflater这个类还是非常有用的,它的作用类似于 findViewById(),
	LayoutInflater inflater;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setContentView(R.layout.viewanimatordemo);
		setTitle("ViewAnimatorDemo");

		inflater = LayoutInflater.from(MainActivity.this);

		final int[] images = { R.drawable.header1, R.drawable.header2,
				R.drawable.header3, R.drawable.header4 };
		final int[] current = { -1 };

		final ViewSwitcher viewSwitcher = (ViewSwitcher) findViewById(R.id.viewSwitcher);

		viewSwitcher.setFactory(new ViewFactory() {

			@Override
			public View makeView() {
				// TODO Auto-generated method stub
				// LayoutInflater是用来找layout下xml布局文件,并且实例化!
				return inflater.inflate(R.layout.viewswitch_item, null);

			}
		});

		Button button1 = (Button) findViewById(R.id.button1);
		Button button2 = (Button) findViewById(R.id.button2);

		OnClickListener listener = new OnClickListener() {

			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub

				if (v.getId() == R.id.button2) {
					viewSwitcher.setInAnimation(MainActivity.this,
							android.R.anim.slide_in_left); // 奇怪怎么没有slide_in_right
					viewSwitcher.setOutAnimation(MainActivity.this,
							android.R.anim.slide_out_right);
					if (++current[0] >= images.length)
						current[0] = 0;
					((ImageView) viewSwitcher.getNextView())
							.setImageResource(images[current[0]]);
					viewSwitcher.showNext();
				} else {
					viewSwitcher.setInAnimation(MainActivity.this,
							android.R.anim.slide_in_left);
					viewSwitcher.setOutAnimation(MainActivity.this,
							android.R.anim.slide_out_right);
					if (--current[0] < 0)
						current[0] = 3;
					((ImageView) viewSwitcher.getNextView())
							.setImageResource(images[current[0]]);
					viewSwitcher.showPrevious();
				}
			}
		};
		button1.setOnClickListener(listener);
		button2.setOnClickListener(listener);
		// 初始化一下
		current[0] = 0;
		((ImageView) viewSwitcher.getNextView())
				.setImageResource(images[current[0]]);
		viewSwitcher.showNext();

	}

}


 

viewswitch_item.xml

<ImageView xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/imageView"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content" >

</ImageView>


viewanimatordemo.xml

<?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"
    android:orientation="vertical" >

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="ViewSwitcher 代表了视图切换组件,本身继承了FrameLayout,可以将多个View叠加在一起,每次只显示一个组件,当从一个View切换到另一个View时,ViewSwitcher可以指定显示的动画。"
        android:textColor="#f00"
        android:textSize="20sp" >
    </TextView>

    <ViewSwitcher
        android:id="@+id/viewSwitcher"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" >
    </ViewSwitcher>

    <LinearLayout
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:orientation="horizontal" >

        <Button
            android:id="@+id/button1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="上一个" >
        </Button>

        <Button
            android:id="@+id/button2"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="下一个" >
        </Button>
    </LinearLayout>

</LinearLayout>


 

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值