在android中使用Shape来制作圆角
创建名为
roundcorner.xml
的 xml 文件
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android">
<solid android:color="#33DDFF" />
<corners android:radius="4dp" />
</shape>
在您的 ImageButton 中添加此属性
android:background="@drawable/roundcorner"
<ImageButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/imageButton"
android:layout_marginTop="57dp"
android:src="@drawable/friends"
android:background="@drawable/roundcorner"
android:padding="1dp"
android:layout_alignParentTop="true"
android:layout_toLeftOf="@+id/imageButton2"
android:layout_marginRight="62dp" />
您可以使用由形状可绘制的选择器作为背景,例如:
rounded_bg.xml(在 res/drawable-nodpi 文件夹中创建)
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle">
<solid android:color="#ffffff" />
<corners
android:bottomLeftRadius="4dp"
android:bottomRightRadius="4dp"
android:topLeftRadius="4dp"
android:topRightRadius="4dp" />
</shape>
创建另一个文件,更改
solid android:color="#ffffff"
中引用的颜色,例如更改为 solid android:color="#ff0000"
并将该文件命名为 rounded_bg_selected.xml
创建选择器(也在 res/drawable-nodpi 中),将其命名为
selectable_button_bg.xml
:
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_pressed="true"
android:drawable="@drawable/rounded_bg_selected" />
<item android:state_focused="false"
android:drawable="@drawable/rounded_bg" />
</selector>
然后在您的布局中引用它:
<ImageButton
android:background="@drawable/selectable_button_bg"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/imageButton"
android:layout_marginTop="57dp"
android:src="@drawable/friends"
android:padding="1dp"
android:layout_alignParentTop="true"
android:layout_toLeftOf="@+id/imageButton2"
android:layout_marginRight="62dp" />
在 /res/drawable 中创建
image_rounded_corner.xml
<?xml version="1.0" encoding="UTF-8" ?>
<shape xmlns:android="http://schemas.android.com/apk/res/android">
<solid android:color="#000000" />
<stroke android:width="3dp" android:color="#776da8" />
<corners android:bottomRightRadius="5dp" android:bottomLeftRadius="5dp" android:topLeftRadius="5dp" android:topRightRadius="5dp" />
<padding android:left="2dp" android:top="2dp" android:right="2dp" android:bottom="2dp" />
</shape>
使用
android:background
调用 image_rounded_corner.xml 文件
<ImageView android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/myimage"
android:src="@drawable/icon"
android:background="@drawable/image_rounded_corner" />
或使用 绘制 9-patch 文件,如“Artoo Detoo”建议。
使用这个: 把它放在 res/drawable 文件夹中
my_gradient.xml
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android">
<corners android:radius="100dp" />
<stroke
android:width="5dp"
android:color="#090" />
</shape>
在您的 ImageButton 中只需输入:
android:background="@drawable/my_gradient"
public static Bitmap toRoundCorner(Bitmap bitmap, int pixels) {
Bitmap output = Bitmap.createBitmap(bitmap.getWidth(), bitmap.getHeight(), Bitmap.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(PorterDuff.Mode.SRC_IN));
canvas.drawBitmap(bitmap, rect, rect, paint);
return output;
}
这个代码会对你有帮助。(来自中文文章:http://www.cnblogs.com/liuweiming/archive/2012/04/23/2466074.html)
这以编程方式对我有用:
val gradientDrawable = GradientDrawable()
gradientDrawable.shape = GradientDrawable.OVAL
gradientDrawable.cornerRadius = <YOUR CORNER RADIUS HERE>
yourView.clipToOutline = true
yourView.background = gradientDrawable
编辑: 如果不需要 .OVAL,请尝试更改为 .RECTANGLE。