I have a custom view DrawingPanel and in I'm using this to make a paint application which lets you draw on image.I now want to add the erasing capability to make the image transparent.I've tried using mPaint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.CLEAR)); but it gives me black color on touch and I can't change this color.How to improve the erasing bitmap functionality?
public DrawingPanel(Context context, int color) {
super(context);
this.color = color;
setFocusable(true);
setFocusableInTouchMode(true);
this.setOnTouchListener(this);
mBitmapPaint = new Paint(Paint.DITHER_FLAG);
mPaint = new Paint();
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(3);
mPaint.setTextSize(30);
mPath = new Path();
paths.add(new PathPoints(mPath, color, false));
mCanvas = new Canvas();
}
#Override
protected void onDraw(Canvas canvas) {
canvas.drawBitmap(mBitmap, 0, 0, mBitmapPaint);
for (PathPoints p : paths) {
mPaint.setColor(p.getColor());
Log.v("", "Color code : " + p.getColor());
if (p.isTextToDraw()) {
canvas.drawText(p.textToDraw, p.x, p.y, mPaint);
} else {
canvas.drawPath(p.getPath(), mPaint);
}
}
}
Related
Here is the Code:
#Override
public void draw(Canvas canvas) {
super.draw(canvas);
drawUPS( canvas);
drawFPS( canvas);
}
public void drawUPS(#NonNull Canvas canvas){
String averageUPS = Double.toString(gameloop.getAverageUPS());
Paint textPaint = new Paint();
int color = ContextCompat.getColor(context, R.color.white);
canvas.drawText("UPS" + averageUPS, 100, 100,textPaint);
textPaint.setColor(color);
textPaint.setTextSize(50);
}
public void drawFPS(#NonNull Canvas canvas){
String averageFPS = Double.toString(gameloop.getAverageFPS());
Paint paint = new Paint();
int color = ContextCompat.getColor(context, R.color.white);
paint.setColor(color);
paint.setTextSize(50);
canvas.drawText("FPS" + averageFPS, 100, 200,paint);
}
I did everything that is necessary for the canvas to be drawn but it just wont and i dont find any solutions anywhere
class SquareImageView extends View {
public SquareImageView(Context context) {
super(context);
init();
}
private void init() {
bitmap = Bitmap.createBitmap(500, 500, Bitmap.Config.ARGB_8888);
=mCanvas = new Canvas(bitmap);
mpaint = new Paint();
mpaint.setAntiAlias(true);
mpaint.setStyle(Paint.Style.STROKE);
mpaint.setColor(Color.RED);
mCanvas.drawColor(Color.BLUE, PorterDuff.Mode.DARKEN);
mCanvas.drawRect(100, 100, 200, 200, mpaint);
}
#Override
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);
}``
}
if i use mCanas.drawRect() inside init method then it's not drawing anything but if i use inBuilt canvas object inside OnDraw(Canvas canvas) method like this
#Override
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);
canvas.drawRect(100, 100, 200, 200, mpaint);
}
then rectangle is drawing and if i used mCanvas inside onDraw() then again it's not showing anything can anybody explain to me why this happening i m really get frustrated any help would be appreciated
It means that onDraw is the only drawing gate for you. https://developer.android.com/training/custom-views/custom-drawing#java
I am developing color splash app. in which i use finger paint.
Now i want to emboss bitmap on touch event. i have one demo here in which when i apply emboss then it draw emboss path with red color but i want to emboss behind bitmap on touch.
private Path mPath;
private MaskFilter mEmboss;
public void init(){
mPaint = new Paint();
mPaint.setAntiAlias(true);
mPaint.setDither(true);
mPaint.setColor(color.RED);
mPaint.setStyle(Paint.Style.STROKE);
mPaint.setStrokeJoin(Paint.Join.ROUND);
mPaint.setStrokeCap(Paint.Cap.ROUND);
mPaint.setStrokeWidth(20);
mEmboss = new EmbossMaskFilter(new float[] { 1, 1, 1 }, 0.4f, 6, 3.5f);
}
// on click event
switch (item.getItemId()) {
case EMBOSS_MENU_ID:
mPaint.setMaskFilter(mEmboss);
}
// View Class method
#Override
public void onDraw(Canvas canvas) {
super.onDraw(canvas);
canvas.drawPath(mPath, mPaint);
canvas.drawPath(circlePath, circlePaint);
}
#Override
public boolean onTouchEvent(MotionEvent ev) {
float x = ev.getX();
float y = ev.getY();
invalidate();
return true;
}
I eventually find the solution:
Use BitmapShader with same bitmap
private Path mPath;
private MaskFilter mEmboss;
public void init(){
mPaint = new Paint();
mPaint.setAntiAlias(true);
mPaint.setDither(true);
mPaint.setColor(color.RED);
mPaint.setStyle(Paint.Style.STROKE);
mPaint.setStrokeJoin(Paint.Join.ROUND);
mPaint.setStrokeCap(Paint.Cap.ROUND);
mPaint.setStrokeWidth(20);
BitmapShader fillBMPshader = new BitmapShader(bm_original, Shader.TileMode.MIRROR, Shader.TileMode.CLAMP);
mPaint.setShader(fillBMPshader);
mEmboss = new EmbossMaskFilter(new float[] { 1, 1, 1 }, 0.4f, 6, 3.5f);
}
// onclick event
switch (item.getItemId()) {
case EMBOSS_MENU_ID:
mPaint.setMaskFilter(mEmboss);
}
// View Class method
#Override
public void onDraw(Canvas canvas) {
super.onDraw(canvas);
canvas.drawPath(mPath, mPaint);
canvas.drawPath(circlePath, circlePaint);
}
#Override
public boolean onTouchEvent(MotionEvent ev) {
float x = ev.getX();
float y = ev.getY();
invalidate();
return true;
}
I have onDraw method where I want to cut a small piece of large bitmap. This will be a circle (position X and Y). Then I want to draw it by canvas.
I rewrite method from this answer for that reason, but always got grey circle instead circle piece of large Bitmap.
private Bitmap cutCircleBitmap() {
Bitmap output = Bitmap.createBitmap(2 * RADIUS, 2 * RADIUS, Bitmap.Config.ARGB_8888);
Canvas canvas = new Canvas(output);
final int color = 0xff424242;
final Paint paint = new Paint();
final Rect rect = new Rect(bitmapX - RADIUS, bitmapY - RADIUS, 2 * RADIUS, 2 * RADIUS);
paint.setAntiAlias(true);
canvas.drawARGB(0, 0, 0, 0);
paint.setColor(color);
canvas.drawCircle(RADIUS, RADIUS, RADIUS, paint);
paint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.SRC_IN));
canvas.drawBitmap(bitmap, rect, rect, paint);
return output;
}
Use Canvas.drawRoundRect() and BitmapShader to do it :
public class CropView extends View {
public CropView(Context context) {
this(context, null);
}
public CropView(Context context, AttributeSet attrs) {
this(context, attrs, 0);
}
public CropView(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
mPaint = new Paint(Paint.ANTI_ALIAS_FLAG);
mPaint.setFilterBitmap(true);
mPaint.setAntiAlias(true);
mPaint.setDither(true);
}
#Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
setMeasuredDimension(radius * 2, radius * 2);
}
private Paint mPaint;
private int radius = 100;
#Override
protected void onDraw(Canvas canvas) {
canvas.drawColor(Color.BLACK); // draw background help us see the result
Bitmap srcBitmap = BitmapFactory.decodeResource(getResources(), R.drawable.sample);
canvas.drawBitmap(cropCircleBitmap(srcBitmap, 200, 60), 0, 0, mPaint);
}
private Bitmap cropCircleBitmap(Bitmap srcBitmap, float left, float top) {
Bitmap dstBitmap = Bitmap.createBitmap(getWidth(), getHeight(), Bitmap.Config.ARGB_8888);
Canvas canvas = new Canvas(dstBitmap);
canvas.drawBitmap(srcBitmap, -left, -top, mPaint);
mPaint.setShader(new BitmapShader(dstBitmap, Shader.TileMode.CLAMP, Shader.TileMode.CLAMP));
dstBitmap = Bitmap.createBitmap(getWidth(), getHeight(), Bitmap.Config.ARGB_8888);
canvas.setBitmap(dstBitmap);
canvas.drawRoundRect(new RectF(0, 0, getWidth(), getHeight()), radius, radius, mPaint);
return dstBitmap;
}
}
I searched here lot of answers found but no one is worked like 1.Deleting a path from a canvas - Android 2.How to erase path area from canvas (Android).
my coding is:
mClear.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
drawView.clear();
}
});
public void init() {
mPaint = new Paint();
mPaint.setAntiAlias(true);
mPaint.setColor(Color.GREEN);
mPaint.setStyle(Paint.Style.STROKE);
mPaint.setStrokeJoin(Paint.Join.ROUND);
mPaint.setStrokeCap(Paint.Cap.ROUND);
mPaint.setStrokeWidth(3);
}
class DrawingView extends View {
Path path;
Bitmap mBitmap;
Canvas mCanvas;
public DrawingView(Context context) {
super(context);
path = new Path();
mBitmap = Bitmap.createBitmap(820, 480, Bitmap.Config.ARGB_8888);
mCanvas = new Canvas(mBitmap);
this.setBackgroundColor(Color.WHITE);
}
public void clear() {
path.reset();
invalidate();
}
#Override
public boolean onTouchEvent(MotionEvent event) {
mCanvas.drawPath(path, mPaint);
if (event.getAction() == MotionEvent.ACTION_DOWN) {
path.moveTo(event.getX(), event.getY());
path.lineTo(event.getX(), event.getY());
} else if (event.getAction() == MotionEvent.ACTION_MOVE) {
path.lineTo(event.getX(), event.getY());
}
invalidate();
return true;
}
#Override
protected void onDraw(Canvas canvas) {
canvas.drawPath(path, mPaint);
}
}
I tried for clearing some other like below:
mBitmap = Bitmap.createBitmap(820, 480, Bitmap.Config.ARGB_8888);
mCanvas = new Canvas(mBitmap);
i need some assist here..please give some idea to delete
Try some thing like this in your
clearing method like:
((YourDrawingClass)YourView).clear();