簡體   English   中英

Android中Imageview的圓角

[英]Rounded corners for an Imageview in android

我在linearlayout中有一個textview和imageview。 Textview位於頂部,imageview位於底部。 我使用下面的線條為linearlayout設置了圓角。 但是imageview角落並沒有四舍五入。 我看到只有linearlayout的頂角是圓形的。 我如何才能獲得imageview的圓角? (如果刪除imageview,我看到所有的角都是圓角的)

rounded_corners.xml

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle" >

<solid android:color="#ffffff" />


<corners
    android:bottomLeftRadius="8dp"
    android:bottomRightRadius="8dp"
    android:topLeftRadius="8dp"
    android:topRightRadius="8dp" />

</shape>

main.xml中

  <LinearLayout
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:layout_marginBottom="50dp"
    android:layout_marginLeft="10dp"
    android:layout_marginRight="10dp"
    android:layout_marginTop="50dp"
    android:background="@xml/rounded_corners"
    android:orientation="vertical" >

    <TextView
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="xxxxxxxx" />

    <ImageView     
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:scaleType="centerCrop"
        android:src="@drawable/my_image_view" />
</LinearLayout>

屏幕截圖: 在此輸入圖像描述

您可以將圖像的左下角和右下角四舍五入,如下所示:

在此輸入圖像描述

碼:

public static Bitmap getRoundCornerBitmap(Bitmap bitmap, int radius) {
    int w = bitmap.getWidth();
    int h = bitmap.getHeight();
    Bitmap output = Bitmap.createBitmap(w, h, Bitmap.Config.ARGB_8888);
    Canvas canvas = new Canvas(output);

    final Paint paint = new Paint(Paint.ANTI_ALIAS_FLAG);
    final RectF rectF = new RectF(0, 0, w, h);

    canvas.drawRoundRect(rectF, radius, radius, paint);

    paint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.SRC_IN));
    canvas.drawBitmap(bitmap, null, rectF, paint);

    /**
     * here to define your corners, this is for left bottom and right bottom corners
     */
    final Rect clipRect = new Rect(0, 0, w, h - radius);
    paint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.SRC));
    canvas.drawRect(clipRect, paint);

    paint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.SRC_IN));
    canvas.drawBitmap(bitmap, null, rectF, paint);

    bitmap.recycle();

    return output;
}

此方法可以為您提供左下角和右下角圓角的圖像。

要獲得imageview的圓角,請將圖像轉換為位圖,然后嘗試以下代碼:

public static Bitmap roundCorner(Bitmap src, float round) 
{
    // image size
    int width = src.getWidth();
    int height = src.getHeight();

    // create bitmap output
    Bitmap result = Bitmap.createBitmap(width, height, Config.ARGB_8888);

    // set canvas for painting
    Canvas canvas = new Canvas(result);
    canvas.drawARGB(0, 0, 0, 0);

    // config paint
    final Paint paint = new Paint();
    paint.setAntiAlias(true);
    paint.setColor(Color.BLACK);

    // config rectangle for embedding
    final Rect rect = new Rect(0, 0, width, height);
    final RectF rectF = new RectF(rect);

    // draw rect to canvas
    canvas.drawRoundRect(rectF, round, round, paint);

    // create Xfer mode
    paint.setXfermode(new PorterDuffXfermode(Mode.SRC_IN));
    // draw source image to canvas
    canvas.drawBitmap(src, rect, rect, paint);

    // return final image
     return result;
}

你的線性布局是圓角,毫無疑問,但你的形象不是。 在屏幕截圖中,圖像與底部的線性布局重疊。 嘗試在線性布局中添加一些填充( android:padding="20dp" )。 這應該工作。

暫無
暫無

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

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