简体   繁体   English

圆角android图像按钮

[英]Rounded corners android image buttons

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

Use Shape in android to make the rounder corners 在Android中使用Shape制作圆角

create the xml file named it as roundcorner.xml 创建一个名为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>

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

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

Create image_rounded_corner.xml inside /res/drawable 在/ 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>

Call the image_rounded_corner.xml file with android:background 使用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" /> 

or use an Draw 9-patch file as "Artoo Detoo" suggested. 或使用Draw 9-patch文件作为“ Artoo Detoo”建议。

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM