如何确定imageview中位图上绘制的矩形的宽度和高度

时间:2017-04-25 07:23:37

标签: android canvas bitmap imageview

这是我用来获取x,y坐标的代码。我还需要绘制矩形的宽度和高度。 我从网站链接中获取此代码 - http://android-er.blogspot.in/2013/09/detect-touch-and-draw-rect-on-bitmap.html 请提供解决方案

enter image description here

 public class MainActivity extends Activity {

Button btnLoadImage;
TextView textSource;
ImageView imageResult, imageDrawingPane;

final int RQS_IMAGE1 = 1;

Uri source;
Bitmap bitmapMaster;
Canvas canvasMaster;
Bitmap bitmapDrawingPane;
Canvas canvasDrawingPane;
projectPt startPt;
projectPt endpt;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    btnLoadImage = (Button)findViewById(R.id.loadimage);
    textSource = (TextView)findViewById(R.id.sourceuri);
    imageResult = (ImageView)findViewById(R.id.result);
    imageDrawingPane = (ImageView)findViewById(R.id.drawingpane);


    btnLoadImage.setOnClickListener(new OnClickListener(){

        @Override
        public void onClick(View arg0) {
            Intent intent = new Intent(Intent.ACTION_PICK,
                    android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
            startActivityForResult(intent, RQS_IMAGE1);
        }});

    imageResult.setOnTouchListener(new OnTouchListener(){

        @Override
        public boolean onTouch(View v, MotionEvent event) {

            int action = event.getAction();
            int x = (int) event.getX();
            int y = (int) event.getY();
            switch(action){
                case MotionEvent.ACTION_DOWN:
                    textSource.setText("ACTION_DOWN- " + x + " : " + y);
                    startPt = projectXY((ImageView)v, bitmapMaster, x, y);
                    break;
                case MotionEvent.ACTION_MOVE:
                    textSource.setText("ACTION_MOVE- " + x + " : " + y);
                     drawOnRectProjectedBitMap((ImageView)v, bitmapMaster, x, y);

                    break;
                case MotionEvent.ACTION_UP:
                    textSource.setText("ACTION_UP- " + x + " : " + y);
                    drawOnRectProjectedBitMap((ImageView)v, bitmapMaster, x, y);

                    finalizeDrawing();
                    break;
            }

            return true;
        }});

}

class projectPt{
    int x;
    int y;

    projectPt(int tx, int ty){
        x = tx;
        y = ty;
    }
}

private projectPt projectXY(ImageView iv, Bitmap bm, int x, int y){
    if(x<0 || y<0 || x > iv.getWidth() || y > iv.getHeight()){
        //outside ImageView
        return null;
    }else{
        int projectedX = (int)((double)x * ((double)bm.getWidth()/(double)iv.getWidth()));
        int projectedY = (int)((double)y * ((double)bm.getHeight()/(double)iv.getHeight()));

        return new projectPt(projectedX, projectedY);
    }
}

private void drawOnRectProjectedBitMap(ImageView iv, Bitmap bm, int x, int y){

    if(x<0 || y<0 || x > iv.getWidth() || y > iv.getHeight()){
        //outside ImageView
        return;
    }else{
        int projectedX = (int)((double)x * ((double)bm.getWidth()/(double)iv.getWidth()));
        int projectedY = (int)((double)y * ((double)bm.getHeight()/(double)iv.getHeight()));

        //clear canvasDrawingPane
        canvasDrawingPane.drawColor(Color.TRANSPARENT, Mode.CLEAR);

        Paint paint = new Paint();
        paint.setStyle(Paint.Style.STROKE);
        paint.setColor(Color.WHITE);
        paint.setStrokeWidth(4);
        canvasDrawingPane.drawRect(startPt.x, startPt.y, projectedX, projectedY, paint);
        imageDrawingPane.invalidate();
       // textSource.setText(x + ":" + y + "/" + iv.getWidth() + " : " + iv.getHeight() + "\n" +
        //                projectedX + " : " + projectedY + "/" + bm.getWidth() + " : " + bm.getHeight()

        textSource.setText(startPt.x + ":" + startPt.y + "/" + iv.getWidth() + " : " + iv.getHeight() + "\n" +
                projectedX + " : " + projectedY+ "/" + bm.getWidth() + " : " + bm.getHeight()

        );
    }
}

private void finalizeDrawing(){
    canvasMaster.drawBitmap(bitmapDrawingPane, 0, 0, null);
}

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    super.onActivityResult(requestCode, resultCode, data);

    Bitmap tempBitmap;


    if(resultCode == RESULT_OK){
        switch (requestCode){
            case RQS_IMAGE1:
                source = data.getData();
                textSource.setText(source.toString());

                try {

                    tempBitmap = BitmapFactory.decodeStream(
                            getContentResolver().openInputStream(source));

                    Config config;
                    if(tempBitmap.getConfig() != null){
                        config = tempBitmap.getConfig();
                    }else{
                        config = Config.ARGB_8888;
                    }


                    bitmapMaster = Bitmap.createBitmap(
                            tempBitmap.getWidth(),
                            tempBitmap.getHeight(),
                            config);

                    canvasMaster = new Canvas(bitmapMaster);
                    canvasMaster.drawBitmap(tempBitmap, 0, 0, null);

                    imageResult.setImageBitmap(bitmapMaster);



                    bitmapDrawingPane = Bitmap.createBitmap(
                            tempBitmap.getWidth(),
                            tempBitmap.getHeight(),
                            config);
                    canvasDrawingPane = new Canvas(bitmapDrawingPane);
                    imageDrawingPane.setImageBitmap(bitmapDrawingPane);



                }
                catch (FileNotFoundException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }

                break;
        }

    }
}
}

2 个答案:

答案 0 :(得分:0)

此处int projectedX和int int projectedY包含绘制的矩形的宽度和高度。

您可以在课堂的任何地方进行全局访问和访问。

喜欢这个..

//rectangle width and height
int projectedX;
int projectedY;
//end
Button btnLoadImage;
TextView textSource;
ImageView imageResult, imageDrawingPane;

现在访问projectedX&amp; projectionY可以在任何地方获得宽度和高度。

答案 1 :(得分:0)

在绘制之前

Rect rect = new Rect(startPt.x, startPt.y, projectedX, projectedY);

canvasDrawingPane.drawRect(rect,paint);

width = rect.width();

height = rect.height();

imageDrawingPane.invalidate();

这是你的答案。