【问题标题】:Draw a line on ImageView set by Picasso在毕加索设置的 ImageView 上画一条线
【发布时间】:2014-11-22 19:27:24
【问题描述】:

我试图弄清楚如何在 Picasso 中设置的图像上简单地画一条线。我发现,如果我只是简单地使用 Picasso 设置图像,给定一个 URI,并尝试使用以下方法对其进行绘制:

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);

然后我得到一个崩溃,说位图需要首先是可变的。所以我在该代码上方添加了这个:

Bitmap workingBitmap = ((BitmapDrawable) image.getDrawable()).getBitmap();
Bitmap mutableBitmap = workingBitmap.copy(Bitmap.Config.ARGB_8888, true);

然后使用new Canvas(mutableBitmap) 创建画布。这消除了崩溃,但是没有绘制任何内容。我相信这是因为我的毕加索之前设置了图像,所以现在我需要用这个新的可变位图重置毕加索。问题是这段代码在 Picasso 的 onSuccess() 回调中。我该怎么做才能通过 Picasso 在图像上绘制 Paint?

【问题讨论】:

    标签: android image android-canvas paint picasso


    【解决方案1】:

    只需按照以下步骤操作:

    1. 编写您自己的类扩展类转换,如下所示:

       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、将 Transformation 添加到使用 Picasso.load() 函数创建的 RequestCreator 中,如下所示:

      Picasso picasso = Picasso.with(getApplicationContext());
      DrawLineTransformation myTransformation = new DrawLineTransformation();
      picasso.load("http://www.baidu.com/img/bdlogo.png").transform(myTransformation).into(imageview);
      

    这就是您需要做的所有步骤,尽情享受吧!

    【讨论】:

      猜你喜欢
      • 2021-04-22
      • 1970-01-01
      • 1970-01-01
      • 2021-08-09
      • 1970-01-01
      • 1970-01-01
      • 2017-08-20
      • 2018-01-21
      相关资源
      最近更新 更多