【问题标题】:Get specific true coordinates of the Bitmap inside an ImageView获取 ImageView 中位图的特定真实坐标
【发布时间】:2016-06-08 08:49:40
【问题描述】:

我有一个custom ImageView setBitmap() 到,Bitmaps 都带有dimensions of 800x600

我设置了在视图顶部绘制矩形的触摸事件,稍后我想获取这些矩形坐标并将它们发送回服务器进行更新。

问题在于 event.getXevent.getY 返回相对/可能缩放的坐标(尺寸为 2000*1700 的地方)强>

我希望得到真正的位图坐标。

定义视图的 xml

<com.example.tombushmits.drawtesting.DrawbleImageView
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:id="@+id/imageView"
    android:background="@drawable/border"
    android:scaleType="fitCenter"
    />

在我的custom ImageView onTouch 上:

switch(event.getAction()){
            case MotionEvent.ACTION_DOWN:
                if(rec_list.size()<5) {
                    drawRectangle = true;
                    xTop = event.getX();
                   yTop = event.getY();
                   xBottom = event.getX();
                   yBottom = event.getY();
//                    float x = event.getRawX();
//                    float y = event.getRawY();
//                    truex= (x-location[0])*width_ratio;
//                    truey = (y-location[1])*height_ratio;
//
                   invalidate();


               }
                break;
            case MotionEvent.ACTION_MOVE:
                if(rec_list.size() <5) {
                    xBottom = event.getX();
                    yBottom = event.getY();
                    invalidate();
                }
                break;
            case MotionEvent.ACTION_UP:
//ignore
//                truex_end = (event.getX()-location[0])*width_ratio;
//                truey_end = (event.getY()- location[1])*height_ratio;
                if(rec_list.size() < 5) {
                    drawRectangle = false;

                    rec_list.add(new Rect((int) xTop, (int) yTop, (int) xBottom, (int) yBottom));
                    true_rec_list.add(new Rect((int)truex,(int)truey,(int)truex_end,(int)truey_end));
                    invalidate();

                }
                break;
        }
        return true;
    }
    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        if(clear)
        {
            canvas.drawRect(0,0,0,0,paint);
            rec_list.clear();
            clear =false;
            return;

        }
        for(Rect rect:rec_list)
            canvas.drawRect(rect, paint);
        if(drawRectangle)
            canvas.drawRect(xTop, yTop, xBottom, yBottom, paint);

    }

    public ArrayList<Rect> returnRecs(){
//        return this.rec_list;
        return this.true_rec_list;
    }

非常感谢。

【问题讨论】:

    标签: android android-layout bitmap android-drawable


    【解决方案1】:

    您应该能够通过 onTouch 中的 View.getLocationOnScreen() 方法获取 ImageView 的坐标。这将为您提供左上角的坐标。由于您知道位图的大小,因此您可以计算您可能需要的任何其他坐标。

    @Override
    public boolean onTouch(View v, MotionEvent event) {
        int[] location = new int[2];
        v.getLocationOnScreen(location);
        // location array now has x and y coordinates
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-11-26
      • 1970-01-01
      • 2017-08-25
      • 2020-01-28
      • 1970-01-01
      相关资源
      最近更新 更多