安卓学习UI组件-TextSwitcher和ImageSwitcher-文字和图片切换

TextSwitcher

滑动屏幕,左滑或者右滑

代码:

<?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
安卓学习UI组件-TextSwitcher和ImageSwitcher-文字和图片切换_第1张图片滑动后安卓学习UI组件-TextSwitcher和ImageSwitcher-文字和图片切换_第2张图片
代码:
<?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;
}
}













你可能感兴趣的:(安卓学习UI组件-TextSwitcher和ImageSwitcher-文字和图片切换)