TextSwitcherandroid
滑動屏幕,左滑或者右滑app
代碼:ide
<?xml version="1.0" encoding="utf-8"?>
<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.administrator.myapplication.MainActivity2">
<TextSwitcher
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/textSwitcher"
android:layout_alignParentTop="true"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true" />
</RelativeLayout>
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+1<texts.length?++index:0);
textSwitcher.setInAnimation(this,android.R.anim.fade_in);
textSwitcher.setOutAnimation(this, android.R.anim.fade_out);
// imageSwitcher.setImageResource(images[index]);
}
else if(endX-startX>20){ //上一張
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
滑動後
代碼:
<?xml version="1.0" encoding="utf-8"?>
<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.administrator.myapplication.MainActivity">
<ImageSwitcher
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/imageSwitcher"
android:layout_alignParentTop="true"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true" />
</RelativeLayout>
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+1<images.length?++index:0); imageSwitcher.setInAnimation(this,android.R.anim.fade_in); imageSwitcher.setOutAnimation(this, android.R.anim.fade_out); // imageSwitcher.setImageResource(images[index]); } else if(endX-startX>20){ //上一張 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; }}