android: create circular image with picasso

后端 未结 10 799
天命终不由人
天命终不由人 2020-11-29 18:31

The question had been asked and there had been a promise made for the very version of Picasso that I am using: How do I send a circular bitmap to an ImageView using Picasso?

相关标签:
10条回答
  • 2020-11-29 18:50

    This one is working with the current Picasso 3 snapshot:

    class CircleTransformation : Transformation {
    
      override fun transform(source: RequestHandler.Result): RequestHandler.Result {
        if (source.bitmap == null) {
          return source
        }
    
        var bitmap: Bitmap
    
        // since we cant transform hardware bitmaps create a software copy first
        if (VERSION.SDK_INT >= VERSION_CODES.O && source.bitmap!!.config == Config.HARDWARE) {
          val softwareCopy = source.bitmap!!.copy(Config.ARGB_8888, true)
          if (softwareCopy == null) {
            return source
          } else {
            bitmap = softwareCopy
            source.bitmap!!.recycle()
          }
        } else {
          bitmap = source.bitmap!!
        }
    
        var size = bitmap.width
        // if bitmap is non-square first create square one
        if (size != bitmap.height) {
          var sizeX = size
          var sizeY = bitmap.height
          size = Math.min(sizeY, sizeX)
          sizeX = (sizeX - size) / 2
          sizeY = (sizeY - size) / 2
    
          val squareSource = Bitmap.createBitmap(bitmap, sizeX, sizeY, size, size)
          bitmap.recycle()
          bitmap = squareSource
        }
    
        val circleBitmap = Bitmap.createBitmap(size, size, Config.ARGB_8888)
        val canvas = Canvas(circleBitmap)
        val paint = Paint()
        val shader = BitmapShader(bitmap, Shader.TileMode.CLAMP, Shader.TileMode.CLAMP)
    
        paint.shader = shader
        paint.isAntiAlias = true
        val centerAndRadius = size / 2f
        canvas.drawCircle(centerAndRadius, centerAndRadius, centerAndRadius, paint)
    
        bitmap.recycle()
        return RequestHandler.Result(circleBitmap, source.loadedFrom, source.exifRotation)
      }
    
      override fun key(): String {
        return "circleTransformation()"
      }
    }
    

    Picasso3 gist: https://gist.github.com/G00fY2/f3fbc468570024930c1fd9eb4cec85a1

    0 讨论(0)
  • 2020-11-29 18:53

    I have tried all solutions above but none of them gives me circle transform without cropping picture..those solution will work only for images with same width and height..this is my solution on above

    first ------

    Picasso.with(getActivity())
                .load(url)
                .error(R.drawable.image2)
                .placeholder(R.drawable.ic_drawer)
                .resize(200, 200)
                .transform(new ImageTrans_CircleTransform())
                .into(imageView1);
    

    then do this --------

    import android.graphics.Bitmap;
    import android.graphics.BitmapShader;
    import android.graphics.Canvas;
    import android.graphics.Color;
    import android.graphics.Paint;
    import android.graphics.Shader.TileMode;
    
    import com.squareup.picasso.Transformation;
    public class ImageTrans_CircleTransform implements Transformation {
     @Override
        public Bitmap transform(Bitmap source) {
     if (source == null || source.isRecycled()) {
                    return null;
                }
    
                final int width = source.getWidth() + borderwidth;
                final int height = source.getHeight() + borderwidth;
    
                Bitmap canvasBitmap = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888);
                BitmapShader shader = new BitmapShader(source, TileMode.CLAMP, TileMode.CLAMP);
                Paint paint = new Paint();
                paint.setAntiAlias(true);
                paint.setShader(shader);
    
                Canvas canvas = new Canvas(canvasBitmap);
                float radius = width > height ? ((float) height) / 2f : ((float) width) / 2f;
                canvas.drawCircle(width / 2, height / 2, radius, paint);
    
                //border code
                paint.setShader(null);
                paint.setStyle(Paint.Style.STROKE);
                paint.setColor(bordercolor);
                paint.setStrokeWidth(borderwidth);
                canvas.drawCircle(width / 2, height / 2, radius - borderwidth / 2, paint);
                //--------------------------------------
    
                if (canvasBitmap != source) {
                    source.recycle();
                }
    
                return canvasBitmap;
    }
     @Override
        public String key() {
            return "circle";
        }
    }
    
    0 讨论(0)
  • 2020-11-29 19:00

    This one worked for me

    <com.androidhub4you.crop.RoundedImageView
            android:id="@+id/imageView_round"
            android:layout_width="100dp"
            android:layout_height="100dp"
            android:layout_marginTop="15dp"
            android:src="@drawable/ic_launcher" />
    

    http://www.androidhub4you.com/2014/10/android-custom-shape-imageview-rounded.html

    0 讨论(0)
  • 2020-11-29 19:01

    Include the xml drawable of type Layer- list with the code below

    <layer-list xmlns:android="http://schemas.android.com/apk/res/android">
        <item android:id="@+id/shape_status">
            <shape android:shape="oval">
                <solid android:color="@android:color/black"/>
            </shape>
        </item>
    <item android:drawable="@drawable/ic_status_content"/></layer-list>
    

    then use the xml to your ImageView in the android.src

     <ImageView
                android:id="@+id/iconStatus"
                android:layout_width="55dp"
                android:layout_height="55dp"
                android:layout_gravity="right"
                android:src="@drawable/ic_circle_status"
                android:layout_alignParentTop="true"
                android:layout_alignParentEnd="true"/>
    
    0 讨论(0)
提交回复
热议问题