
image.png
目錄
TextSwitcher
TextSwitcher 繼承自ViewSwitcher, ViewSwitcher繼承自ViewAnimator.
使用其實(shí)現(xiàn)文字的切換.
使用方式:
<TextSwitcher
android:id="@+id/textSwitcher"
android:layout_width="400dp"
android:layout_height="400dp">
</TextSwitcher>
使用實(shí)例
activity_main.xml文件:
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<TextSwitcher
android:id="@+id/textSwitcher"
android:layout_width="400dp"
android:layout_height="400dp">
</TextSwitcher>
</android.support.constraint.ConstraintLayout>
代碼:
package com.example.user.viewsw;
import android.app.Activity;
import android.graphics.Color;
import android.os.Bundle;
import android.view.MotionEvent;
import android.view.View;
import android.view.animation.AnimationUtils;
import android.widget.LinearLayout;
import android.widget.TextSwitcher;
import android.widget.TextView;
import android.widget.Toast;
import android.widget.ViewSwitcher;
public class MainActivity extends Activity implements ViewSwitcher.ViewFactory, View.OnTouchListener {
/**
* TextSwitcher 的引用
*/
private TextSwitcher mtestSwitcher;
/**
* 文字?jǐn)?shù)組
*/
final String[] strs = new String[] {
"數(shù)學(xué)老師",
"美食家",
"天才",
"兒童"
};
/**
* 當(dāng)前選中的文字id序號
*/
private int currentPosition;
/**
* 按下點(diǎn)的X坐標(biāo)
*/
private float downX;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//TextSwitcher
mtestSwitcher = (TextSwitcher) findViewById(R.id.textSwitcher);
//設(shè)置Factory
mtestSwitcher.setFactory(this);
//設(shè)置OnTouchListener,我們通過Touch事件來切換圖片
mtestSwitcher.setOnTouchListener(this);
//初始位置為0
currentPosition = 0;
//設(shè)置動畫
mtestSwitcher.setInAnimation(AnimationUtils.loadAnimation(this,
android.R.anim.fade_in));
mtestSwitcher.setOutAnimation( AnimationUtils.loadAnimation(this,
android.R.anim.fade_out));
}
@Override
public View makeView() {
final TextView textView = new TextView(this);
textView.setTextSize(100);
textView.setLayoutParams(new TextSwitcher.LayoutParams(
LinearLayout.LayoutParams.FILL_PARENT, LinearLayout.LayoutParams.FILL_PARENT));
textView.setTextColor(Color.rgb(255, 0, 0));
textView.setText(strs[currentPosition]);
return textView;
}
@Override
public boolean onTouch(View v, MotionEvent event) {
switch (event.getAction()) {
case MotionEvent.ACTION_DOWN:{
//手指按下的X坐標(biāo)
downX = event.getX();
break;
}
case MotionEvent.ACTION_UP:{
float lastX = event.getX();
//抬起的時(shí)候的X坐標(biāo)大于按下的時(shí)候移動
if(lastX > downX){
if(currentPosition > 0){
currentPosition --;
mtestSwitcher.setCurrentText(strs[currentPosition]);
}else{
Toast.makeText(this, "已經(jīng)是第一", Toast.LENGTH_SHORT).show();
}
}
if(lastX < downX){
if(currentPosition < strs.length - 1){
currentPosition ++ ;
mtestSwitcher.setCurrentText(strs[currentPosition]);
}else{
Toast.makeText(this, "到了最后", Toast.LENGTH_SHORT).show();
}
}
}
break;
}
return true;
}
}
運(yùn)行結(jié)果:

image.png

image.png