我试图将OnTouchListener设置为ImageView,我在其上进行了大量的自定义绘图。问题:我的X值已关闭,并且我不断地将触摸记录到左侧太远。 Y值是可以的,所以算法中的问题在哪里?
我使用的画布宽400像素,高300像素。我没有对ImageView应用任何特殊的缩放,我只是依靠标准的ImageView行为来缩放自身以适应屏幕。用户触摸屏幕时应该出现小绿点,但是在我的Nexus 5上它们离左边太远了。我的数学对Y值是否正确,但X值是否错误?我同时请求x比例和y比例,所以即使ImageView正在改变宽高比,我应该考虑到这一点,对吧?
public class FullscreenActivity extends AppCompatActivity {
private ImageView drawingImageView;
private Canvas canvas;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
int mUIFlag = View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN
| View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION
| View.SYSTEM_UI_FLAG_LAYOUT_STABLE
| View.SYSTEM_UI_FLAG_LOW_PROFILE
| View.SYSTEM_UI_FLAG_FULLSCREEN
| View.SYSTEM_UI_FLAG_HIDE_NAVIGATION
| View.SYSTEM_UI_FLAG_IMMERSIVE_STICKY;
getWindow().getDecorView().setSystemUiVisibility(mUIFlag);
setContentView(R.layout.activity_fullscreen);
drawingImageView = (ImageView) this.findViewById(R.id.game_map_fixed_id);
Bitmap bitmap = Bitmap.createBitmap(Map.width, Map.height, Bitmap.Config.ARGB_8888);
canvas = new Canvas(bitmap);
drawingImageView.setImageBitmap(bitmap);
drawingImageView.setOnTouchListener(mapOnTouchListener);
}
View.OnTouchListener mapOnTouchListener = new View.OnTouchListener() {
@Override
public boolean onTouch(View v, MotionEvent event) {
float[] bounds = new float[6];
bounds = getBitmapPositionInsideImageView(drawingImageView);
float x = event.getX() / bounds[4] - bounds[0];
float y = event.getY() / bounds[5] - bounds[1];
Paint paint = new Paint();
paint.setStyle(Paint.Style.FILL_AND_STROKE);
paint.setColor(Color.GREEN);
canvas.drawCircle(x, y, 1, paint);
return false;
}
};
public static float[] getBitmapPositionInsideImageView(ImageView imageView) {
float[] rect = new float[6];
if (imageView == null || imageView.getDrawable() == null)
return rect;
// Get image dimensions
// Get image matrix values and place them in an array
float[] f = new float[9];
imageView.getImageMatrix().getValues(f);
// Extract the scale values using the constants (if aspect ratio maintained, scaleX == scaleY)
final float scaleX = f[Matrix.MSCALE_X];
final float scaleY = f[Matrix.MSCALE_Y];
rect[4] = scaleX;
rect[5] = scaleY;
// Get the drawable (could also get the bitmap behind the drawable and getWidth/getHeight)
final Drawable d = imageView.getDrawable();
final int origW = d.getIntrinsicWidth();
final int origH = d.getIntrinsicHeight();
// Calculate the actual dimensions
final int actW = Math.round(origW * scaleX);
final int actH = Math.round(origH * scaleY);
rect[2] = actW;
rect[3] = actH;
// Get image position
// We assume that the image is centered into ImageView
int imgViewW = imageView.getWidth();
int imgViewH = imageView.getHeight();
float left = (imgViewW - actW)/2;
float top = (imgViewH - actH)/2;
rect[0] = left;
rect[1] = top;
return rect;
}
}
答案 0 :(得分:0)
错误在于从getBitmapPositionInsideImageView接收信息后计算点数。
float x = event.getX() / bounds[4] - bounds[0];
float y = event.getY() / bounds[5] - bounds[1];
应该是
float x = (event.getX() - bounds[0]) / bounds[4];
float y = (event.getY() - bounds[1]) / bounds[5];
我在垂直轴上没有任何问题的原因是因为图像在顶部或底部没有接收任何填充,这意味着边界[5]始终为零,隐藏了错误在那个等式中。