如何引用随机图像数组中返回的图像?

发布于 2024-10-29 15:40:24 字数 1478 浏览 5 评论 0原文

我正在整理一些代码并尝试使用附加的代码分配壁纸。 随机图像部分有效,将图像分配到壁纸有效,但我不知道如何获得显示为壁纸图像的确切图像。

public class RandomImage extends Activity implements OnClickListener{

    private Integer [] mImageIds = { 
        R.drawable.big1, 
        R.drawable.big2, 
        R.drawable.big3, 
    };

    private static final Random rgenerator = new Random();
    private ImageView iv;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.winner);

        Integer q = mImageIds[rgenerator.nextInt(mImageIds.length)];
        iv = (ImageView) findViewById(R.id.imageView1);
        iv.setImageResource(q);
        iv.setImageResource(mImageIds[rgenerator.nextInt(mImageIds.length)]);

        View setButton = findViewById(R.id.set_wallpaper);
        setButton.setOnClickListener(this);     
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.set_wallpaper:
                WallpaperManager myWallpaperManager = 
                    WallpaperManager.getInstance(getApplicationContext());
                try {     
                    //How to reference the image selected in above random array? 
                myWallpaperManager.setResource(????);   
                } catch (IOException e) { 
                    // TODO Auto-generated catch block     
                    e.printStackTrace();
                }
         }
     }
}

I am putting together some codes and attempting to assign a wallpaper with the attached code.
The random image part works, the assign image to wallpaper works, but I do not see how to get the exact image shown to be the image for the wallpaper.

public class RandomImage extends Activity implements OnClickListener{

    private Integer [] mImageIds = { 
        R.drawable.big1, 
        R.drawable.big2, 
        R.drawable.big3, 
    };

    private static final Random rgenerator = new Random();
    private ImageView iv;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.winner);

        Integer q = mImageIds[rgenerator.nextInt(mImageIds.length)];
        iv = (ImageView) findViewById(R.id.imageView1);
        iv.setImageResource(q);
        iv.setImageResource(mImageIds[rgenerator.nextInt(mImageIds.length)]);

        View setButton = findViewById(R.id.set_wallpaper);
        setButton.setOnClickListener(this);     
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.set_wallpaper:
                WallpaperManager myWallpaperManager = 
                    WallpaperManager.getInstance(getApplicationContext());
                try {     
                    //How to reference the image selected in above random array? 
                myWallpaperManager.setResource(????);   
                } catch (IOException e) { 
                    // TODO Auto-generated catch block     
                    e.printStackTrace();
                }
         }
     }
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

深海少女心 2024-11-05 15:40:24

为什么不声明一个存储 id 的 int 类变量,例如

int lastImageRef;

……

lastImageRef = mImageIds[rgenerator.nextInt(mImageIds.length)];
iv.setImageResource(lastImageRef);

myWallpaperManager.setResource(lastImageRef); 

Why not declaring an int class variable that stocks the id, e.g:

int lastImageRef;

...

lastImageRef = mImageIds[rgenerator.nextInt(mImageIds.length)];
iv.setImageResource(lastImageRef);

...

myWallpaperManager.setResource(lastImageRef); 
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文