1、简介
ImageSwitcher 用来进行图片的 切换预览
2、功能实现
- 文件结构
2)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:gravity="center_horizontal" >
<ImageSwitcher
android:id="@+id/imageswitcher"
android:layout_width="300dip"
android:layout_height="200dip" >
</ImageSwitcher>
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@id/imageswitcher"
android:layout_centerHorizontal="true"
android:orientation="horizontal" >
<Button
android:id="@+id/btn_previous"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="上一张" />
<Button
android:id="@+id/btn_next"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="下一张" />
</LinearLayout>
</RelativeLayout>
3)anim 文件定义的动画
lefe_in.xml
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android">
<translate
android:fromXDelta="-100%p"
android:toXDelta="0"
android:duration="600"
/>
<alpha
android:fromAlpha="0.1"
android:toAlpha="1.0"
android:duration="600"
/>
</set>
left_out.xml
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android" >
<translate
android:duration="600"
android:fromXDelta="0"
android:toXDelta="-100%p" />
<alpha
android:duration="600"
android:fromAlpha="1.0"
android:toAlpha="0.1" />
</set>
reight_in.xml
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android" >
<translate
android:duration="600"
android:fromXDelta="100%p"
android:toXDelta="0" />
<alpha
android:duration="600"
android:fromAlpha="0.1"
android:toAlpha="1.0" />
</set>
right_out.xml
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android">
<translate
android:fromXDelta="0"
android:toXDelta="100%p"
android:duration="600"
/>
<alpha
android:fromAlpha="1.0"
android:toAlpha="0.1"
android:duration="600"
/>
</set>
- mainActivity.java
package myapplication21.lum.com.mytestview;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.animation.AnimationUtils;
import android.widget.Button;
import android.widget.ImageSwitcher;
import android.widget.ImageView;
import android.widget.ViewSwitcher.ViewFactory;
public class MainActivity extends Activity implements OnClickListener,
ViewFactory {
private ImageSwitcher imageSwitcher;
private Button btn_next;
private Button btn_previous;
// 获取图片资源
private int[] images = { R.drawable.image1, R.drawable.image2,
R.drawable.image3, R.drawable.image4, R.drawable.image5 };
private int index;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
imageSwitcher = (ImageSwitcher) findViewById(R.id.imageswitcher);
imageSwitcher.setFactory(this);
btn_next = (Button) findViewById(R.id.btn_next);
btn_previous = (Button) findViewById(R.id.btn_previous);
btn_next.setOnClickListener(this);
btn_previous.setOnClickListener(this);
imageSwitcher.setImageResource(images[0]);
}
@Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.btn_next:
imageSwitcher.setInAnimation(AnimationUtils.loadAnimation(this,
R.anim.right_in));
imageSwitcher.setOutAnimation(AnimationUtils.loadAnimation(this,
R.anim.left_out));
index++;
if (index >= images.length) {
index = images.length - 1;
return;
}
imageSwitcher.setImageResource(images[index]);
break;
case R.id.btn_previous:
imageSwitcher.setInAnimation(AnimationUtils.loadAnimation(this,
R.anim.left_in));
imageSwitcher.setOutAnimation(AnimationUtils.loadAnimation(this,
R.anim.right_out));
index--;
if (index < 0) {
index = 0;
return;
}
imageSwitcher.setImageResource(images[index]);
break;
}
}
@Override
public View makeView() {
ImageView imageView = new ImageView(this);
return imageView;
}
}