问题描述
我试图找出如何简单地画一条线,在毕加索被设置在图像上。我发现,如果我只是设置图像,给定一个URI,与毕加索并尝试使用以下绘制油漆它:
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);
然后我得到一个崩溃说,位图必须是可变的第一位。所以我说这上面code:
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);
,然后创建新的Canvas(mutableBitmap)
,而不是在画布上。这样取出的崩溃,但是什么都没有绘制。我相信这是因为我的毕加索之前设置的图像,所以现在我需要用这种新的可变位复位毕加索。问题是这样的code是在的onSuccess()回调毕加索。我能做些什么,以允许通过毕加索的图像上绘制的画?
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,添加),以RequestCreator与Picasso.load(创建的转换功能,如下图所示:
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!
这篇关于借鉴ImageView的一条线毕加索集的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!