如何在 Android 中使用 canvas 在其他图像中添加图像?
How to add an image inside other image using canvas in Android?
我正在尝试使用触发触摸事件的 canvas 在其他图像中添加图像。蓝图在 DialogFragment 内部并由 canvas 加载,我的代码有效但坐标错误,我不知道为什么。
正在加载里面的蓝图canvas:
Bitmap bitmap = BitmapFactory.decodeResource(this
.getResources(), R.mipmap.blueprint_test);
bmOverlay = Bitmap.createBitmap(bitmap.getWidth(),
bitmap.getHeight(), Bitmap.Config.ARGB_8888);
canvas = new Canvas(bmOverlay);
//canvas.drawARGB(0x00, 0, 0, 0);
canvas.drawBitmap(bitmap, 0, 0, null);
BitmapDrawable dr = new BitmapDrawable(getResources(),bmOverlay);
dr.setBounds(0, 0, dr.getBounds().width(), dr.getBounds().height());
blueprint.setImageDrawable(dr);
blueprint.setOnTouchListener(this);
触摸监听器
@Override
public boolean onTouch(View view, MotionEvent motionEvent) {
int touchX = (int) motionEvent.getX();
int touchY = (int) motionEvent.getY();
switch (motionEvent.getAction()){
case MotionEvent.ACTION_DOWN:
Log.d(TAG,"Touch coordinates : " +
touchX + "x" + touchY);
canvas.drawBitmap(getMarkerBitmap(),touchX,touchY,null);
BitmapDrawable dr = new BitmapDrawable(getResources(),bmOverlay);
dr.setBounds(0, 0, dr.getIntrinsicWidth(), dr.getIntrinsicHeight());
blueprint.setImageDrawable(dr);
break;
case MotionEvent.ACTION_MOVE:
break;
}
return true;
}
这是结果:
绿色标记是触发触摸事件的地方,标记出现在其他地方
我将 motionEvent.getX() 和 motionEvent.getY() 的相同坐标与 canvas.drawBitmap 函数相加,然后减去标记的高度和宽度。此外,当使用 MotionEvent.ACTION_UP 设置位图时,也解决了滚动问题。这是触摸监听器的最终代码:
@Override
public boolean onTouch(View view, MotionEvent motionEvent) {
int touchX = (int) motionEvent.getX();
int touchY = (int) motionEvent.getY();
switch (motionEvent.getAction()){
case MotionEvent.ACTION_DOWN:
break;
case MotionEvent.ACTION_UP:
canvas.drawColor(Color.TRANSPARENT, PorterDuff.Mode.CLEAR);
canvas.drawBitmap(bitmapBluePrint, 0, 0, null);
canvas.drawBitmap(getMarkerBitmap(),touchX+touchX,touchY+touchY-120,null);
blueprint.invalidate();
break;
case MotionEvent.ACTION_MOVE:
break;
}
return true;
}
我在不同尺寸的设备上尝试过,效果很好。
我正在尝试使用触发触摸事件的 canvas 在其他图像中添加图像。蓝图在 DialogFragment 内部并由 canvas 加载,我的代码有效但坐标错误,我不知道为什么。
正在加载里面的蓝图canvas:
Bitmap bitmap = BitmapFactory.decodeResource(this
.getResources(), R.mipmap.blueprint_test);
bmOverlay = Bitmap.createBitmap(bitmap.getWidth(),
bitmap.getHeight(), Bitmap.Config.ARGB_8888);
canvas = new Canvas(bmOverlay);
//canvas.drawARGB(0x00, 0, 0, 0);
canvas.drawBitmap(bitmap, 0, 0, null);
BitmapDrawable dr = new BitmapDrawable(getResources(),bmOverlay);
dr.setBounds(0, 0, dr.getBounds().width(), dr.getBounds().height());
blueprint.setImageDrawable(dr);
blueprint.setOnTouchListener(this);
触摸监听器
@Override
public boolean onTouch(View view, MotionEvent motionEvent) {
int touchX = (int) motionEvent.getX();
int touchY = (int) motionEvent.getY();
switch (motionEvent.getAction()){
case MotionEvent.ACTION_DOWN:
Log.d(TAG,"Touch coordinates : " +
touchX + "x" + touchY);
canvas.drawBitmap(getMarkerBitmap(),touchX,touchY,null);
BitmapDrawable dr = new BitmapDrawable(getResources(),bmOverlay);
dr.setBounds(0, 0, dr.getIntrinsicWidth(), dr.getIntrinsicHeight());
blueprint.setImageDrawable(dr);
break;
case MotionEvent.ACTION_MOVE:
break;
}
return true;
}
这是结果:
绿色标记是触发触摸事件的地方,标记出现在其他地方
我将 motionEvent.getX() 和 motionEvent.getY() 的相同坐标与 canvas.drawBitmap 函数相加,然后减去标记的高度和宽度。此外,当使用 MotionEvent.ACTION_UP 设置位图时,也解决了滚动问题。这是触摸监听器的最终代码:
@Override
public boolean onTouch(View view, MotionEvent motionEvent) {
int touchX = (int) motionEvent.getX();
int touchY = (int) motionEvent.getY();
switch (motionEvent.getAction()){
case MotionEvent.ACTION_DOWN:
break;
case MotionEvent.ACTION_UP:
canvas.drawColor(Color.TRANSPARENT, PorterDuff.Mode.CLEAR);
canvas.drawBitmap(bitmapBluePrint, 0, 0, null);
canvas.drawBitmap(getMarkerBitmap(),touchX+touchX,touchY+touchY-120,null);
blueprint.invalidate();
break;
case MotionEvent.ACTION_MOVE:
break;
}
return true;
}
我在不同尺寸的设备上尝试过,效果很好。