【问题标题】:ImageView subclass to make Rounded Right CornerImageView 子类制作圆角右角
【发布时间】:2014-11-07 06:55:30
【问题描述】:

我正在尝试拍摄这样的图像

并将它们变成图像的右上角 1/4。下图是我需要的示例。

**更新:**

我让它(有点)工作,但是缩放图像会产生不可接受的失真量,我的代码似乎非常低效。关于如何改进此代码或可能的更好方法的任何想法?

我正在继承 Volley 的 NetworkImageView。

这是我的代码

<com.RoundedNetworkImageView
    android:id="@+id/smallProductImage"
    android:layout_width="56dp"
    android:layout_height="56dp"
    android:layout_centerVertical="true"
    android:layout_margin="5dp"
    android:adjustViewBounds="true"
    android:background="@android:color/white "
    android:scaleType="fitCenter" />

public static Bitmap getCroppedBitmap(Bitmap bmp, int radius) {
    Bitmap sbmp;

    if (bmp.getWidth() != radius || bmp.getHeight() != radius) {
        float smallest = Math.min(bmp.getWidth(), bmp.getHeight());
        float factor = smallest / radius;
        sbmp = Bitmap.createScaledBitmap(bmp, (int) (bmp.getWidth() / factor), (int) (bmp.getHeight() / factor),
                false);
    } else {
        sbmp = bmp;
    }

    Bitmap output = Bitmap.createBitmap(radius, radius, Config.ARGB_8888);
    Canvas canvas = new Canvas(output);

    final int color = 0xffa19774;
    final Paint paint = new Paint();
    final Rect rect = new Rect(0, 0, radius, radius);

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

    Bitmap bitmap = Bitmap.createBitmap(output, output.getWidth() / 2, 0, output.getWidth() / 2,
            output.getHeight() / 2);
    Bitmap scaledBmp = Bitmap.createScaledBitmap(bitmap, radius, radius, true);

    return scaledBmp;
}

【问题讨论】:

  • 为什么要子类化?您可以通过 XML 实现相同的目标
  • 如何使用 xml 裁剪和舍入图像?我不想简单地画一个圆圈。

标签: java android bitmap imageview android-volley


【解决方案1】:

恕我直言,这是最简单的方法,您可以完全自定义课程以实现您的目标。 https://github.com/memoryleak/Android-RoundedImageView

【讨论】:

    猜你喜欢
    • 2011-01-28
    • 1970-01-01
    • 2017-07-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-09-13
    • 2020-09-07
    相关资源
    最近更新 更多