您的位置:首页 > 其它

ImageSwitcher,TextSwitcher实现图片,文字切换

2016-06-25 18:14 585 查看
ImageSwitcher想实现自动播放可以添加线程中

public class MyImageSwitch extends Activity implements ViewFactory,OnTouchListener{

private ImageSwitcher is;
private int [] images={R.drawable.btn_casepx_normal,
R.drawable.btn_casepx_press,R.drawable.btn_casequery_press,R.drawable.btn_casepx_press,R.drawable.btn_casequery_press};
private int index;
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.imageswitchs);
is=(ImageSwitcher) findViewById(R.id.Image);
is.setOnTouchListener(this);
is.setFactory(this);

}

@Override
public View makeView() {
ImageView iv=new ImageView(this);
iv.setImageResource(images[0]);
return iv;
}

float start=0.0f;
float end=0.0f;
//实现触屏
@Override
public boolean onTouch(View view, MotionEvent event) {
int action = event.getAction();//获取当前的动作
if(action==MotionEvent.ACTION_DOWN){ //当点下去的时候
start = event.getX();
return true;
}
if(action==MotionEvent.ACTION_UP){//获取当你点击完后的x轴
end = event.getX();
return true;
}
if(start-end>100){ //像左
index=index+1<images.length ? ++index : 0;

is.setInAnimation(this,android.R.anim.fade_in);//设置进的动画
is.setOutAnimation(this,android.R.anim.fade_out);//设置出的动画
}else if(end-start>20){
index=index-1>=0 ? --index:images.length-1;
is.setInAnimation(this,android.R.anim.fade_in);//设置进的动画
is.setOutAnimation(this,android.R.anim.fade_out);//设置出的动画
}
is.setImageResource(images[index]);
return true;
}
}


TextSwitcher使用方法:

public class MytextSwitch extends Activity implements ViewFactory ,OnTouchListener{

private TextSwitcher tsw;
private String [] texts={"付出一定有回报","人生巅峰","我有钱,我任性"};
private int index=0;
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.mytext);
tsw=(TextSwitcher) findViewById(R.id.textswitch);
tsw.setOnTouchListener(this);
tsw.setFactory(this);
}
@Override
public View makeView() {
TextView tv=new TextView(this);
tv.setText(texts[index]);
return tv;
}
float start=0.0f;
float end=0.0f;
@Override
public boolean onTouch(View arg0, MotionEvent event) {
int action = event.getAction();//获取当前的动作
if(action==MotionEvent.ACTION_DOWN){ //当点下去的时候
start = event.getX();
return true;
}
if(action==MotionEvent.ACTION_UP){//获取当你点击完后的x轴
end = event.getX();
return true;
}
if(start-end>50){ //像左
index=index+1<texts.length ? ++index : 0;

tsw.setInAnimation(this,android.R.anim.fade_in);//设置进的动画
tsw.setOutAnimation(this,android.R.anim.fade_out);//设置出的动画
}else if(end-start>50){
index=index-1>=0 ? --index:texts.length-1;
tsw.setInAnimation(this,android.R.anim.fade_in);//设置进的动画
tsw.setOutAnimation(this,android.R.anim.fade_out);//设置出的动画
}
tsw.setText(texts[index]);
return true;
}
}


《Android版本更新、热更新》系列课程视频

版本更新6.0,7.0统统搞定!!

热修复不在麻烦,再也不用担心上线后出bug!!

http://edu.csdn.net/course/detail/6523

http://edu.csdn.net/course/play/6523/131198

《Kotlin语法基础到实战开发》系列课程视频

http://edu.csdn.net/course/detail/6409?locationNum=7&fps=1&ref=srch&loc=1

http://edu.csdn.net/course/play/6409/123752
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: