当前位置: 动力学知识库 > 问答 > 编程问答 >

android - How to generate Random rectangle on ImageView

问题描述:

I want to generate Rectangle on same size but at random positions on an ImageView. When I click the button the position of the rectangle must change

I have attached the entire Activity. First, I open an image from the gallery and draw a rectangle on it. When the shuffle button is clicked, it has to move

public class Register extends Activity {

ImageView img;

String picpath;

Bitmap bmp;

Canvas cnvs;

protected void onCreate(Bundle savedInstanceState)

{

super.onCreate(savedInstanceState);

setContentView(R.layout.register);

img=(ImageView)findViewById(R.id.imageView1);

//Open Button

Button open=(Button)findViewById(R.id.button3);

open.setOnClickListener(new View.OnClickListener() {

@Override

public void onClick(View v) {

// TODO Auto-generated method stub

Intent gal_open=new Intent(Intent.ACTION_PICK,android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);

startActivityForResult(gal_open,1);

}

});

Button shuffle=(Button)findViewById(R.id.button2);

shuffle.setOnClickListener(new View.OnClickListener() {

@Override

public void onClick(View v) {

// TODO Auto-generated method stub

int height=img.getHeight();

int width=img.getWidth();

int x;

int y;

Random r=new Random();

x=r.nextInt(width)*width;

y=r.nextInt(height)*height;

Paint paint=new Paint();

cnvs.drawBitmap(BitmapFactory.decodeFile(picpath), 0, 0, null);

cnvs.drawRect(x, y,x+50,y+50 , paint);

img.setImageBitmap(bmp);

}

});

}

public void onActivityResult(int requestCode,int resultCode,Intent intentData)

{

super.onActivityResult(requestCode, resultCode, intentData);

if(requestCode==1 && resultCode==RESULT_OK && intentData!=null)

{

//ImageView img=(ImageView)findViewById(R.id.imageView1 );

Bitmap bmp=Bitmap.createBitmap(img.getHeight(),img.getWidth(),Bitmap.Config.RGB_565);

Canvas cnvs=new Canvas(bmp);

Paint paint=new Paint();

paint.setColor(Color.RED);

Uri data=intentData.getData();

String[] filePath={MediaStore.Images.Media.DATA};

Cursor cur=getContentResolver().query(data,filePath,null,null,null );

cur.moveToFirst();

int colIndex=cur.getColumnIndex(filePath[0]);

picpath=cur.getString(colIndex);

cur.close();

cnvs.drawBitmap(BitmapFactory.decodeFile(picpath), 0, 0, null);

cnvs.drawRect(20, 20,50,50 , paint);

img.setImageBitmap(bmp);

//cnvs.drawRect(20, 20,50,50 , paint);

}

}

}

网友答案:

Please consider this:

//drawRect (float left, float top, float right, float bottom, Paint paint)

in your case it would never change the position as its starting point would always be same you need to change that also. I think this is the case you are getting wrong values in X,and Y . Also try to give the range to the Random generator like 1 to 60

You can simply change the values of X and Y like x=r.nextInt(width); y=r.nextInt(height); but make it sure it should not go beyond the bounds of bitmap

网友答案:

x=r.nextInt(width) will already give you 0 <= x < width, so you shouldn't multiply it again by width.

Same for y and height.

Also, where is your cnvs coming from ? I'm not sure you can just draw on your ImageView in the onClick. I would have made a custom ImageView which remembers where the rectangle is supposed to be and draw it in its onDraw method, then during the onClick, method I would change the variables that store the rectangle position and force a redraw (so onDraw will draw the rectangle at the new position).

网友答案:

Create Random r as a class member and initialize it in the onCreate() method and not as Random r=new Random() inside the onClick()

分享给朋友:
您可能感兴趣的文章:
随机阅读: