ImageSwitcher自定意效果+定时切换图片

时间:2022-11-12 23:01:36
Activity实现
1 import android.app.Activity;
import android.os.Bundle;
import android.view.MotionEvent;
import android.view.View;
import android.view.Window;
import android.view.animation.AnimationUtils;
import android.widget.Gallery;
import android.widget.ImageSwitcher;
import android.widget.ImageView; import android.widget.Gallery.LayoutParams;
import android.widget.Toast;
import android.widget.ViewSwitcher.ViewFactory; public class Switcher extends Activity implements
ViewFactory {
private ImageSwitcher is;
private int Iindex=0;
private Gallery gallery;
/**
* 按下点的X坐标
*/
private float downX;
private int DuringTime=10000; private Integer[] mThumbIds = {R.drawable.a,R.drawable.b, R.drawable.c,
R.drawable.d,R.drawable.e, R.drawable.f, R.drawable.g}; private Integer[] mImageIds ={R.drawable.a,R.drawable.b, R.drawable.c,
R.drawable.d,R.drawable.e, R.drawable.f, R.drawable.g}; @Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(R.layout.switcher); is = (ImageSwitcher) findViewById(R.id.switcher);
is.setFactory(this); //is.setInAnimation(AnimationUtils.loadAnimation(this,R.anim.push_bottom_in));//渐入效果
is.setInAnimation(AnimationUtils.loadAnimation(this,android.R.anim.slide_in_left));//系统提供渐变效果
is.setOutAnimation(AnimationUtils.loadAnimation(this,
android.R.anim.slide_out_right));//渐出效果
//定时执行图片切换
is.postDelayed(new Runnable() {
public void run() {
if(Iindex==mImageIds.length-1){
Iindex=0;
}else{
Iindex++;
}
is.setImageResource(mImageIds[Iindex]);
is.postDelayed(this, DuringTime);
}
}, DuringTime); } @Override
public View makeView() {
ImageView i = new ImageView(this);
i.setBackgroundColor(0xFF000000);
i.setScaleType(ImageView.ScaleType.FIT_CENTER);
i.setLayoutParams(new ImageSwitcher.LayoutParams(
LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT));
i.setImageResource(mImageIds[Iindex]);
i.setOnTouchListener(new View.OnTouchListener() {
@Override
public boolean onTouch(View view, MotionEvent event) {
switch (event.getAction()) {
case MotionEvent.ACTION_DOWN:{
//手指按下的X坐标
downX = event.getX();
break;
}
case MotionEvent.ACTION_UP:{
float lastX = event.getX();
//抬起的时候的X坐标大于按下的时候就显示上一张图片
if(lastX > downX){
if(Iindex==0){
Iindex=mImageIds.length-1;
}else{
Iindex--;
}
is.setImageResource(mImageIds[Iindex]);
}else if(lastX < downX){
if(Iindex==mImageIds.length-1){
Iindex=0;
}else{
Iindex++;
}
is.setImageResource(mImageIds[Iindex]);
}
} break;
}
return true;
} });
//点击事件换图
/* i.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
if(Iindex==mImageIds.length-1){
Iindex=0;
}else{
Iindex++;
}
is.setImageResource(mImageIds[Iindex]);
//sm("you click this picture");
}
});*/
return i;
} private void sm(String mes){
Toast.makeText(this,mes,Toast.LENGTH_SHORT).show();
}
}
布局代码
1 <?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"> <ImageSwitcher android:id="@+id/switcher"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_alignParentTop="true"
android:layout_alignParentLeft="true"
/>
</RelativeLayout>
特效定义
<?xml version="1.0" encoding="utf-8"?>
<!-- 上下滑入式 -->
<set xmlns:android="http://schemas.android.com/apk/res/android" > <!--<translate
android:duration="200"
android:fromYDelta="100%p"
android:toYDelta="0"
/>-->
<scale
android:interpolator= "@android:anim/decelerate_interpolator"
android:fromXScale="0.0"
android:toXScale="0.5"
android:fromYScale="1.0"
android:pivotX="50%"
android:pivotY="50%"
android:startOffset="0"
android:duration="1000"
android:repeatCount="1"
android:repeatMode="reverse"
/>
</set>