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

本文介绍了一种使用TextSwitcher和ImageSwitcher组件实现滑动屏幕切换文本和图片的方法。通过监听触摸事件,当用户左右滑动时,可以实现前后内容的切换,并配合淡入淡出动画提升用户体验。

4P{L]4%U_%%UX_C_6LE1FTD

78~SNSXPBHML1[J6G$1%P09

TextSwitcher

image

滑动屏幕,左滑或者右滑

image

代码:

<?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;
}
}













转载于:https://my.oschina.net/xiaofeiandroid/blog/632834

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值