将图像插入画布

时间:2019-01-08 09:39:14

标签: android canvas bitmap

我正在尝试在自定义视图类的mCanvas.drawBitmap(iconBitmap,x-100,y-100, mBitmapPaint);事件中使用方法MotionEvent.ACTION_DOWN。但是,我要显示的图像此时并未出现在画布上。我正在尝试制作一个绘画应用程序,用户可以在其中绘画自由手并同时插入图像以进行游戏。

这是我的自定义视图类:

package com.example.shazs.autismate;
public class PaintView extends View {

public static int BRUSH_SIZE = 20;
public static final int DEFAULT_COLOR = Color.RED;
public static final int DEFAULT_BG_COLOR = Color.WHITE;
private static final float TOUCH_TOLERANCE = 4;
private float mX, mY;
private Path mPath;
private Paint mPaint;
private ArrayList<FingerPath> paths = new ArrayList<>();
private int currentColor;
private int backgroundColor = DEFAULT_BG_COLOR;
private int strokeWidth;
private boolean emboss;
private boolean blur;
private MaskFilter mEmboss;
private MaskFilter mBlur;
private Bitmap mBitmap;
private Paint mBitmapPaint;
private Canvas mCanvas;
private Bitmap iconBitmap;
private static AtomicBoolean drawIcon = new AtomicBoolean();
public PaintView(Context context) {
    this(context, null);
}

public PaintView(Context context, AttributeSet attrs) {
    super(context, attrs);
    mPaint = new Paint();
    mPaint.setAntiAlias(true);
    mPaint.setDither(true);
    mPaint.setColor(DEFAULT_COLOR);
    mPaint.setStyle(Paint.Style.STROKE);
    mPaint.setStrokeJoin(Paint.Join.ROUND);
    mPaint.setStrokeCap(Paint.Cap.ROUND);
    mPaint.setXfermode(null);
    mPaint.setAlpha(0xff);

    mBitmapPaint = new Paint(Paint.DITHER_FLAG);
    mEmboss = new EmbossMaskFilter(new float[] {1, 1, 1}, 0.4f, 6, 3.5f);
    mBlur = new BlurMaskFilter(5, BlurMaskFilter.Blur.NORMAL);
}

public void init(DisplayMetrics metrics) {
    int height = metrics.heightPixels;
    int width = metrics.widthPixels;

    mBitmap = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888);
    mCanvas = new Canvas(mBitmap);

    currentColor = DEFAULT_COLOR;
    strokeWidth = BRUSH_SIZE;
}

public void normal() {
    emboss = false;
    blur = false;
}

public void emboss() {
    emboss = true;
    blur = false;
}

public void blur() {
    emboss = false;
    blur = true;
}

public void setPaintColor(int color){
    if (currentColor!=color)
        Log.d("xyzn","current color changed");
    else
        Log.d("xyzn","current color not changed");
    currentColor=color;

}

public void drawBitmap(Bitmap bm){
    drawIcon.set(true);
    iconBitmap = bm;
}


public int getPaintColor(){
    return currentColor;
}
public void clear() {
    backgroundColor = DEFAULT_BG_COLOR;
    paths.clear();
    normal();
    invalidate();
}

@Override
protected void onDraw(Canvas canvas) {
    canvas.save();
    mCanvas.drawColor(backgroundColor);

    for (FingerPath fp : paths) {
        mPaint.setColor(fp.color);
        mPaint.setStrokeWidth(fp.strokeWidth);
        mPaint.setMaskFilter(null);

        if (fp.emboss)
            mPaint.setMaskFilter(mEmboss);
        else if (fp.blur)
            mPaint.setMaskFilter(mBlur);

        mCanvas.drawPath(fp.path, mPaint);

    }

    canvas.drawBitmap(mBitmap, 0, 0, mBitmapPaint);
    canvas.restore();
}

private void touchStart(float x, float y) {
    mPath = new Path();
    FingerPath fp = new FingerPath(currentColor, emboss, blur, strokeWidth, mPath);
    paths.add(fp);

    mPath.reset();
    mPath.moveTo(x, y);
    mX = x;
    mY = y;
}

private void touchMove(float x, float y) {
    float dx = Math.abs(x - mX);
    float dy = Math.abs(y - mY);

    if (dx >= TOUCH_TOLERANCE || dy >= TOUCH_TOLERANCE) {
        mPath.quadTo(mX, mY, (x + mX) / 2, (y + mY) / 2);
        mX = x;
        mY = y;
    }
}

private void touchUp() {
    mPath.lineTo(mX, mY);
}

@Override
public boolean onTouchEvent(MotionEvent event) {


    float x = event.getX();
    float y = event.getY();

    switch(event.getAction()) {
        case MotionEvent.ACTION_DOWN :
            if (drawIcon.get()){
                mCanvas.drawBitmap(iconBitmap,x-100,y-100, mBitmapPaint);
                mCanvas.save();

                break;
            }
            touchStart(x, y);
            invalidate();
            break;
        case MotionEvent.ACTION_MOVE :
            if (drawIcon.get())
                break;
            touchMove(x, y);
            invalidate();
            break;
        case MotionEvent.ACTION_UP :
            if (drawIcon.get()){
                drawIcon.set(false);
                break;
            }
            touchUp();
            invalidate();
            break;
    }

    return true;
}

}

使用此自定义视图类并传递对象的位图以绘制的主要活动:

public class drawActivity extends AppCompatActivity implements 
ColorPickerDialog.OnColorChangedListener {

private PaintView paintView;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.css_layout);
    paintView = (PaintView) findViewById(R.id.paintView);
    DisplayMetrics metrics = new DisplayMetrics();
    getWindowManager().getDefaultDisplay().getMetrics(metrics);
    paintView.init(metrics);
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    MenuInflater menuInflater = getMenuInflater();
    menuInflater.inflate(R.menu.main, menu);
    return super.onCreateOptionsMenu(menu);
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    switch(item.getItemId()) {
        case R.id.draw:
            Bitmap bm = BitmapFactory.decodeResource(getResources(), R.drawable.apple);
            paintView.drawBitmap(bm);
            break;

我使用drawIcon来查看用户是否需要插入图标还是徒手画画。

有趣的是,当我进入手机的应用程序视图以查看所有当前正在运行的应用程序时,图标/图像显示在画布上。但是,当我返回到应用程序时,它消失了。插入图标后只会发生一次,不会重复。

enter image description here

enter image description here

1 个答案:

答案 0 :(得分:1)

好,我必须删除该行
mCanvas.drawColor(backgroundColor); 从我的onDraw(Canvas canvas)方法中调用invalidate()