【问题标题】:android: create circular image with picassoandroid:用毕加索创建圆形图像
【发布时间】:2014-11-24 13:39:33
【问题描述】:

有人问过这个问题,并且对我正在使用的毕加索版本做出了承诺:如何使用毕加索将圆形位图发送到 ImageView?我是毕加索的新手,我唯一用过的就是

Picasso.with(context).load(url).resize(w, h).into(imageview);

我已经找到https://gist.github.com/julianshen/5829333,但我不知道如何以一种不尴尬的方式将它与上面的行结合起来。

【问题讨论】:

  • 您提供的链接足以回答您自己的问题。而你只需要申请 Picasso.with(activity).load(mayorShipImageLink).transform(new CircleTransform()).into(ImageView);

标签: android bitmap imageview picasso


【解决方案1】:

请先研究一下,因为有可用的答案。总之,关注This Link,仔细阅读了解如何使用。

试试这个:

import com.squareup.picasso.Transformation;

public class CircleTransform implements Transformation {
    @Override
    public Bitmap transform(Bitmap source) {
        int size = Math.min(source.getWidth(), source.getHeight());

        int x = (source.getWidth() - size) / 2;
        int y = (source.getHeight() - size) / 2;

        Bitmap squaredBitmap = Bitmap.createBitmap(source, x, y, size, size);
        if (squaredBitmap != source) {
            source.recycle();
        }

        Bitmap bitmap = Bitmap.createBitmap(size, size, source.getConfig());

        Canvas canvas = new Canvas(bitmap);
        Paint paint = new Paint();
        BitmapShader shader = new BitmapShader(squaredBitmap,
                Shader.TileMode.CLAMP, Shader.TileMode.CLAMP);
        paint.setShader(shader);
        paint.setAntiAlias(true);

        float r = size / 2f;
        canvas.drawCircle(r, r, r, paint);

        squaredBitmap.recycle();
        return bitmap;
    }

    @Override
    public String key() {
        return "circle";
    }
}

然后简单地应用它:

Picasso.with(activity).load(mayorShipImageLink).transform(new CircleTransform()).into(ImageView);

【讨论】:

  • @anirudh-sharma 确保添加这些导入: import android.graphics.Bitmap;导入 android.graphics.BitmapShader;导入android.graphics.Canvas;导入android.graphics.Paint;导入 com.squareup.picasso.Transformation;
  • @all:如果您在 gif 图像上遇到空指针异常,check this
  • 在 Kotlin 中也能像魅力一样工作。现有 API 的一项更改,BitmapShader.TileMode.CLAMP 不再存在,我已更新答案
  • DOES NOT WORK! Potential Fatal signal 6 (SIGBART),这意味着 bitmap.recycle() 实际上正在从内存中删除引用并且导致signal 6 (SIGABRT), code -6 (SI_TKILL), fault addr -------- 11-12 00:03:47.941 29091 29091 F DEBUG : Abort message: 'Error, cannot access an invalid/free'd bitmap here!' -> 中止消息:'错误,无法在此处访问无效/释放的位图! 因为它是从内存中收集的垃圾。 bitmap.recycle 用于较旧的 Android 设备,这是我的理解。
【解决方案2】:

这是 support-v4 库提供的内容!调查 RoundedBitmapDrawable。无需自己动手:

Picasso.with(context).load(url)
                        .resize(w, h)
                        .into(myImageView, new Callback() {
                            @Override
                            public void onSuccess() {
                                Bitmap imageBitmap = ((BitmapDrawable) myImageView.getDrawable()).getBitmap();
                                RoundedBitmapDrawable imageDrawable = RoundedBitmapDrawableFactory.create(getResources(), imageBitmap);
                                imageDrawable.setCircular(true);
                                imageDrawable.setCornerRadius(Math.max(imageBitmap.getWidth(), imageBitmap.getHeight()) / 2.0f);
                                myImageView.setImageDrawable(imageDrawable);
                            }
                            @Override
                            public void onError() {
                                myImageView.setImageResource(R.drawable.default_image);
                            }
                        });

注意:Picasso 也有一个 .transform(customTransformation) 调用,理论上你可以使用它,但是,我对此有疑问。以上工作。祝你好运!

【讨论】:

  • 这对于方形图像非常有效。在任何图像的角半径中使用 Math.min。 imageDrawable.setCornerRadius(Math.min(imageBitmap.getWidth(), imageBitmap.getHeight()) / 2.0f);
  • 我们是否应该在 OnSuccess() 中创建单独的线程,因为从原始位图创建圆角位图可能是一项很长的任务?
【解决方案3】:

毕加索有一个转换库。

只需添加 gradle 依赖项

implementation 'jp.wasabeef:picasso-transformations:2.2.1'

结束使用

Picasso.with(context)
       .load(url)
       .resize(w, h)
       .transform(new CropCircleTransformation())
       .into(imageview);

维基:Picasso Transformations

【讨论】:

  • 这真是这个问题的完美答案,怎么票数这么少..
【解决方案4】:

我发现的另一个选择是这个家伙库。它可以独立工作,也可以与毕加索结合使用。我选择了毕加索路线,如下图:

https://github.com/vinc3m1/RoundedImageView

Transformation transformation = new RoundedTransformationBuilder()
          .borderColor(Color.BLACK)
          .borderWidthDp(3)
          .cornerRadiusDp(30)
          .oval(false)
          .build();

Picasso.with(context)
    .load(url)
    .fit()
    .transform(transformation)
    .into(imageView);

为我工作!

【讨论】:

    【解决方案5】:

    我已经尝试了上面的所有解决方案,但没有一个可以让我在不裁剪图片的情况下进行圆形变换。这些解决方案仅适用于具有相同宽度和高度的图像。这是我上面的解决方案

    第一 ------

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

    然后这样做--------

    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";
        }
    }
    

    【讨论】:

      【解决方案6】:

      使用这个库来创建一个圆形的图像视图。 To make a circular ImageView, add this CircularImageView library to your project 并在您的布局 XML 中添加 CircularImageView

      <com.pkmmte.view.CircularImageView
              android:layout_width="250dp"
              android:layout_height="250dp"
              android:src="@drawable/image"
              app:border_color="#EEEEEE"
              app:border_width="4dp"
              app:shadow="true" />`
      

      然后使用 picasso 将所需的图像加载到这个 imageView 中。 Picasso 做了所有你不需要担心的缓存

      【讨论】:

        【解决方案7】:

        在下面的代码中包含类型为 Layer-list 的 xml 可绘制对象

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

        然后在android.src中使用xml到你的ImageView

         <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"/>
        

        【讨论】:

          【解决方案8】:

          这个对我有用

          <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

          【讨论】:

            【解决方案9】:

            这个正在使用当前的 Picasso 3 快照:

            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()"
              }
            }
            

            毕加索3要点:https://gist.github.com/G00fY2/f3fbc468570024930c1fd9eb4cec85a1

            【讨论】:

              【解决方案10】:

              Picasso v2.71828 对我有用

              class CircleTransform : Transformation {
              override fun transform(source: Bitmap?): Bitmap? {
                  if (source == null) {
                      return source
                  }
              
                  var bitmap: Bitmap
              
                  // since we cant transform hardware bitmaps create a software copy first
                  if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O && source.config == Bitmap.Config.HARDWARE) {
                      val softwareCopy = source.copy(Bitmap.Config.ARGB_8888, true)
                      if (softwareCopy == null) {
                          return source
                      } else {
                          bitmap = softwareCopy
                          source.recycle()
                      }
                  } else {
                      bitmap = source
                  }
              
                  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, Bitmap.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 circleBitmap
              }
              
              
              override fun key(): String {
                  return "circleTransformation()"
              }
              

              }

              【讨论】:

                猜你喜欢
                • 2018-03-02
                • 2017-05-17
                • 1970-01-01
                • 2019-05-13
                • 1970-01-01
                • 1970-01-01
                • 1970-01-01
                • 1970-01-01
                • 1970-01-01
                相关资源
                最近更新 更多