带有位图标记的Android XML可绘制圆角

时间:2012-08-26 20:40:38

标签: android xml xml-drawable

我有下一个XML drawable blue_button

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
  <item>
    <layer-list>
      <item><bitmap android:src="@drawable/button_blue_bg" />
      </item>
      <item >
         <shape>
                <corners android:radius="50dip" />
                <stroke android:width="1dip" android:color="#ccffffff" />
                <solid android:color="#00000000" />
                <padding android:bottom="3dip" 
                         android:left="3dip"
                         android:right="3dip"
                         android:top="3dip" />
         </shape>
       </item>
     </layer-list>
  </item>
</selector>

我有图像button_blue_bg,渐变宽度为1px。

当我设置按钮背景时,我会得到下一张图像

enter image description here

如你所见,我的背景没有修剪圆角边框。

我如何为背景渐变图像提供修改xml不在边界外?

我理解为什么它会发生,因为我使用图层 - 所以这就像夹心 - 但我也在目标c上编程,并且它也使用图层。但在苹果公司,它很好。

2 个答案:

答案 0 :(得分:4)

我使用此....首先是一个基本定义的图层,将此图层设置为布局的背景:

<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
  <item>
    <shape xmlns:android="http://schemas.android.com/apk/res/android" android:shape="rectangle">
        <stroke android:width="0.2dp" android:color="@color/anycolor" />
        <corners android:radius="10dp" > </corners> 
    </shape>
  </item>
  <item>
      <bitmap android:src="@drawable/imgback" />
  </item>
 </layer-list>

我使用此功能制作圆角:

    public static Bitmap getRoundedCornerBitmap(Bitmap bitmap) {

        Bitmap output = Bitmap.createBitmap(bitmap.getWidth(),
            bitmap.getHeight(), 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 = 12;

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

        return output ;
      }     

最后,活动中的代码:

public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.menu);
    // i use a Relative Layout  
    RelativeLayout rl = (RelativeLayout) findViewById(R.id.rlmenu);
    // Obtain then backgroud of RelativeLayout
    LayerDrawable layer = (LayerDrawable) rl.getBackground();
    // obtain the image set in the Layer
    BitmapDrawable bg = (BitmapDrawable) layer.getDrawable(1);
    // create a new BitmapDrawable from the function
    Drawable d =new BitmapDrawable(getRoundedCornerBitmap(bg.getBitmap()));
    // set the new roundcorner image in the layer
    layer.setDrawableByLayerId(1, d);
    rl.setBackgroundDrawable(d);

}

答案 1 :(得分:0)

9-patch对您的情况非常适合