
下面给出代码:
点击(此处)折叠或打开
-
package cn.com.xiebiao.test;
-
-
import android.app.Activity;
-
import android.os.Bundle;
-
import android.view.MotionEvent;
-
import android.view.View;
-
import android.view.View.OnTouchListener;
-
import android.view.ViewGroup;
-
import android.view.animation.AnimationUtils;
-
import android.widget.ImageSwitcher;
-
import android.widget.ImageView;
-
import android.widget.LinearLayout;
-
import android.widget.RelativeLayout.LayoutParams;
-
import android.widget.Toast;
-
import android.widget.ViewSwitcher.ViewFactory;
-
-
public class MainActivity extends Activity implements ViewFactory, OnTouchListener{
-
//ImagaSwitcher 的引用
-
private ImageSwitcher mImageSwitcher;
-
//图片id数组
-
private int[] imgIds;
-
// 当前选中的图片id序号
-
private int currentPosition;
-
//按下点的X坐标
-
private float downX;
-
// 装载当前状态点的线性布局
-
private LinearLayout linearLayout;
-
// 当前状态点数组
-
private ImageView[] tips;
-
-
@Override
-
protected void onCreate(Bundle savedInstanceState) {
-
super.onCreate(savedInstanceState);
-
setContentView(R.layout.activity_main);
-
-
//图片数组,可以在SD卡中读取
-
imgIds = new int[]{R.drawable.p1,R.drawable.p2,R.drawable.p3,R.drawable.p4};
-
//实例化ImageSwitcher
-
mImageSwitcher = (ImageSwitcher)findViewById(R.id.imageSwitcher1);
-
-
/*****************设置Factory***********************/
-
mImageSwitcher.setFactory(this);
-
-
//设置OnTouchListener,通过Touch事件来切换图片
-
mImageSwitcher.setOnTouchListener(this);
-
-
//当前状态点的显示布局
-
linearLayout = (LinearLayout) findViewById(R.id.viewGroup);
-
-
tips = new ImageView[imgIds.length];
-
for(int i=0; i<imgIds.length; i++){
-
ImageView mImageView = new ImageView(this);
-
tips[i] = mImageView;
-
LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(new ViewGroup.LayoutParams(LayoutParams.WRAP_CONTENT,
-
LayoutParams.WRAP_CONTENT));
-
layoutParams.rightMargin = 10;
-
layoutParams.leftMargin = 10;
-
-
mImageView.setBackgroundResource(R.drawable.tip1);
-
linearLayout.addView(mImageView, layoutParams);
-
}
-
-
//默认的图片为第一张
-
currentPosition =0;
-
mImageSwitcher.setImageResource(imgIds[currentPosition]);
-
setImageBackground(currentPosition);
-
}
-
-
/**
-
* 设置选中的tip的背景
-
* @param selectItems
-
* tip1为未被选中的背景,灰色圆
-
* tip2为被选中的背景,红色圆
-
*/
-
private void setImageBackground(int selectItems){
-
for(int i=0; i<tips.length; i++){
-
if(i == selectItems){
-
tips[i].setBackgroundResource(R.drawable.tip2);
-
}else{
-
tips[i].setBackgroundResource(R.drawable.tip1);
-
}
-
}
-
}
-
-
/*ViewFactory接口
-
要将图片显示在ImageSwitcher控件中,必须为ImageSwitcher类设置一个ViewFactory,用来将显示的图片和父窗口区分开来。这可以通过如下方法来实现:
-
mImageSwitcher.setFactory();
-
此外,我们还需要实现ViewSwitcher.ViewFactory接口中的makeView()抽象方法,通过该方法可以返回一个ImageView对象,所有图片都将通过该ImageView对象来进行显示。*/
-
@Override
-
public View makeView() {
-
final ImageView imageView = new ImageView(MainActivity.this);
-
//setScaleType设置了图片的显示方式
-
imageView.setScaleType(ImageView.ScaleType.CENTER_CROP);
-
return imageView ;
-
}
-
-
@Override
-
public boolean onTouch(View v, 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(currentPosition > 0){
-
//设置动画
-
mImageSwitcher.setInAnimation(AnimationUtils.loadAnimation(getApplication(), R.anim.left_in));
-
mImageSwitcher.setOutAnimation(AnimationUtils.loadAnimation(getApplication(), R.anim.right_out));
-
currentPosition --;
-
mImageSwitcher.setImageResource(imgIds[currentPosition % imgIds.length]);
-
setImageBackground(currentPosition);
-
}else{
-
Toast.makeText(getApplication(), "已经是第一张", Toast.LENGTH_SHORT).show();
-
}
-
}
-
-
if(lastX < downX){
-
if(currentPosition < imgIds.length - 1){
-
mImageSwitcher.setInAnimation(AnimationUtils.loadAnimation(getApplication(), R.anim.right_in));
-
mImageSwitcher.setOutAnimation(AnimationUtils.loadAnimation(getApplication(), R.anim.left_out));
-
currentPosition ++ ;
-
mImageSwitcher.setImageResource(imgIds[currentPosition]);
-
setImageBackground(currentPosition);
-
}else{
-
Toast.makeText(getApplication(), "已经是最后一张", Toast.LENGTH_SHORT).show();
-
}
-
}
-
}
-
-
break;
-
}
-
return true;
- }
activity_main.xnl
-
<?xml version="1.0" encoding="UTF-8"?>
-
<FrameLayout xmlns:android=""
-
android:layout_width="fill_parent"
-
android:layout_height="fill_parent" >
-
<ImageSwitcher
-
android:id="@+id/imageSwitcher1"
-
android:layout_width="fill_parent"
-
android:layout_height="fill_parent">
-
</ImageSwitcher>
-
-
<RelativeLayout
-
android:layout_width="fill_parent"
-
android:layout_height="wrap_content"
-
android:orientation="vertical" >
-
<LinearLayout
-
android:id="@+id/viewGroup"
-
android:layout_width="fill_parent"
-
android:layout_height="wrap_content"
-
android:layout_alignParentBottom="true"
-
android:layout_marginBottom="30dp"
-
android:gravity="center_horizontal"
-
android:orientation="horizontal" >
-
</LinearLayout>
- </RelativeLayout>
left_in.xml
-
<?xml version="1.0" encoding="UTF-8"?>
-
<set xmlns:android="">
-
<translate
-
android:fromXDelta="-100%"
-
android:toXDelta="0"
-
android:duration="500"/>
- </set>
tip1.xml
-
<shape xmlns:android=""
-
android:shape="oval">
-
<solid android:color="#8d9a95"/>
-
<size android:width="15dp"
-
android:height="15dp"/>
- </shape>
其它几个xml差不多,就不贴出来了