【发布时间】:2014-06-28 06:22:25
【问题描述】:
我有几个图像视图和几个图像要添加到布局中,我随机添加它们,我需要确保两个图像视图不包含相同的图像...这是我的 for 循环,用于将图像动态添加到图像视图。
Random random = new Random(System.currentTimeMillis());
for(int v : imageViews) {
ImageView iv = (ImageView)findViewById(v);
iv.setImageResource(images[random.nextInt(images.length-1)]);
}
我找到了一种方法,我将添加编辑后的代码:
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);
}
【问题讨论】:
标签: android for-loop random imageview