我在drawable文件夹中有7张图片。我想在屏幕上随机显示它们。我是处理图形的新手。现在它只在屏幕上显示一个图像。如何在屏幕上显示随机图像?感谢
public class MyGraphics extends SurfaceView implements Runnable {
SurfaceHolder ourHolder;
Thread ourThread = null;
Boolean isRunning = true;
int[] images = new int[]{R.drawable.img1, R.drawable.img2, R.drawable.img3, R.drawable.img4, R.drawable.img5, R.drawable.img6,R.drawable.img7};
public void run() {
while(isRunning){
if(!ourHolder.getSurface().isValid())
continue;
Canvas ourCanvas = ourHolder.lockCanvas();
ourCanvas.drawRGB(0, 0, 0);
Bitmap img_blue = BitmapFactory.decodeResource(getResources(), R.drawable.mg1);
ourCanvas.drawBitmap(img_blue, 0, 0, null);
ourHolder.unlockCanvasAndPost(ourCanvas);
}
}
答案 0 :(得分:1)
生成随机数0-6并从int数组中获取图像源。
Random rn = new Random();
int location=rn.rn.nextInt(7);
Bitmap img_blue = BitmapFactory.decodeResource(getResources(), images[location]);