I am trying to figure out how to simply draw a line on an image that is being set in Picasso. I found that if I simply set the image, given a URI, with Picasso and try to draw paint to it using the following:
canvas = new Canvas(bitmap);
image.draw(canvas);
topEdge = new Paint();
topEdge.setColor(context.getResources().getColor(R.color.blue));
topEdge.setStrokeWidth(5);
canvas.drawLine(c1.getX(), c1.getY(), c2.getX(), c2.getY(), topEdge);
Then I get a crash saying that the bitmap needs to be mutable first. So I added this above that code:
Bitmap workingBitmap = ((BitmapDrawable) image.getDrawable()).getBitmap();
Bitmap mutableBitmap = workingBitmap.copy(Bitmap.Config.ARGB_8888, true);
And then create the canvas with new Canvas(mutableBitmap) instead. This removed the crash, however nothing is being drawn. I believe this is because my Picasso is setting the image before, so now I need to reset Picasso with this new mutable bitmap. The problem is this code is in the onSuccess() callback for Picasso. What can I do to allow Paint to be drawn on an image through Picasso?
just follow the steps below:
Write your own class extends the class Transformation like below:
class DrawLineTransformation implements Transformation {
#Override
public String key() {
// TODO Auto-generated method stub
return "drawline";
}
#Override
public Bitmap transform(Bitmap bitmap) {
// TODO Auto-generated method stub
synchronized (DrawLineTransformation.class) {
if(bitmap == null) {
return null;
}
Bitmap resultBitmap = bitmap.copy(bitmap.getConfig(), true);
Canvas canvas = new Canvas(resultBitmap);
Paint paint = new Paint();
paint.setColor(Color.BLUE);
paint.setStrokeWidth(10);
canvas.drawLine(0, resultBitmap.getHeight()/2, resultBitmap.getWidth(), resultBitmap.getHeight()/2, paint);
bitmap.recycle();
return resultBitmap;
}
}
}
2、Add the Transformation to RequestCreator created with Picasso.load() function like below:
Picasso picasso = Picasso.with(getApplicationContext());
DrawLineTransformation myTransformation = new DrawLineTransformation();
picasso.load("http://www.baidu.com/img/bdlogo.png").transform(myTransformation).into(imageview);
That's all steps you need to do , just enjoy!
Related
I am trying to invert image mask bitmap using below code
static final PorterDuffXfermode eraseMode = new PorterDuffXfermode(PorterDuff.Mode.CLEAR);
public void invertSelection() {
Bitmap inverted = Bitmap.createBitmap(imageBitmap.getWidth(), imageBitmap.getHeight(), Bitmap.Config.ARGB_8888);
if (!annotationBitmap.sameAs(inverted)) {
Canvas canvas = new Canvas(inverted);
paint.setColor(Color.RED);
canvas.drawPaint(paint);
paint.setXfermode(eraseMode);
canvas.drawBitmap(annotationBitmap, 0,0,paint);
annotationBitmap = inverted;
undoStack.push(annotationBitmap.copy(annotationBitmap.getConfig(), true));
invalidate();
}
}
after calling this function I am no longer able to draw on annotationBitmap.
What am I doing wrong here???
I think you should read this beautiful article at medium
and i am not sure but i think if you changePorterDuff.Mode.CLEAR to PorterDuff.Mode.DST_OUT
your problem will solve
In my application am using canvas and adding image to canvas as bitmap.I am making bitmap transparent and erasing bitmap.I need to undo erased bitmap while trying sometimes the bitmap which is transparent it is becoming more brightness i think its alpha value is changing.How to avoid this please help me how to solve this issue.Below is my code.
// TODO Auto-generated method stub
int num=imgbit.size();
if(num>=1)
{
unoo=1;
if(num==1)
{
bitmap=null;
bitmap=imgbit.get(0);
Paint paint = new Paint();
paint.setAlpha(178);
pcanvas.drawBitmap(bitmap, 0,0, paint);
undo.setEnabled(false);
}
if((num!=1))
{
bitmap=null;
if(capture==true)
{
bitmap=imgbit.get(num-2);
}
else
{
bitmap=imgbit.get(num-1);
}
Paint paint = new Paint();
paint.setAlpha(178);
pcanvas.drawBitmap(bitmap, 0, 0,paint);
}
imgbit.remove(num-1);
}
capture=false;
In my application I extended the ImageView and overriden its onDraw() method. I am using a color filter to manipulate the bitmap for adding some effects like invert, grayscale etcc. After drawing the bitmap I am trying to save it but I am only able to save the original bitmap with no added effects. Here is the code for onDraw() and save method:
protected void onDraw(Canvas canvas)
{
Paint paint = mPaint;
//cmf is the color matrix filter
paint.setColorFilter(cmf);
if(mBitmap != null)
{
canvas.drawBitmap(mBitmap, offsetW, offsetH, paint);
}
}
code for saving the bitmap:
try
{
FileOutputStream fout = new FileOutputStream(path);
mBitmap.compress(CompressFormat.JPEG, 100, fout);
} catch (FileNotFoundException e)
{
e.printStackTrace();
}
Am I doing something wrong? Any help will be appretiated.
You are painting on the canvas that is displayed, original bitmap is not changed. You should create a new bitmap and paint on it. When color matrix filter changes do this:
Bitmap tmp = Bitmap.createBitmap(mBitmap.getWidth(), mBitmap.getHeight(), mBitmap.getConfig())
Canvas canvas = new Canvas(tmp)
cnvas.drawBitmap(tmp, 0, 0, paint);
Then, you can use this tmp bitmap to draw it and save it.
Instead of using customized ImageView use a normal one and set its image to this new bitmap:
imageView.setImageBitmap(tmp)
I'm trying to draw to a Bitmap so I can put my custom view inside an imageView.The code within the onDraw method is:
public void onDraw(Canvas canvas) {
Bitmap drawGraph = Bitmap.createBitmap(canvas.getWidth(),canvas.getHeight(),Bitmap.Config.ARGB_8888);
canvas.setBitmap(drawGraph);
canvas.drawBitmap(drawGraph, 0, 0, bgPaint);
My problem is that if I try to use a Bitmap in this way, I just get a black screen. I know that the rest of my code works as it displays if I don't try to draw to a bitmap.
If I comment out the line
canvas.setBitmap(drawGraph);
Then everything works perfectly, so this is the problem but I dont know why.
where am I going wrong?
Turns out I did have to create a second canvas. My working code is below just for anyone who might need it:
public void onDraw(Canvas canvas) {
Canvas singleUseCanvas = new Canvas();
drawGraph = Bitmap.createBitmap(canvas.getWidth(),canvas.getHeight(),Bitmap.Config.ARGB_8888);
singleUseCanvas.setBitmap(drawGraph);
canvas.drawBitmap(drawGraph, 100, 100, bgPaint);
I think is the canvas and canvas2 dichotomy. Try to use only canvas2 (the parameter) to draw.
AFAIK The most efficient way is to override drawable setters.
#Override
public void setImageBitmap(Bitmap bm) {
bmp = bm;
}
#Override
public void setImageDrawable(Drawable drawable) {
try {
bmp = ((BitmapDrawable) drawable).getBitmap();
} catch (Exception e){
log(e.toString());
}
}
Good Day Everyone
I was hoping if you could help me understand the concepts of understanding how to add an image into a canvas on a OnTouchEvent implemented on a View. So far, this is what i've come up with.
parent is the Activity where in this customized view is instantiated and is added into.
#Override
protected void onDraw(Canvas canvas)
{
// TODO Auto-generated method stub
super.onDraw(canvas);
}
public void insertImage()
{
if (parent.selected_icon.contentEquals("image1"))
{
image = getResources().getDrawable(R.drawable.image1);
}
else if (parent.selected_icon.contentEquals("image1"))
{
image = getResources().getDrawable(R.drawable.image2);
}
else if (parent.selected_icon.contentEquals("iamge3"))
{
image = getResources().getDrawable(R.drawable.image3);
}
Rect srcRect = new Rect(0, 0, image.getIntrinsicWidth(),
image.getIntrinsicHeight());
Rect dstRect = new Rect(srcRect);
Bitmap bitmap = Bitmap.createBitmap(image.getIntrinsicWidth(),
image.getIntrinsicHeight(), Bitmap.Config.ALPHA_8);
Canvas canvas = new Canvas();
canvas.drawBitmap(bitmap, srcRect, dstRect, null);
invalidate();
}
When you want to draw over a view, you have to do that in onDraw(), using the Canvas passed there. That Canvas is already bound to the Bitmap that is the actual drawing of your view.
I had to do something similar and my approach was like this:
I had a list of "things to be drawn over the view" as a member of the class.
whenever I added something to that list, I called invalidate(), so that onDraw() would get called.
My onDraw() looked like this:
...
protected void onDraw(Canvas canvas) {
super.onDraw(canvas); // the default drawing
for(ThingToBeDrawn thing : mListOfThingsToBeDrawn) {
thing.drawThing(canvas); // draw each thing over the view
}
}
A Canvas is just a tool used to draw a Bitmap, and it works quite differently than SurfaceView.