[英]How to draw a rectangle on Image Bitmap in ImageView
I am new Android programming. 我是新的Android编程。 I am trying to open an image from the Gallery of a phone and draw a Rectangle on top of the image I have opened.
我试图从手机的图库中打开图像,并在我打开的图像上绘制一个矩形。 But I am able to open the image but I am not able to see the rectangle.
但我能够打开图像,但我无法看到矩形。 I am using ImageView and Canvas to open image and draw.
我正在使用ImageView和Canvas来打开图像和绘图。 I have created a Button and used to image from Gallery.
我创建了一个Button,用于从Gallery中进行图像处理。 I have written the code to open and draw in onActiviesult() method.Could somebody help me?!.
我已经编写了代码来打开并绘制onActiviesult()方法。有人可以帮助我吗?! Thanks in advance
提前致谢
public class MainActivity extends ActionBarActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button gal=(Button)findViewById(R.id.button1);
gal.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);
}
});
}
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);
//img.setImageBitmap(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]);
String picPath=cur.getString(colIndex);
cur.close();
cnvs.drawRect(20, 20,50,50 , paint);
img.setImageBitmap(bmp);
img.setImageBitmap(BitmapFactory.decodeFile(picPath));
}
}
return super.onOptionsItemSelected(item);
}
}
try drawing the bitmap on the canvas and then call setImageBitmap
just once. 尝试在画布上绘制位图,然后只调用一次
setImageBitmap
。 In this moment you are overriding the content of the ImageView
. 在这一刻,您将覆盖
ImageView
的内容。 Remove img.setImageBitmap(BitmapFactory.decodeFile(picPath));
删除
img.setImageBitmap(BitmapFactory.decodeFile(picPath));
and 和
cnvs.drawBitmap(BitmapFactory.decodeFile(picPath), 0, 0, null);
cnvs.drawRect(20, 20,50,50 , paint);
img.setImageBitmap(bmp);
I am not sure abaut the drawing order. 我不确定绘图顺序。 If you don't see the rectangle try changing the order of
drawRect
and drawBitmap
如果您没有看到矩形,请尝试更改
drawRect
和drawBitmap
的顺序
You may try like this way which i use: create customborder.xml file. 您可以尝试使用以下方式:创建customborder.xml文件。
Just take imageview in layout and make border xml file 只需在布局中获取imageview并制作边框xml文件
and now use this customborder in layout backgourd like: 现在在布局backgourd中使用此自定义边框,如:
android:layout_height="fill_parent"
android:background="@drawable/customborder">
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.