目录
TextSwitcher
TextSwitcher 继承自ViewSwitcher, ViewSwitcher继承自ViewAnimator.
使用其实现文字的切换.
使用方式:
<TextSwitcher
android:id="@+id/textSwitcher"
android:layout_width="400dp"
android:layout_height="400dp">
</TextSwitcher>
使用实例
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;
/**
* 文字数组
*/
final String[] strs = new String[] {
"数学老师",
"美食家",
"天才",
"儿童"
};
/**
* 当前选中的文字id序号
*/
private int currentPosition;
/**
* 按下点的X坐标
*/
private float downX;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//TextSwitcher
mtestSwitcher = (TextSwitcher) findViewById(R.id.textSwitcher);
//设置Factory
mtestSwitcher.setFactory(this);
//设置OnTouchListener,我们通过Touch事件来切换图片
mtestSwitcher.setOnTouchListener(this);
//初始位置为0
currentPosition = 0;
//设置动画
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坐标
downX = event.getX();
break;
}
case MotionEvent.ACTION_UP:{
float lastX = event.getX();
//抬起的时候的X坐标大于按下的时候移动
if(lastX > downX){
if(currentPosition > 0){
currentPosition --;
mtestSwitcher.setCurrentText(strs[currentPosition]);
}else{
Toast.makeText(this, "已经是第一", 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;
}
}
运行结果: