ImageSwitcher:
activity_main.xml
<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.p_w_picpathswitch.MainActivity" >
<ImageSwitcher
android:id="@+id/p_w_picpathSwitcher1_1"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_alignParentTop="true"
>
</ImageSwitcher>
</RelativeLayout>
<!-- android:inAnimation="@android:anim/slide_in_left"
android:outAnimation="@android:anim/slide_out_right"
-->
MianActivity
package com.example.p_w_picpathswitch;
import android.app.Activity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnTouchListener;
import android.widget.ImageSwitcher;
import android.widget.ImageView;
import android.widget.ViewSwitcher.ViewFactory;
public class MainActivity extends Activity implements ViewFactory ,OnTouchListener{
private ImageSwitcher p_w_picpathSwitch;
private int [] p_w_picpaths={
R.drawable.kobe0,
R.drawable.kobe1,
R.drawable.kobe3,
R.drawable.kobe4,
R.drawable.kobe6,
};
private int index; //要显示的图片的下标
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
p_w_picpathSwitch=(ImageSwitcher) findViewById(R.id.p_w_picpathSwitcher1_1);
//设置创建ImageView的工厂
p_w_picpathSwitch.setFactory(this);
//设置触屏事件
p_w_picpathSwitch.setOnTouchListener(this);
}
//ViewFactory工厂接口的方法,通过这个方法,给ImageSwitch组件提供两个
@Override
public View makeView() {
// TODO Auto-generated method stub
ImageView p_w_picpathView=new ImageView(this);
p_w_picpathView.setImageResource(p_w_picpaths[0]);
return p_w_picpathView;
}
//定义两个x坐标点
float startx=0.0F; //开始位置
float endx=0.0F; //结束位置
//触屏事件监听方法
@Override
public boolean onTouch(View v, MotionEvent event) {
if(event.getAction()==MotionEvent.ACTION_DOWN){
startx=event.getX();
return true;
}else if(event.getAction()==MotionEvent.ACTION_UP){
endx=event.getX();
//判断左滑动
if(startx-endx>20){
index=(index+1)<(p_w_picpaths.length-1)?++index:0;
p_w_picpathSwitch.setImageResource(p_w_picpaths[index]);
//在代码中设置动画效果
p_w_picpathSwitch.setInAnimation(this, android.R.anim.fade_in);
p_w_picpathSwitch.setOutAnimation(this, android.R.anim.fade_out);
}
//判断右滑动
if(endx-startx>20){
index=(index-1)>0?--index:p_w_picpaths.length-1;
p_w_picpathSwitch.setImageResource(p_w_picpaths[index]);
p_w_picpathSwitch.setInAnimation(this, android.R.anim.slide_in_left);
p_w_picpathSwitch.setOutAnimation(this, android.R.anim.slide_out_right);
}
}
return true;
}
}
TextSwitcher与ImageSwitcher类似,只需要将ImageSwitcher换成TextSwitcher
设置图片资源修改为设置文本即可
亿速云「云服务器」,即开即用、新一代英特尔至强铂金CPU、三副本存储NVMe SSD云盘,价格低至29元/月。点击查看>>
免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。