圆角android图像按钮

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/21633637/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-20 04:50:57  来源:igfitidea点击:

Rounded corners android image buttons

androidimagebutton

提问by CBreeze

I am trying to round corners on an android ImageButton, the code looks like this;

我正在尝试在 android ImageButton 上圆角,代码如下所示;

<?xml version="1.0" encoding="utf-8"?>

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical" android:layout_width="match_parent"
    android:layout_height="match_parent">

    <RelativeLayout 
        android:layout_width="fill_parent"
        android:layout_height="fill_parent">

        <ImageButton
            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" />

    </RelativeLayout>
</LinearLayout>

Basically our output is an ImageButton but it has squared corners, we are trying to round off the corners.

基本上我们的输出是一个 ImageButton 但它有方角,我们试图把角圆角。

Thanks

谢谢

回答by Nambi

Use Shapein android to make the rounder corners

在android中使用Shape制作圆角

create the xml file named it as roundcorner.xml

创建名为的xml文件 roundcorner.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>

In your ImageButton add this attribute android:background="@drawable/roundcorner"

在你的 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" />

回答by 2Dee

You could use a selector made of shape drawables as background, for example :

您可以使用由形状可绘制对象组成的选择器作为背景,例如:

rounded_bg.xml (to be created in res/drawable-nodpi folder)

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>

Create another one, changing the color referenced in solid android:color="#ffffff", for example to solid android:color="#ff0000"and name that file rounded_bg_selected.xml

创建另一个,更改 中引用的颜色solid android:color="#ffffff",例如solid android:color="#ff0000"并命名该文件rounded_bg_selected.xml

Create the selector (also in res/drawable-nodpi), name it selectable_button_bg.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>

Then reference it in your layout :

然后在您的布局中引用它:

<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" />

回答by Jorgesys

Create image_rounded_corner.xmlinside /res/drawable

image_rounded_corner.xml在 /res/drawable 内部创建

<?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>

Call the image_rounded_corner.xml file with android:background

调用 image_rounded_corner.xml 文件 android:background

<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" /> 

or use an Draw 9-patchfile as "Artoo Detoo" suggested.

或使用“Artoo Detoo”建议的Draw 9-patch文件。

回答by Sagar Shah

Use this: Put this in res/drawable folder

使用这个:把它放在 res/drawable 文件夹中

my_gradient.xml

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>

In your ImageButton Just put:

在您的 ImageButton 中只需输入:

android:background="@drawable/my_gradient"

回答by user3682351

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

this code will help you.(From a chinese article:http://www.cnblogs.com/liuweiming/archive/2012/04/23/2466074.html)

此代码将帮助您。(来自中文文章:http: //www.cnblogs.com/liuweiming/archive/2012/04/23/2466074.html