TextSwitcher,ImageSwitcher切换器

TextSwitcher和ImageSwitcher可以用来实现文本或图片的切换功能。

这两个组件使用上基本类似,先来看TextSwitcher的使用。

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

    <TextSwitcher
        android:id="@+id/switcher"
        android:layout_width="match_parent"
        android:layout_height="match_parent" >
    </TextSwitcher>

</LinearLayout>

通过程序来动态建立切换器中显示文字的TextView。

public class MainActivity extends Activity {

	private TextSwitcher switcher;

	// 准备一组要显示的文字
	private String[] allTexts = {
			"Specialized ViewSwitcher that contains only children of type TextView. A TextSwitcher is useful to animate a label on screen. Whenever setText(CharSequence) is called, TextSwitcher animates the current text out and animates the new text in.Summary[Expand",
			"]Inherited XML AttributesFrom class android.widget.ViewAnimatorFrom class android.widget.FrameLayoutFrom",
			" class android.view.ViewGroupFrom class android.view.View[Expand]Inherited ConstantsFrom class android.",
			"view.ViewGroupFrom class android.view.View[Expand]Inherited FieldsFrom class android.view.ViewPublic",
			" ConstructorsTextSwitcher(Context context)Creates a new empty TextSwitcher.TextSwitcher(Context context",
			", AttributeSet attrs)Creates a new empty TextSwitcher for the given context and with the specified set ",
			"attributes.Public Methodsvoid	 addView(View child, int index, ViewGroup.LayoutParams params)Adds a child",
			" view with the specified layout parameters.void	 onInitializeAccessibilityEvent(AccessibilityEvent " };

	// 准备当前要显示的索引
	private int index = 0;

	// 保存按下时的坐标和时间
	private float startX;
	private long startTime;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);

		Globals.init(this);

		// 设置默认使用的布局文件
		setContentView(R.layout.activity_main);

		switcher = (TextSwitcher) findViewById(R.id.switcher);

		// 设置建立TextView的工厂类
		switcher.setFactory(new ViewFactory() {

			@Override
			public View makeView() {
				// 建立要显示文字的TextView
				TextView text = new TextView(MainActivity.this);
				text.setTextColor(Color.BLACK);
				text.setBackgroundColor(Color.WHITE);
				text.setTextSize(16);
				text.setLayoutParams(new LayoutParams(
						LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT));

				return text;
			}
		});

		// 处理内容
		switcher.setText(allTexts[index]);

		// 设置动画效果, 系统动画功能不完善,一般都满足不了开发的需求, 因此这里完成一个自定义的平移动画
		// switcher.setInAnimation(AnimationUtils.loadAnimation(this,
		// android.R.anim.slide_in_left));
		// switcher.setOutAnimation(AnimationUtils.loadAnimation(this,
		// android.R.anim.slide_out_right));

		// 加入触屏监听, 使用该监听的前提是, 组件必须先加入过OnClickListener
		switcher.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
			}
		});

		switcher.setOnTouchListener(new OnTouchListener() {
			@Override
			public boolean onTouch(View v, MotionEvent event) {
				// 只支持单点操作
				if (event.getPointerCount() == 1) {
					if (event.getAction() == MotionEvent.ACTION_DOWN) {
						// 手指刚按到屏幕上
						startX = event.getX();
						startTime = System.currentTimeMillis();
					} else if (event.getAction() == MotionEvent.ACTION_MOVE) {
						// 手指在屏幕上滑动
					} else if (event.getAction() == MotionEvent.ACTION_UP) {
						// 手指从屏幕上离开
						// 先判断时间是否合法, 要求在1秒内完成滑动的动作
						if (System.currentTimeMillis() - startTime < 1000) {
							// 判断当前的横坐标
							if (startX - event.getX() >= 50) {
								// 下一页
								if (index < allTexts.length - 1) {
									index++;

									Animation inAnim = new TranslateAnimation(
											Globals.SCREEN_WIDTH + 10, 0, 0, 0);
									Animation outAnim = new TranslateAnimation(
											0, -Globals.SCREEN_WIDTH - 10, 0, 0);

									// 设置动画持续时间
									inAnim.setDuration(1000);
									outAnim.setDuration(1000);

									switcher.setInAnimation(inAnim);
									switcher.setOutAnimation(outAnim);

									switcher.setText(allTexts[index]);

								}
							} else if (event.getX() - startX >= 50) {
								// 上一页
								if (index > 0) {
									index--;

									Animation inAnim = new TranslateAnimation(
											-Globals.SCREEN_WIDTH - 10, 0, 0, 0);
									Animation outAnim = new TranslateAnimation(
											0, Globals.SCREEN_WIDTH + 10, 0, 0);
									// 设置动画持续时间
									inAnim.setDuration(1000);
									outAnim.setDuration(1000);

									switcher.setInAnimation(inAnim);
									switcher.setOutAnimation(outAnim);

									switcher.setText(allTexts[index]);
								}
							}
						}
					}
				} else {
					Toast.makeText(MainActivity.this, "只支持单点操作",
							Toast.LENGTH_SHORT).show();
				}

				return false;
			}
		});

	}
}

如果想改为ImageSwitcher,只需要将设置的内容变为图片,建立的组件变为ImageView即可。

















评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值