Android 图像在某段时间内自动滑动
我的申请有问题。我想让我的背景图像自动滑动,而不按任何按钮,但使用时间间隔。 我遵循一些文章中的说明,但他们只是使用按钮滑动背景图像。是否有人可以帮助我编辑代码,使图像将在一段时间内自动滑动?谢谢 这是我的密码 Java代码:Android 图像在某段时间内自动滑动,android,image,sliding,Android,Image,Sliding,我的申请有问题。我想让我的背景图像自动滑动,而不按任何按钮,但使用时间间隔。 我遵循一些文章中的说明,但他们只是使用按钮滑动背景图像。是否有人可以帮助我编辑代码,使图像将在一段时间内自动滑动?谢谢 这是我的密码 Java代码: int image[] = {R.drawable.backgroundtwo, R.drawable.backgroundthree, R.drawable.backgroundfour}; ImageSwitcher imageSwitcher; Button bu
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]);
}
}
});
}
您可以在res文件夹下创建动画文件夹 并在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>
很抱歉,我现在才打开看看,谢谢你给我一个解决这个问题的灵感。我非常感激。
<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"
/>