【问题标题】:How to convert ImageView into Rounded Shape?如何将 ImageView 转换为圆形?
【发布时间】:2016-04-26 17:52:08
【问题描述】:

我正在使用以下代码将 ImageView 转换为圆形 iage,但在 "Mode.SRC_IN" 处出现错误。需要帮忙。提前谢谢你。

public static Bitmap getRoundedCornerBitmap(Bitmap bitmap, int pixels) {
        Bitmap output = Bitmap.createBitmap(bitmap.getWidth(), bitmap
                .getHeight(), Config.ARGB_8888);
        Canvas canvas = new Canvas(output);

        final int color = 0xff424242;
        final Paint paint = new Paint();
        final Rect rect = new Rect(0, 0, bitmap.getWidth(), bitmap.getHeight());
        final RectF rectF = new RectF(rect);
        final float roundPx = pixels;

        paint.setAntiAlias(true);
        canvas.drawARGB(0, 0, 0, 0);
        paint.setColor(color);
        canvas.drawRoundRect(rectF, roundPx, roundPx, paint);

        paint.setXfermode(new PorterDuffXfermode(Mode.SRC_IN));
        canvas.drawBitmap(bitmap, rect, rect, paint);

        return output;
    } 

【问题讨论】:

  • 您在 Mode.SRC_IN 遇到了什么错误?
  • 检查导入,我认为你需要导入 import android.graphics.PorterDuff.Mode; 而不是 import android.graphics.AvoidXfermode.Mode;
  • @user3387867 在这里查看我的答案stackoverflow.com/questions/22200614/…
  • @pskink 我给出了相同的答案,但有人给了我的答案-1。
  • @KishanDhamat 你是什么意思?

标签: android


【解决方案1】:

使用这个类:

public class RoundedImageView extends ImageView {
private Paint objPaint = new Paint();

public RoundedImageView(Context context) {
    super(context);
    // TODO Auto-generated constructor stub
}

public RoundedImageView(Context context, AttributeSet attrs) {
    super(context, attrs);
}

public RoundedImageView(Context context, AttributeSet attrs, int defStyle) {
    super(context, attrs, defStyle);
}

@Override
protected void onDraw(Canvas canvas) {

    Drawable drawable = getDrawable();

    if (drawable == null) {
        return;
    }

    if (getWidth() == 0 || getHeight() == 0) {
        return;
    }
    Bitmap b = ((BitmapDrawable) drawable).getBitmap();
    Bitmap bitmap = b.copy(Bitmap.Config.ARGB_8888, true);

    int w = getWidth(), h = getHeight();
    Log.i("TAG", "Bitmap Width:" + w);

    Bitmap roundBitmap = getCroppedBitmap(bitmap, w);
    objPaint.setAntiAlias(true);
    objPaint.setDither(true);
    canvas.drawBitmap(roundBitmap, 0, 0, objPaint);

}

public static Bitmap getCroppedBitmap(Bitmap bmp, int radius) {
    Bitmap sbmp;
    if (bmp.getWidth() != radius || bmp.getHeight() != radius)
        sbmp = Bitmap.createScaledBitmap(bmp, radius, radius, false);
    else
        sbmp = bmp;
    Bitmap output = Bitmap.createBitmap(sbmp.getWidth(), sbmp.getHeight(),
            Config.ARGB_8888);
    Canvas canvas = new Canvas(output);

    final int color = 0xffa19774;
    final Paint paint = new Paint();
    final Rect rect = new Rect(0, 0, sbmp.getWidth(), sbmp.getHeight());

    paint.setAntiAlias(true);
    paint.setFilterBitmap(true);
    paint.setDither(true);
    canvas.drawARGB(0, 0, 0, 0);
    paint.setColor(Color.parseColor("#BAB399"));
    canvas.drawCircle(sbmp.getWidth() / 2 + 0.7f,
            sbmp.getHeight() / 2 + 0.7f, sbmp.getWidth() / 2 + 0.1f, paint);
    paint.setXfermode(new PorterDuffXfermode(Mode.SRC_IN));
    canvas.drawBitmap(sbmp, rect, rect, paint);

    return output;
}

}

现在在 xml 文件中:

       <path to your class
        android:id="@+id/iv_leaderboard_profile_icon"
        android:layout_width="@dimen/lederboard_image_size"
        android:layout_height="@dimen/lederboard_image_size"
        android:src="@drawable/ic_launcher" />

这里

  path= com.example.view.RoundedImageView 

类似的东西。

希望你能理解,谢谢。

【讨论】:

    【解决方案2】:

    Romain guy 有一篇关于在 ImageViews 上实现圆角的精彩博文here

    【讨论】:

      【解决方案3】:

      对于 PorterDuffXfermode,你必须写 import android.graphics.PorterDuffXfermode;

      对于 Config.ARGB_8888,你必须写 import android.graphics.Bitmap.Config;

      否则直接按 CTRL + SHIFT + O 组织导入。

      【讨论】:

        【解决方案4】:

        android.support.v4 中引入了一个新类。它为您提供任何自定义的圆圈图像。

        RoundedBitmapDrawable roundedBitmapDrawable = RoundedBitmapDrawableFactory.create(mContext.getResources(), bitmap);
        roundedBitmapDrawable.setCornerRadius(pixels);
        imageView.setImageDrawable(roundedBitmapDrawable);
        

        我会推荐使用它。

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 2021-11-19
          • 1970-01-01
          • 1970-01-01
          • 2017-02-06
          • 1970-01-01
          • 1970-01-01
          • 2018-10-11
          相关资源
          最近更新 更多