效果图:
![这里写图片描述](https://img-blog.csdn.net/20150803152638810)
布局代码:
<?xml version="1.0" encoding="UTF-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<ImageSwitcher
android:id="@+id/imageSwitcher"
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/ll_view"
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>
</FrameLayout>
页面代码:
public class ImageSwitcherActivity extends Activity implements ViewSwitcher.ViewFactory, View.OnTouchListener {
private int[] imgIds;
private int currentPosition;
private ImageSwitcher mImageSwitcher;
private float downX;
private ImageView[] tips;
private LinearLayout linearLayout;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_switcher);
imgIds = new int[]{R.drawable.bg, R.drawable.c2, R.drawable.c3, R.drawable.c4, R.drawable.c5, R.drawable.c6, R.drawable.c7, R.drawable.c8, R.drawable.c9};
mImageSwitcher = (ImageSwitcher) findViewById(R.id.imageSwitcher);
mImageSwitcher.setFactory(this);
mImageSwitcher.setOnTouchListener(this);
linearLayout = (LinearLayout) findViewById(R.id.ll_view);
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 = 3;
layoutParams.leftMargin = 3;
mImageView.setBackgroundResource(R.drawable.page_indicator_unfocused);
linearLayout.addView(mImageView, layoutParams);
}
currentPosition = getIntent().getIntExtra("position", 0);
mImageSwitcher.setImageResource(imgIds[currentPosition]);
setImageBackground(currentPosition);
}
private void setImageBackground(int selectItems) {
for (int i = 0; i < tips.length; i++) {
if (i == selectItems) {
tips[i].setBackgroundResource(R.drawable.page_indicator_focused);
} else {
tips[i].setBackgroundResource(R.drawable.page_indicator_unfocused);
}
}
}
@Override
public View makeView() {
final ImageView i = new ImageView(this);
i.setBackgroundColor(0xff000000);
i.setScaleType(ImageView.ScaleType.CENTER_CROP);
i.setLayoutParams(new ImageSwitcher.LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT));
return i;
}
@Override
public boolean onTouch(View v, MotionEvent event) {
switch (event.getAction()) {
case MotionEvent.ACTION_DOWN: {
downX = event.getX();
break;
}
case MotionEvent.ACTION_UP: {
float lastX = event.getX();
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.lift_out));
currentPosition++;
mImageSwitcher.setImageResource(imgIds[currentPosition]);
setImageBackground(currentPosition);
} else {
Toast.makeText(getApplication(), "到了最后一张", Toast.LENGTH_SHORT).show();
}
}
}
break;
}
return true;
}
}