当按钮具有背景图像时,圆角不起作用

时间:2013-10-31 06:05:52

标签: android button

我的活动中有一个按钮,它也有一个背景图片。我添加了选择器属性,它适用于set_pressed和set_focused.But圆角不是默认的按钮状态,因为我已插入一个背景图片也。帮助我...

activity_sam.xml

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

  <item    android:state_pressed="true">
    <shape  >

    <solid android:color="#f27d0f"/>
    <corners  android:radius="7dp"/>
    </shape> 
  </item>

  <item    android:state_focused="true">
    <shape  >

    <solid android:color="#f27d0f"/>
    <corners android:radius="7dp"/>
    </shape> 
  </item>

  <item  android:state_focused="false" 
      android:state_enabled="true" 
      android:drawable="@drawable/sam_logo" >
    <shape  >    
    <corners  android:radius="7dp"/>
    </shape> 
  </item>

</selector>

4 个答案:

答案 0 :(得分:3)

  • 添加相同尺寸的按钮

  • 提供背景图片并设置圆角xml作为Button的背景..如果它可以接受答案。


<LinearLayout
     android:layout_width="wrap_content"
     android:layout_height="wrap_content"
     android:background="@drawable/ic_launcher"
     android:orientation="vertical" >

    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:background="@drawable/top_right_left_coner"
        android:text="@string/hello_world" />
</LinearLayout>

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

    <solid android:color="@android:color/transparent" />

    <stroke
        android:width="1dp"
        android:color="@android:color/black" />

    <padding
        android:bottom="1dp"
        android:left="1dp"
        android:right="1dp"
        android:top="1dp" />

    <corners
        android:bottomLeftRadius="10dip"
        android:bottomRightRadius="10dip"
        android:topLeftRadius="10dip"
        android:topRightRadius="10dip" />        
</shape>

答案 1 :(得分:0)

在最后一项下面添加此项。

 <item>
    <shape>    
    <corners  android:radius="7dp"/>
    </shape> 
  </item>

它将作为按钮的默认样式。

答案 2 :(得分:0)

添加另一项以显示默认状态,如下所示:

<shape>    
<corners  android:radius="7dp"/>
</shape> 

如果要以编程方式显示圆角,请执行以下操作:

  public Drawable getRoundedBitmap(Bitmap bitmap, float Rnd_px) {

    Bitmap output = Bitmap.createBitmap(bitmap.getWidth(),
            bitmap.getHeight(), Config.ARGB_8888);
    Canvas canvas = new Canvas(output);

    final int color = 0xff151515;
    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 = Rnd_px;

    paint.setAlpha(50);
    paint.setAntiAlias(true);
    canvas.drawARGB(0, 0, 0, 0);
    paint.setColor(color);
    canvas.drawRoundRect(rectF, roundPx, roundPx, paint);
    paint.setXfermode(new PorterDuffXfermode(Mode.SRC_IN));
    canvas.drawBitmap(bitmap, rect, rect, paint);
    Drawable image = new BitmapDrawable(output);
    return image;
}

以上代码将裁剪图像的边缘。如果要在图像视图上显示圆角图层。请执行以下操作。

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

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

<stroke
    android:width="3dp"
    android:color="#0000CC" />

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

<padding
    android:bottom="6dp"
    android:left="6dp"
    android:right="6dp"
    android:top="6dp" />

答案 3 :(得分:0)

我宁愿在代码中尝试这样的东西(尚未检查):

    public class MyDrawable extends PaintDrawable {
    BitmapShader mShader;
Rect mRect = new Rect();
        public MyDrawable(BitmapDrawable bitmapDrawable) {
        super();


        final BitmapShader mShader = new BitmapShader(bitmapDrawable.getBitmap(), bitmapDrawable.getTileModeX() == null ? Shader.TileMode.CLAMP : bitmapDrawable.getTileModeX(), bitmapDrawable.getTileModeY() == null ? Shader.TileMode.CLAMP : bitmapDrawable.getTileModeY());
        mPaint = new Paint();
        mPaint.setAntiAlias(true);
        mPaint.setFilterBitmap(true);
        mPaint.setDither(true);
        mPaint.setStyle(Paint.Style.FILL);
        mPaint.setShader(mShader);
    }


    @Override
    public void draw(Canvas canvas) {
        int saveCount = canvas.getSaveCount();
        canvas.save();
        getPadding(mRect);
        canvas.translate(mRect.left, mRect.top);
        getShape().draw(canvas, mPaint);
        canvas.translate(-mRect.left, -mRect.top);
        canvas.restoreToCount(saveCount);

    }


}

多亏了这个你可以使用MyDrawable.setCornerRadii(float []半径),并且感谢这个在rect中绘制的图像有4个不同的半径(左上角,右上角,左下角,左下角)