我的申请有问题。我想让我的背景图像自动滑动而不按任何按钮但使用时间间隔。 我按照一些文章的一些说明,但他们只是使用按钮滑动背景图像。有人可以帮我编辑代码,以便图像在一段时间内自动滑动吗?感谢
这是我的代码。
Java代码:
int image[] = {R.drawable.backgroundtwo, R.drawable.backgroundthree, R.drawable.backgroundfour};
ImageSwitcher imageSwitcher;
Button button;
Animation slide_in_left, slide_out_right;
int curIndex;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_homepage);
imageSwitcher = (ImageSwitcher) findViewById(R.id.imageSwitcher);
button = (Button) findViewById(R.id.button);
slide_in_left = AnimationUtils.loadAnimation(this, android.R.anim.slide_in_left);
slide_out_right = AnimationUtils.loadAnimation(this, android.R.anim.slide_out_right);
imageSwitcher.setInAnimation(slide_in_left);
imageSwitcher.setOutAnimation(slide_out_right);
imageSwitcher.setFactory(new ViewSwitcher.ViewFactory() {
@Override
public View makeView() {
ImageView imageView = new ImageView(Homepage.this);
imageView.setScaleType(ImageView.ScaleType.FIT_CENTER);
ViewGroup.LayoutParams params = new ImageSwitcher.LayoutParams(
ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT
);
imageView.setLayoutParams(params);
return imageView;
}
});
curIndex = 0;
imageSwitcher.setImageResource(image[curIndex]);
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if(curIndex == image.length - 1){
curIndex = 0;
imageSwitcher.setImageResource(image[curIndex]);
} else {
imageSwitcher.setImageResource(image[++curIndex]);
}
}
});
}
答案 0 :(得分:0)
您在res文件夹下创建anim文件夹 并在anim文件夹下创建一个imgae_animation.xml文件 写在imgae_animation.xml
<?xml version="1.0" encoding="utf-8"?>
<animation-list xmlns:android="http://schemas.android.com/apk/res/android"
android:oneshot="false" >
<item
android:drawable="@drawable/img1"
android:duration="120"/>
<item
android:drawable="@drawable/img2"
android:duration="120"/>
<item
android:drawable="@drawable/img3"
android:duration="120"/>
<item
</animation-list>
然后将.xml下的此动画添加到imageview
<ImageView
android:id="@+id/img_home_list"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="5dp"
android:background="@null"
android:scaleType="fitXY"
android:adjustViewBounds="true"
android:contentDescription="@null"
android:src="@anim/imgae_animation"
/>