drawview.java_draw on image view in android

I try to save raster of ImageView but when I get the raster the background color changes to black. I don't really sure what's happening but I erase background to white but it changes every time to black when I save it to SQLite and fetch and set it again. Here is my costume drawer class.

import android.content.Context;

import android.graphics.Bitmap;

import android.graphics.Bitmap.Config;

import android.graphics.Canvas;

import android.graphics.Color;

import android.graphics.Paint;

import android.graphics.Path;

import android.util.AttributeSet;

import android.view.MotionEvent;

import android.view.View;

public class MyDrawView extends View

{

public Bitmap mBitmap;

public Canvas mCanvas;

private Path mPath;

private Paint mBitmapPaint;

private Paint mPaint;

public MyDrawView(Context c, AttributeSet attrs)

{

super(c, attrs);

mPath = new Path();

mBitmapPaint = new Paint(Paint.DITHER_FLAG);

mPaint = new Paint();

mPaint.setAntiAlias(true);

mPaint.setDither(true);

mPaint.setColor(Color.WHITE);

mPaint.setStyle(Paint.Style.STROKE);

mPaint.setStrokeJoin(Paint.Join.ROUND);

mPaint.setStrokeCap(Paint.Cap.ROUND);

mPaint.setStrokeWidth(9);

}

public void eraser()

{

mPaint.setAntiAlias(true);

mPaint.setDither(true);

mPaint.setColor(Color.WHITE);

mPaint.setStyle(Paint.Style.STROKE);

mPaint.setStrokeJoin(Paint.Join.ROUND);

mPaint.setStrokeCap(Paint.Cap.ROUND);

mPaint.setStrokeWidth(22);

}

public void pen(int color, int size)

{

mPaint.setAntiAlias(true);

mPaint.setDither(true);

mPaint.setColor(color);

mPaint.setStyle(Paint.Style.STROKE);

mPaint.setStrokeJoin(Paint.Join.ROUND);

mPaint.setStrokeCap(Paint.Cap.ROUND);

mPaint.setStrokeWidth(size);

}

@Override

protected void onSizeChanged(int w, int h, int oldw, int oldh)

{

super.onSizeChanged(w, h, oldw, oldh);

mBitmap = Bitmap.createBitmap(w, h, Config.ARGB_8888);

mCanvas = new Canvas(mBitmap);

}

@Override

protected void onDraw(Canvas canvas)

{

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

canvas.drawPath(mPath, mPaint);

}

private float mX, mY;

private static final float TOUCH_TOLERANCE = 4;

private void touch_start(float x, float y)

{

mPath.reset();

mPath.moveTo(x, y);

mX = x;

mY = y;

}

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)

{

mPath.quadTo(mX, mY, (x + mX) / 2, (y + mY) / 2);

mX = x;

mY = y;

}

}

private void touch_up()

{

mPath.lineTo(mX, mY);

// commit the path to our offscreen

mCanvas.drawPath(mPath, mPaint);

// kill this so we don't double draw

mPath.reset();

}

@Override

public boolean onTouchEvent(MotionEvent event)

{

float x = event.getX();

float y = event.getY();

switch (event.getAction())

{

case MotionEvent.ACTION_DOWN:

touch_start(x, y);

invalidate();

break;

case MotionEvent.ACTION_MOVE:

touch_move(x, y);

invalidate();

break;

case MotionEvent.ACTION_UP:

touch_up();

invalidate();

break;

}

return true;

}

public Bitmap getBitmap()

{

//this.measure(100, 100);

//this.layout(0, 0, 100, 100);

this.setDrawingCacheEnabled(true);

this.buildDrawingCache();

Bitmap bmp = Bitmap.createBitmap(this.getDrawingCache());

this.setDrawingCacheEnabled(false);

return bmp;

}

public void clear()

{

mBitmap.eraseColor(Color.WHITE);

invalidate();

System.gc();

}

}

and in my activity I have below code: public class MainActivity extends AppCompatActivity { MyDrawView myDrawView; ImageView imageView;

@Override

protected void onCreate(Bundle savedInstanceState)

{

super.onCreate(savedInstanceState);

setContentView(R.layout.activity_main);

imageView = findViewById(R.id.img);

myDrawView = findViewById(R.id.draw);

myDrawView.setBackground(getResources()

.getDrawable(R.drawable.bg_customer));

Button button1 = findViewById(R.id.button1);

button1.setOnClickListener(new View.OnClickListener()

{

public void onClick(View v)

{

Bitmap well = myDrawView.getBitmap();

imageView.setImageBitmap(well);

imageView.setOnClickListener(new View.OnClickListener()

{

@Override

public void onClick(View view)

{

//now save in room database

}

});

}

});

}

}

when I save My paint background is black (not image) can anybody help me?

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值