ImageView子类使圆角右角

时间:2014-02-11 17:53:52

标签: java android bitmap imageview android-volley

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

enter image description here enter image description here

并将它们转到图像的右上角1/4处。下图是我需要的一个例子。

enter image description here

**更新:**

我(有点)工作,但缩放图像会产生令人无法接受的失真,我的代码看起来非常低效。关于如何改进此代码或可能更好的方法的任何想法?

enter image description here

我是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;
}

3 个答案:

答案 0 :(得分:0)

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

答案 1 :(得分:0)

检查link。它有很多类似于您正在寻找的答案,可能会对您有所帮助。快乐的编码。 :)

答案 2 :(得分:0)

下一个图书馆怎么样:

https://github.com/vinc3m1/RoundedImageView