У меня есть код для пользовательского просмотра, но я не могу понять, как установить изображение, чтобы я мог рисовать на нем. Я пробовал решения, указанные в stackoverflow, но изображение не добавляется, может кто-нибудь помочь мне с этим, Пожалуйста, проверьте код, указанный ниже:
Или как мне установить изображение в качестве фона для моего customview. Я получаю только белый фон.
public class CustomView extends View {
private static final String LOG_CAT = CustomView.class.getSimpleName();
//drawing path
private Path drawPath;
//defines what to draw
private Paint canvasPaint;
//defines how to draw
private Paint drawPaint;
//initial color
private int paintColor = 0xFF660000;
private Paint _paintBlur;
//canvas - holding pen, holds your drawings
//and transfers them to the view
private Canvas drawCanvas;
//canvas bitmap
private Bitmap canvasBitmap;
//brush size
private float currentBrushSize, lastBrushSize;
private ArrayList<Path> paths = new ArrayList<Path>();
private ArrayList<Path> undonePaths = new ArrayList<Path>();
private float mX, mY;
private static final float TOUCH_TOLERANCE = 4;
private void init(){
currentBrushSize = 20;
lastBrushSize = currentBrushSize;
drawPath = new Path();
drawPaint = new Paint();
drawPaint.setColor(paintColor);
drawPaint.setAntiAlias(true);
drawPaint.setStrokeWidth(currentBrushSize);
drawPaint.setStyle(Paint.Style.STROKE);
drawPaint.setStrokeJoin(Paint.Join.ROUND);
drawPaint.setStrokeCap(Paint.Cap.ROUND);
canvasPaint = new Paint(Paint.DITHER_FLAG);
this._paintBlur = new Paint();
this._paintBlur.set(drawPaint);
this._paintBlur.setAntiAlias(true);
this._paintBlur.setDither(true);
this._paintBlur.setStyle(Paint.Style.STROKE);
this._paintBlur.setStrokeJoin(Paint.Join.ROUND);
this._paintBlur.setStrokeCap(Paint.Cap.ROUND);
this._paintBlur.setColor(Color.RED);
this._paintBlur.setStrokeWidth(6);
this._paintBlur.setMaskFilter(new BlurMaskFilter(10.0F, BlurMaskFilter.Blur.OUTER));
}
public CustomView(Context context, AttributeSet attrs) {
super(context, attrs);
init();
}
@Override
protected void onDraw(Canvas canvas) {
for (Path p : paths) {
canvas.drawPath(p, drawPaint);
}
canvas.drawPath(drawPath, drawPaint);
}
@Override
protected void onSizeChanged(int w, int h, int oldw, int oldh) {
//create canvas of certain device size.
super.onSizeChanged(w, h, oldw, oldh);
//create Bitmap of certain w,h
canvasBitmap = Bitmap.createBitmap(w, h, Bitmap.Config.ARGB_8888);
//apply bitmap to graphic to start drawing.
drawCanvas = new Canvas(canvasBitmap);
drawCanvas.drawBitmap(BitmapFactory.decodeResource(getResources(),R.drawable.one),0,0,drawPaint);
}
@Override
public boolean onTouchEvent(MotionEvent event) {
float touchX = event.getX();
float touchY = event.getY();
switch (event.getAction()){
case MotionEvent.ACTION_DOWN:
touch_start(touchX, touchY);
invalidate();
break;
case MotionEvent.ACTION_MOVE:
touch_move(touchX, touchY);
invalidate();
break;
case MotionEvent.ACTION_UP:
touch_up();
invalidate();
break;
default:
return false;
}
return true;
}
private void touch_start(float x, float y) {
undonePaths.clear();
drawPath.reset();
drawPath.moveTo(x, y);
mX = x;
mY = y;
}
private void touch_up() {
drawPath.lineTo(mX, mY);
drawCanvas.drawPath(drawPath, drawPaint);
paths.add(drawPath);
drawPath = new Path();
}
private void touch_move(float x, float y) {
float dx = Math.abs(x - mX);
float dy = Math.abs(y - mY);
if (dx >= TOUCH_TOLERANCE || dy >= TOUCH_TOLERANCE) {
drawPath.quadTo(mX, mY, (x + mX)/2, (y + mY)/2);
mX = x;
mY = y;
}
}
public void onClickUndo () {
if (paths.size()>0)
{
undonePaths.add(paths.remove(paths.size()-1));
invalidate();
}
}
}