如何引用随机图像数组中返回的图像?
我正在整理一些代码并尝试使用附加的代码分配壁纸。 随机图像部分有效,将图像分配到壁纸有效,但我不知道如何获得显示为壁纸图像的确切图像。
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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
为什么不声明一个存储 id 的 int 类变量,例如
……
:
Why not declaring an int class variable that stocks the id, e.g:
...
...