[英]Android get Position of Bitmap in ImageView
我正在使用CropView ,它擴展了Android ImageView
。 它允許視圖中的圖像縮放和移動。 似乎沒有任何公開引用圖像位置或比例。 有沒有辦法獲得圖像相對於我可以嘗試的常規ImageView的位置。
我還嘗試在CropView上設置一個觸摸監聽器,它只是打印觸摸的x和y位置,但我不知道如何使用它們來移動獲取圖像的更新位置。
mCropView.setOnTouchListener(new View.OnTouchListener() {
@Override
public boolean onTouch(View v, MotionEvent event) {
Log.d(TAG, "///" + event.getX() + " __ " + event.getY());
return false;
}
});
你可以嘗試下面的方法,
final float[] getPointOfTouchedCordinate(ImageView view, MotionEvent e) {
final int index = e.getActionIndex();
final float[] coords = new float[] { e.getX(index), e.getY(index) };
Matrix m = new Matrix();
view.getImageMatrix().invert(m);
m.postTranslate(view.getScrollX(), view.getScrollY());
m.mapPoints(coords);
return coords;
}
你可以獲得價值
mCropView.setOnTouchListener(new View.OnTouchListener() {
@Override
public boolean onTouch(View v, MotionEvent event) {
float[] points = getPointOfTouchedCordinate(v, event);
Log.d(TAG, "///" + points[0] + " __ " + points(1));
return false;
}
});
我建議依靠純xml並添加到Imagevie:
android:scaleType="centerCrop"
這將導致繪制所需的輸出。
CropView本身就是視口。 Cropview有兩個方法getViewportWidth和getViewportHeight。 你需要它們。
縮放,移動,裁剪等圖像是Drawable。 Drawable有一個getBounds方法,它將返回一個Rect。 您可以從此Rect獲得頂部,左側寬度和高度,但您需要先獲得Drawable。
CropView調用此Drawable“位圖”。 您可以調用CropView的getImageBitmap()方法。
一旦你擁有它,調用getBounds,它給你圖像的Rect。
要獲得你想要的x和y,你必須使用Rect的頂部,左邊,寬度和高度以及視口的高度和寬度進行一些數學運算,你可以從CropView的getViewportHeight和getViewportWidth方法獲得。
只是:
vpwidth=cropview.getViewPortWidth();
vpheight=cropview.getViewPortHeight();
imagetop=cropview.getImageBitmap().getBounds().top;
imageleft=cropview.getImageBitmap().getBounds().left;
imageheight=cropview.getImageBitmap().getBounds().height;
imagewidth=cropview.getImageBitmap().getBounds().width;
>
數學在這里
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.