TextSwitcher结合OnTouchListener实现翻页效果

来源:互联网 发布:学生党淘宝店铺白菜价 编辑:程序博客网 时间:2024/06/05 00:29

1、activity


import android.annotation.SuppressLint;
import android.app.Activity;
import android.content.Context;
import android.graphics.Color;
import android.os.Bundle;
import android.util.Log;
import android.view.MotionEvent;
import android.view.View;
import android.view.WindowManager;
import android.view.animation.AnimationUtils;
import android.widget.TextSwitcher;
import android.widget.TextView;
import android.widget.ViewSwitcher.ViewFactory;


@SuppressLint("NewApi")
public class MainActivity extends Activity {


// 索引  
    private int index;  
    // 文本数组  
    private String[] poemArray = { 
    "文本(text),从词源上来说,它表示编织的东西。这与中国“文”的概念颇有类似之处。“文”取象人形,指纹身,指花纹。《说文解字叙》称:“仓颉初作书,盖依类象形,故曰文。”“文者,物象之本。”物象均具纹路色彩,因以“文”来指称。《周易·系辞下》记伏羲氏“观鸟兽之文”,鸟兽身上的花纹彩羽。该书又载“物相杂故曰文”,物体的形状、线条色彩相互交错,这也是文。“观乎天文,以察时变,观乎人文,以化成天下。”《说文解字》解释“文”为“错画也。”", 
    "但是文本的概念后来主要变成了:“任何由书写所固定下来的任何话语。”对语言学家来说,文本指的是作品的可见可感的表层结构,是一系列语句串联而成的连贯序列。文本可能只是一个单句,例如谚语、格言、招牌等,但比较普遍的是由一系列句子组成。文本和段落的区别在于,文本构成了一个相对封闭、自足的系统。前苏联符号学家洛特曼指出,文本是外观的,即用一定的符号来表示;它是有限的,既有头有尾;它有内部结构。罗兰·巴特文本一方面是能指,即实际的语言符号以及由它们所组成的词、句子和段落章节,另一方面是所指,即固定的确定的和单一的意思,为表达这种意思的正确性所限定。以上从技术上中性地对文本做出基本界定。", 
    "“文本”一词来自英文text,另有本文、正文、语篇和课文等多种译法。这个词广泛应用于语言学和文体学中,而且也在文学理论与批评中扮演活跃的角色。但它含义丰富而不易界定,给实际运用和理解带来一定困难。可以一般地说,文本是语言的实际运用形态。而在具体场合中,文本是根据一定的语言衔接和语义连贯规则而组成的整体语句或语句系统,有待于读者阅读。和文本相关的是文本学和解释学这两个领域。", 
    "大多数Windows文本文件使用ANSI、OEM或者Unicode编码。Windows所指的ANSI编码通常是1字节的ISO-8859编码,不过对于像中文、日文、朝鲜文这样的环境,需要使用2字节字符集。在过渡至Unicode前,Windows一直用ANSI作为系统默认的编码。而OEM编码,也是通常所说的MS-DOS代码页,是IBM为早期IBM个人电脑的文本模式显示系统定义的。在全屏的MS-DOS程序中同时使用了图形的和按行绘制的字符。新版本的Windows可以使用UTF-16LE和UTF-8之类的Unicode编码。" };  

    int windowWidth;
    
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

//定义文字切换器  
        final TextSwitcher ts = (TextSwitcher) findViewById(R.id.textSwitcher);  
  
        //定义视图显示工厂,并设置  
        ts.setFactory(new ViewFactory() {  
  
            public View makeView() {  
                TextView tv =new TextView(MainActivity.this);  
                tv.setTextSize(32);  
                tv.setTextColor(Color.BLACK);  
                return tv;  
            }  
        });  
  
        WindowManager wm = (WindowManager) this
.getSystemService(Context.WINDOW_SERVICE);
   windowWidth = wm.getDefaultDisplay().getWidth();
        
        // 设置文本来源  
        ts.setText(poemArray[index]);  
        ts.setOnTouchListener(new View.OnTouchListener() {

@Override
public boolean onTouch(View v, MotionEvent event) {

if(event.getAction() == MotionEvent.ACTION_DOWN){
// 计算屏幕触发坐标  判断翻页方向
int eventWidth = (int) event.getX();
if(eventWidth >= windowWidth/2 ){
// 设置切入动画  
ts.setInAnimation(AnimationUtils.loadAnimation(getApplicationContext(), android.R.anim.slide_in_left));  
// 设置切出动画  
ts.setOutAnimation(AnimationUtils.loadAnimation(getApplicationContext(), android.R.anim.slide_out_right));  
// 点击会切换图片  
if(index < (poemArray.length - 1)){
index++;  
ts.setText(poemArray[index]);  
}
}else {
// 设置切入动画  
ts.setInAnimation(AnimationUtils.loadAnimation(getApplicationContext(), R.anim.push_right_in));  
// 设置切出动画  
ts.setOutAnimation(AnimationUtils.loadAnimation(getApplicationContext(), R.anim.push_left_out)); 
// 点击会切换图片  
if(index != 0){
index--;  
ts.setText(poemArray[index]);  
}
}
Log.i("lj", "eventWidth: "+eventWidth);
}
return true;
}
});

}

}


2、activity_main


<RelativeLayout 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:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context="com.example.wifitestdemo.MainActivity" >

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


</RelativeLayout>


0 0
原创粉丝点击