0
我有幾個圖像視圖和幾個圖像添加到佈局,我隨機添加它們,我需要確保兩個圖像視圖不包含相同的圖像...這裏是我的for循環動態添加圖像到圖像視圖。如何查看這樣的圖像不包含相同的圖像
Random random = new Random(System.currentTimeMillis());
for(int v : imageViews) {
ImageView iv = (ImageView)findViewById(v);
iv.setImageResource(images[random.nextInt(images.length-1)]);
}
我發現了一個辦法做到這一點,我會添加editted代碼:
LinearLayout linearLayout1 = (LinearLayout) findViewById(R.id.bottomView);
for(int x=0;x<images.length;x++) {
Bitmap bitmapOrg = BitmapFactory.decodeResource(getResources(),images[x]);
int width = bitmapOrg.getWidth();
int height = bitmapOrg.getHeight();
int newWidth = 200;
int newHeight = 200;
float scaleWidth = ((float) newWidth)/width;
float scaleHeight = ((float) newHeight)/height;
Matrix matrix = new Matrix();
matrix.postScale(scaleWidth, scaleHeight);
matrix.postRotate(0);
Bitmap resizedBitmap = Bitmap.createBitmap(bitmapOrg, 0, 0,
width, height, matrix, true);
BitmapDrawable bmd = new BitmapDrawable(getResources(),resizedBitmap);
ImageView imageView = new ImageView(this);
imageView.setPadding(2, 0, 9, 5);
imageView.setImageDrawable(bmd);
imageView.setTag(x);
imageView.setOnClickListener(new OnClickListener(){
@Override
public void onClick(View v) {
Intent intent = new Intent(Intent.ACTION_VIEW);
}
});
linearLayout1.addView(imageView);
}