我不知道如何将位图加载到画布中并在其上绘制。我尝试过,但是在位图上绘制不起作用,相反,它是在位图下绘制(我在后面看到了)。在此先感谢您,因为我是android新手,所以需要帮助。 正常的绘图正常,并且单击“保存”按钮也可以保存该绘图。但是编辑不正常,我尝试但不正常。
public class DrawingView extends View {
public static int BRUSH_SIZE=18;
public static int ERASER_SIZE=25;
public static final int DEFAULT_COLOR= Color.BLACK;
public static final int DEFAULT_BG_COLOR= Color.WHITE;
private static final float TOUCH_TOLERANCE= 4;
private float pointX,pointY;
private Path path;
private Paint paint;
private ArrayList<FingerPath> pathsList = new ArrayList<>();
private ArrayList<FingerPath> redoList = new ArrayList<>();
private int currentColor;
private int backgroundColor=DEFAULT_BG_COLOR;
private int strokeWidth;
private Bitmap bitmap,bitmapToEdit,nBmp,canvasBitmap;
private Canvas canvas;
private Paint bitmapPaint = new Paint(Paint.DITHER_FLAG);
private int lastPath;
private boolean editMode;
Realm mRealm;
byte[] imgBytes;
public DrawingView(Context context, AttributeSet attrs) {
super(context, attrs);
path=new Path();
paint=new Paint();
paint.setAntiAlias(true);
paint.setDither(true);
paint.setColor(DEFAULT_COLOR);
paint.setStyle(Paint.Style.STROKE);
paint.setStrokeJoin(Paint.Join.ROUND);
paint.setStrokeCap(Paint.Cap.ROUND);
}
public void init(DisplayMetrics metrics){
if(editMode)
startNew();
else {
int height = metrics.heightPixels;
int width = metrics.widthPixels;
bitmap = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888);
canvas = new Canvas(bitmap);
}
currentColor = DEFAULT_COLOR;
strokeWidth = BRUSH_SIZE;
}
public void startNew(){
canvas.drawColor(0, PorterDuff.Mode.CLEAR);
nBmp=bitmapToEdit.copy(Bitmap.Config.ARGB_8888,true);
canvas.drawBitmap(nBmp,0,0,paint);
invalidate();
}
@Override
protected void onSizeChanged(int w, int h, int oldw, int oldh) {
super.onSizeChanged(w, h, oldw, oldh);
Log.e("size changed","called");
bitmapToEdit=Bitmap.createBitmap(w,h, Bitmap.Config.ARGB_8888);
canvasBitmap=Bitmap.createBitmap(w,h, Bitmap.Config.ARGB_8888);
nBmp=Bitmap.createBitmap(w,h, Bitmap.Config.ARGB_8888);
canvas=new Canvas(canvasBitmap);
}
public void editImage(Bitmap gotbitmap){
editMode=true;
bitmapToEdit=gotbitmap;
}
public void save(Context context){
setDrawingCacheEnabled(true);
Bitmap saveBitmap=getDrawingCache();
imgBytes=new byte[0];
mRealm=Realm.getDefaultInstance();
if(saveBitmap!=null) {
ByteArrayOutputStream stream=new ByteArrayOutputStream();
saveBitmap.compress(Bitmap.CompressFormat.WEBP,50,stream);
imgBytes=stream.toByteArray();
final Drawing drawingObj=new Drawing();
mRealm.executeTransaction(new Realm.Transaction() {
@Override
public void execute(Realm realm) {
Number currentNum=mRealm.where(Drawing.class).maximumInt("id");
Log.e("currentNum",String.valueOf(currentNum));
int nextId;
if(currentNum==null){
nextId=1;
}else{
nextId=currentNum.intValue()+1;
}
Log.e("nextId",String.valueOf(nextId));
drawingObj.setId(nextId);
drawingObj.setImage(imgBytes);
mRealm.copyToRealmOrUpdate(drawingObj);
Log.e("imgBytes",imgBytes.toString());
}
});
Toast.makeText(context,"Image saved Successfully",Toast.LENGTH_LONG).show();
destroyDrawingCache();
}else{
Toast.makeText(context,"Image not found",Toast.LENGTH_LONG).show();
}
}
@Override
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);
if(editMode){
canvas.drawBitmap(canvasBitmap,0,0,bitmapPaint);
canvas.drawPath(path,paint);
}else {
canvas.save();
canvas.drawColor(backgroundColor);
for (FingerPath fp : pathsList) {
paint.setColor(fp.color);
paint.setStrokeWidth(fp.strokeWidth);
canvas.drawPath(fp.path, paint);
}
canvas.drawBitmap(bitmap, 0, 0, bitmapPaint);
canvas.restore();
}
}
//This function used when initial touch on screen
private void touchStart(float x,float y){
redoList.clear();
path = new Path();
FingerPath fingerPath = new FingerPath(currentColor,strokeWidth,path);
pathsList.add(fingerPath);
path.reset();
path.moveTo(x,y);
pointX=x;
pointY=y;
}
//This function used when keeping finger down and moving on screen
private void touchMove(float x,float y){
float dx = Math.abs(x - pointX);
float dy = Math.abs(y - pointY);
if(dx >= TOUCH_TOLERANCE || dy >= TOUCH_TOLERANCE){
path.quadTo(pointX,pointY,(x + pointX)/2,(y + pointY)/2);
pointX=x;
pointY=y;
}
}
//This function used when after drawing, finger is removed from the screen
private void touchUp(){
path.lineTo(pointX,pointY);
}
@Override
public boolean onTouchEvent(MotionEvent event) {
float x = event.getX();
float y = event.getY();
if(editMode){
switch (event.getAction()) {
case MotionEvent.ACTION_DOWN:
path.moveTo(x,y);
break;
case MotionEvent.ACTION_MOVE:
path.lineTo(x,y);
break;
case MotionEvent.ACTION_UP:
canvas.drawPath(path,paint);
path.reset();
break;
}
invalidate();
}
else{
switch (event.getAction()) {
case MotionEvent.ACTION_DOWN:
touchStart(x, y);
invalidate();
break;
case MotionEvent.ACTION_MOVE:
touchMove(x, y);
invalidate();
break;
case MotionEvent.ACTION_UP:
touchUp();
invalidate();
break;
}
}
return true;
}
}
答案 0 :(得分:1)
如果editMode
为false,则在绘制路径之后绘制位图。这将把位图放在最上面。要解决此问题,请将canvas.drawBitmap()
呼叫放在canvas.drawColor(backgroundColor)
呼叫之后:)