TextSwitcher
滑动屏幕,左滑或者右滑
代码:
package com.example.administrator.myapplication; import android.support.v7.app.ActionBarActivity; import android.os.Bundle; import android.view.MotionEvent; import android.view.View; import android.widget.TextSwitcher; import android.widget.TextView; import android.widget.ViewSwitcher; public class MainActivity2 extends ActionBarActivity implements ViewSwitcher.ViewFactory,View.OnTouchListener { private TextSwitcher textSwitcher; private String[] texts={"岁月是一把无情的杀猪刀","岁月静好,现世安稳","我有钱我任性"}; private int index; //默认为0 @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main2); textSwitcher= (TextSwitcher) findViewById(R.id.textSwitcher); textSwitcher.setOnTouchListener(this); textSwitcher.setFactory(this); } @Override public View makeView() { TextView tv=new TextView(this); tv.setText(texts[index]); return tv; } float startX=0.0f; float endX=0.0f; @Override public boolean onTouch(View v, MotionEvent event) { int action=event.getAction();//获取当前的事件动作 System.out.println("action="+action);//sout if(action==MotionEvent.ACTION_DOWN) { startX=event.getX(); return true; } if(action==MotionEvent.ACTION_UP) { endX=event.getX(); if(startX-endX>20)//下一张 { index=(index+120){ //上一张 index=(index-1>=0?--index:texts.length-1); // imageSwitcher.setImageResource(images[index]); textSwitcher.setInAnimation(this,android.R.anim.fade_in); textSwitcher.setOutAnimation(this,android.R.anim.fade_out); } System.out.println("index"+index); textSwitcher.setText(texts[index]); } return true; } }
ImageSwitcher
滑动后
代码:
package com.example.administrator.myapplication; import android.app.Activity; import android.os.Bundle; import android.view.MotionEvent; import android.view.View; import android.widget.ImageSwitcher; import android.widget.ImageView; import android.widget.TextView; import android.widget.ViewSwitcher; public class MainActivity extends Activity implements ViewSwitcher.ViewFactory,View.OnTouchListener{ private ImageSwitcher imageSwitcher; private int[] images={R.mipmap.pic11,R.mipmap.pic13,R.mipmap.pic15,R.mipmap.pic6}; private String[] texts={"岁月是一把无情的杀猪刀","岁月静好,现世安稳","我有钱我任性","当你老了"}; private int index; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); imageSwitcher=(ImageSwitcher)findViewById(R.id.imageSwitcher); imageSwitcher.setOnTouchListener(this); imageSwitcher.setFactory(this); } @Override public View makeView() { ImageView iv=new ImageView(this); iv.setImageResource(images[0]); TextView tv=new TextView(this); tv.setText(texts[index]); return iv; } float startX=0.0f; float endX=0.0f; // 触屏事件 @Override public boolean onTouch(View v, MotionEvent event) { int action=event.getAction();//获取当前的事件动作 System.out.println("action="+action);//sout if(action==MotionEvent.ACTION_DOWN) { startX=event.getX(); return true; } if(action==MotionEvent.ACTION_UP) { endX=event.getX(); if(startX-endX>20)//下一张 { index=(index+120){ //上一张 index=(index-1>=0?--index:images.length-1); // imageSwitcher.setImageResource(images[index]); imageSwitcher.setInAnimation(this,android.R.anim.fade_in); imageSwitcher.setOutAnimation(this,android.R.anim.fade_out); } System.out.println("index"+index); imageSwitcher.setImageResource(images[index]); } return true; } }