简体   繁体   English

如何在ImageView中的图像位图上绘制一个矩形

[英]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 如果您没有看到矩形,请尝试更改drawRectdrawBitmap的顺序

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.

相关问题 如何在位图图像上绘制矩形,使图像居中? - How to Draw Rectangle on Bitmap image, keeping image in the centre? Android - 在上传的 bitmap 图像上绘制矩形 - Android - draw rectangle on uploaded bitmap image 如何用手指在ImageView位图上绘制并获取坐标并保存绘制的图像 - How to draw onto an ImageView Bitmap with finger and obtain coordinates and save the drawn image 如何在图像位图上绘制标记? - How to draw a Tag on Image Bitmap? 如何确定imageview中位图上绘制的矩形的宽度和高度 - how to determine width and height of rectangle drawn on bitmap in a imageview Android:在ImageView(或任何其他位图兼容的小部件)上绘制可移动且可调整大小的矩形并裁剪选定的区域 - Android: Draw a moveable and sizeable rectangle on ImageView(Or any other bitmap compatible widget) and crop the selected region 无法在imageview上绘制矩形 - Unable to draw rectangle over imageview 如何在imageViews数组中的每个imageView周围绘制一个矩形? - How can I draw a rectangle around each imageView in an array of imageViews? 如何在Imageview上绘制一个黑色边框和背景透明的矩形? - How to draw a rectangle with black border and background transparent on Imageview? 如何将imageView中的背景图像存储在位图中? - How to store background image from imageView in bitmap?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM