캔버스 (backgroundBitmap)에 변경되지 않고 유지하려는 비트 맵 하나와 backgroundBitmap 위에 드래그 할 수있는 다른 작은 비트 맵 (draggableBitmap)이 있습니다. (그리고 나는 z 축 에서처럼 "위"를 의미합니다).Android 그래픽 - 비트 맵을 드래그하는 동안 캔버스 지우기
제 생각에는 각 ACTION_MOVE로 배경을 다시 그리는 것입니다. 단색으로이 작업을 수행하면 완벽하게 작동합니다. color 대신 backgroundBitmap을 다시 그릴 때 backgroundBitmap은 계속 표시되지만 draggableBitmap은 끌린 경로를 따라 반복됩니다. 단색이 이미지를 "제거"하고 비트 맵이 작동하지 않는 이유는 무엇입니까?
편집 : 많은 도움을 주신 Steve K에게 많은 감사를드립니다. 지금까지 진행 상황을 반영하도록 아래 코드를 업데이트했습니다. 이 시점에서 유일한 문제는 끌기가 시작된 후 이미지가 완전히 검게 변하는 것입니다. 이 배경 비트 맵을 변경할 수있는 설정과 관련이 있습니까? 나는 그것없이 mutability error를 얻고 있었다.
package com.example.drawing;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Path;
import android.graphics.PorterDuff;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.view.View;
public class DrawingView extends View {
// drawing and canvas paint
private Paint canvasPaint;
// canvas
private Canvas drawCanvas;
// canvas bitmap
private Bitmap backgroundBitmap, draggableBitmap;
public DrawingView(Context context, AttributeSet attrs) {
super(context, attrs);
setupDrawing();
}
private void setupDrawing() {
canvasPaint = new Paint();
canvasPaint.setColor(Color.TRANSPARENT);
}
@Override
protected void onSizeChanged(int w, int h, int oldw, int oldh) {
super.onSizeChanged(w, h, oldw, oldh);
BitmapFactory.Options opt1 = new BitmapFactory.Options();
opt1.inMutable = true;
opt1.inSampleSize = 4;
BitmapFactory.Options opt2 = new BitmapFactory.Options();
opt2.inMutable = true;
backgroundBitmap = BitmapFactory.decodeFile("/storage/emulated/0/DCIM/Camera/20140901_223343.jpg", opt1);
draggableBitmap = BitmapFactory.decodeResource(getResources(), R.drawable.draggable_image, opt2);
drawCanvas = new Canvas(backgroundBitmap);
}
@Override
protected void onDraw(Canvas canvas) {
canvas.drawBitmap(backgroundBitmap, 0, 0, null);
}
@Override
public boolean onTouchEvent(MotionEvent event) {
float touchX = event.getX();
float touchY = event.getY();
switch (event.getAction()) {
case MotionEvent.ACTION_DOWN:
// Nothing here
break;
case MotionEvent.ACTION_MOVE:
drawCanvas.drawColor(Color.TRANSPARENT, PorterDuff.Mode.CLEAR);
drawCanvas.drawBitmap(backgroundBitmap, 0, 0, null);
drawCanvas.drawBitmap(draggableBitmap, touchX, touchY, null);
break;
case MotionEvent.ACTION_UP:
// Nothing here
break;
default:
return false;
}
invalidate();
return true;
}
}
죄송합니다. 마지막 코멘트를 삭제했습니다. .drawBitmap의 마지막 인자가'Paint' 객체 일 필요가있는 것처럼 보입니다. 나는 색상을'Color.TRANSPARENT'으로 설정하고 그것을 대신 사용했지만 변경을 시도하지 않았습니다. – user2864874
좋아, 잠깐만 -'lastX','lastY' 비트 맵에 대해'TRANSPARENT'를 색으로 설정하면 화면의 0,0 구석에 잠긴 더 작은 비트 맵 사본이 있습니다. 색상을 '투명'으로 설정하면 사라집니다. 귀하의 제안이 작동하지만 투명 비트 맵이 움직이지 않는 것처럼 보입니다. logcat의 Sysout은 lastX/Y 값을 올바르게 업데이트하는 것을 보여줍니다. – user2864874
이것은 여전히'draggableBitmap'의 흔적을 남깁니다. 모서리에있는 비트 맵이가는 한 원래 코드는 괜찮 았습니다. – user2864874