簡體   English   中英

ImageView子類制作右圓角

[英]ImageView subclass to make Rounded Right Corner

我正在嘗試拍攝這樣的圖像

在此處輸入圖片說明在此處輸入圖片說明

並將它們變成圖像的右上角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;
}

恕我直言,這是最簡單的方法,您可以完全自定義類以實現目標。 https://github.com/memoryleak/Android-RoundedImageView

檢查此鏈接 它具有與您正在尋找的答案相似的許多答案,可能會對您有所幫助。 快樂的編碼。 :)

下一個庫怎么樣:

https://github.com/vinc3m1/RoundedImageView

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM