Draw a line on ImageView set by Picasso

后端 未结 1 1867
無奈伤痛
無奈伤痛 2021-01-26 18:37

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 p

相关标签:
1条回答
  • 2021-01-26 19:16

    just follow the steps below:

    1. 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!

    0 讨论(0)
提交回复
热议问题