Android的XML绘制圆角处理位图标签 [英] Android XML drawable rounded corners with bitmap tag

查看:160
本文介绍了Android的XML绘制圆角处理位图标签的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有下一个XML绘制blue_button

I have next 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的宽度渐变。

And i have image button_blue_bg with gradient with 1px width.

当我设置按钮的背景,我得到下一个图像

When i set button background i get next image

正如你可以看到我有背景不会被裁减与圆润的边框。

As you can see i have background not clipped with rounded border.

我是如何modificate XML的背景渐变的图像不外边框?

How i need modificate xml for background gradient image be not outside border?

我明白为什么happended,因为我使用层 - 所以这就像三明治,但我也对编程客观C,而且它也使用层。但苹果是wotks罚款。

I understand why it happended, because i use layers - so this like sandwich- but i also programming on objective c, and there it's also use layers. But in Apple it's wotks fine.

推荐答案

我用这个....首先一个层的基本定义,设置此层布局的backgound:

i use this.... First a Layer for the basic definition, set this layer as backgound of your layout:

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

我使用该功能使圆角:

i use this function for making round corners:

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

和finaly中,code。在活动:

and finaly, the code in the activity:

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

}

这篇关于Android的XML绘制圆角处理位图标签的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆