package com.imageswitcherdemo;
import android.app.Activity;
import android.os.Bundle;
import android.support.v4.view.ViewPager.LayoutParams;
import android.view.Gravity;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnTouchListener;
import android.view.animation.AnimationUtils;
import android.widget.TextSwitcher;
import android.widget.TextView;
import android.widget.ViewSwitcher.ViewFactory;
/**
* 一個左右滑動瀏覽文本的Demo
*
* @author tianjf
*
*/
public class TextSwitcherDemoActivity extends Activity implements ViewFactory,
OnTouchListener {
private TextSwitcher textSwicher;
// 圖片數組
private String[] arrayTexts = { "文本01", "文本02", "文本03", "文本04" };
// 要顯示的圖片在圖片數組中的Index
private int textIndex;
// 左右滑動時手指按下的X座標
private float touchDownX;
// 左右滑動時手指鬆開的X座標
private float touchUpX;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.textdemo);
textSwicher = (TextSwitcher) findViewById(R.id.textSwicher);
// 爲TextSwitcher設置Factory,用來爲TextSwitcher製造TextView
textSwicher.setFactory(this);
// 設置TextSwitcher左右滑動事件
textSwicher.setOnTouchListener(this);
}
@Override
public View makeView() {
TextView textView = new TextView(this);
textView.setTextSize(100);
textView.setLayoutParams(new TextSwitcher.LayoutParams(
LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT));
textView.setGravity(Gravity.CENTER);
textView.setText(arrayTexts[textIndex]);
return textView;
}
@Override
public boolean onTouch(View v, MotionEvent event) {
if (event.getAction() == MotionEvent.ACTION_DOWN) {
// 取得左右滑動時手指按下的X座標
touchDownX = event.getX();
return true;
} else if (event.getAction() == MotionEvent.ACTION_UP) {
// 取得左右滑動時手指鬆開的X座標
touchUpX = event.getX();
// 從左往右,看前一文本
if (touchUpX - touchDownX > 100) {
// 取得當前要看的文本的index
textIndex = textIndex == 0 ? arrayTexts.length - 1
: textIndex - 1;
// 設置文本切換的動畫
textSwicher.setInAnimation(AnimationUtils.loadAnimation(this,
android.R.anim.slide_in_left));
textSwicher.setOutAnimation(AnimationUtils.loadAnimation(this,
android.R.anim.slide_out_right));
// 設置當前要看的文本
textSwicher.setText(arrayTexts[textIndex]);
// 從右往左,看下一張
} else if (touchDownX - touchUpX > 100) {
// 取得當前要看的文本的index
textIndex = textIndex == arrayTexts.length - 1 ? 0
: textIndex + 1;
// 設置文本切換的動畫
// 因爲Android沒有提供slide_out_left和slide_in_right,因此仿照
slide_in_left和slide_out_right編寫了slide_out_left和slide_in_right
textSwicher.setInAnimation(AnimationUtils.loadAnimation(this,
android.R.anim.fade_in));
textSwicher.setOutAnimation(AnimationUtils.loadAnimation(this,
android.R.anim.fade_out));
// 設置當前要看的文本
textSwicher.setText(arrayTexts[textIndex]);
}
return true;
}
return false;
}
}
<?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" >
<TextSwitcher
android:id="@+id/textSwicher"
android:layout_width="fill_parent"
android:layout_height="0dip"
android:layout_weight="1"
android:background="@android:color/white"
android:gravity="center" >
</TextSwitcher>
</LinearLayout>
android