Android 定期更改背景图像图像视图

Android 定期更改背景图像图像视图,android,multithreading,performance,android-layout,imageview,Android,Multithreading,Performance,Android Layout,Imageview,我将布局FrameLayout作为根布局 <FrameLayout 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" > <ImageView

我将布局FrameLayout作为根布局

<FrameLayout 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"
 >

    <ImageView
        android:id="@+id/background_image"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:src="@drawable/init_image"
        android:scaleType="centerCrop">
    </ImageView>

......
......
</FrameLayout>
我需要在无限循环中周期性地改变一些图像,比如幻灯片放映。 我曾尝试使用该处理程序,但当我更改时,会出现内存错误。 我也试过ImageSwitcher,但它似乎不能正常工作。
请给出一个例子或建议,如何按照安卓thx的所有设计模式正确地实现它。

Hye在那里,我最近在搜索这个问题,发现了一个很好的解决方案,因为它不应用基于密度的图像自动缩放并按原样使用它们

1-将所有图像放在res/drawable nodpi文件夹中,您必须创建此文件夹,因为它不存在

2-使它们在无限循环中从一个自动更改为另一个

int images[] = { R.drawable.background_1, 
        R.drawable.background_2,
        R.drawable.background_3, 
        R.drawable.background_4,
        R.drawable.background_5, 
        R.drawable.background_6,
        R.drawable.background_7 }; // i've these images in **res/drawable-nodpi**
下面是onCreate方法

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    imageView = (ImageView) findViewById(R.id.imageView_Animated); 

    handler.postDelayed(changeImage, 5000); // after every 5secs it will change image.

 }
现在来处理所有图像更改的部分,在onCreate之外执行此操作

    Runnable changeImage = new Runnable() {

    @Override
    public void run() {

        if (count >6) {
            handler.removeCallbacks(changeImage);
        } else {

            if (count >= 6)
                count = 0;
            AlphaAnimation animation1 = new AlphaAnimation(0.5f, 1.0f); //here is a bit of animation for ya ;)
            animation1.setDuration(5000);
            animation1.setStartOffset(1300); //time for that color effect
            animation1.setFillAfter(true);
            imageView.startAnimation(animation1);
            imageView.setBackgroundResource(images[count++]);
            handler.postDelayed(changeImage, 5000);
            }
    }
};

全部完成,希望我能帮助您。

您可以使用计时器,在经过一定时间后更改imageview的可绘制性使用view pager,它将在所述时间间隔滑动图像尝试使用AsyncTask加载新图像,并在加载后更改图像时播放一些动画。以时间间隔在处理程序中执行asynctask。