找到位图的像素,与Imageview大小和scaletype无关

时间:2014-05-07 07:40:29

标签: android bitmap imageview

我想将位图的像素映射到Imageview x,y坐标,而不管Imageview大小和Scaletype。例如,当用户在图像的鼻子上的第一个图像视图中触摸时,如果在3个具有不同尺寸和比例类型的图像视图中显示相同的图像,那么我想在剩余的两个图像上添加鼻子上的按钮。

到目前为止,我已设法检测用户触摸的像素

imageView1.setOnTouchListener(new OnTouchListener() {

    @Override
    public boolean onTouch(View view, MotionEvent event) {
        float eventX = event.getX();
        float eventY = event.getY();
        float[] eventXY = new float[] { eventX, eventY };

        Matrix invertMatrix = new Matrix();
        ((ImageView) view).getImageMatrix().invert(invertMatrix);

        invertMatrix.mapPoints(eventXY);
        int pixelX = Integer.valueOf((int) eventXY[0]);
        int pixelY = Integer.valueOf((int) eventXY[1]);
        return false;
    }
});

现在......这就是我将pixelX,pixelY转换为imageview坐标的方法,如果Imageviews与原始图像的比例相同,这个工作正常....

private void addButtonInLayout2(RelativeLayout mRelativeLayout, float pixelX, float pixelY, ImageView mImageView) {
mImageView.setBackgroundColor(Color.GREEN);

    final int imageviewWidth = mImageView.getWidth();
    final int imageviewHeight = mImageView.getHeight();
    Drawable imgDrawable = mImageView.getDrawable();
    Bitmap mBitmap = ((BitmapDrawable) imgDrawable).getBitmap();
    int buttonX = (int) ((pixelX * imageviewWidth) / (mBitmap.getWidth()));
    int buttonY = (int) ((pixelY * imageviewHeight) / (mBitmap.getHeight()));
    System.out.println("buttonX  ===========   " + buttonX);
    System.out.println("buttonY  ===========   " + buttonY);

    RelativeLayout.LayoutParams mParams = new RelativeLayout.LayoutParams(50, 50);
    mParams.setMargins(buttonX, buttonY, 0, 0);
    mRelativeLayout.removeView(yellowButton);
    mRelativeLayout.addView(yellowButton, mParams);
    mRelativeLayout.invalidate();
}

1 个答案:

答案 0 :(得分:1)

试试这个:

LinearLayout ll = new LinearLayout(this);
ll.setOrientation(LinearLayout.VERTICAL);

final ImageView iv0 = new ImageView(this);
iv0.setImageResource(R.drawable.layer0);
ll.addView(iv0, new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));

final FrameLayout fl = new FrameLayout(this);
final ImageView iv1 = new ImageView(this);
iv1.setImageResource(R.drawable.layer0);
fl.addView(iv1);
ll.addView(fl, new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT));
setContentView(ll);
OnTouchListener l = new OnTouchListener() {
    @Override
    public boolean onTouch(View v, MotionEvent event) {
        Matrix m = iv0.getImageMatrix();
        Matrix inverse = new Matrix();
        m.invert(inverse);
        float[] pts = { event.getX(), event.getY() };
        inverse.mapPoints(pts);

        // get the coordinates for other ImageView
        m = iv1.getImageMatrix();
        m.mapPoints(pts);

        // add the Button
        Button b = new Button(fl.getContext(), null, android.R.attr.buttonStyleSmall);
        int left = (int) pts[0];
        int top = (int) pts[1];
        b.setText("pos [" + left + "," + top + "]");
        FrameLayout.LayoutParams p = new FrameLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT, Gravity.NO_GRAVITY);
        p.setMargins(left, top, 0, 0);
        fl.addView(b, p);
        return false;
    }
};
iv0.setOnTouchListener(l);