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

我有下一个XML drawable blue_button

我有图像button_blue_bg与渐变宽度为1px.

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

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

我如何需要modificate xml背景渐变图像不在边界外?

我明白为什么它会发生,因为我使用图层 – 所以这就像三明治 – 但我也在目标c上编程,并且它也使用图层.但在苹果公司,它很好.

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

我用这个函数制作圆角:

    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,bitmap.getWidth(),bitmap.getHeight());
        final RectF rectF = new RectF(rect);
        final float roundPx = 12;

        paint.setAntiAlias(true);
        canvas.drawARGB(0,0);
        paint.setColor(color);
        canvas.drawRoundRect(rectF,roundPx,paint);

        paint.setXfermode(new PorterDuffXfermode(Mode.SRC_IN));
        canvas.drawBitmap(bitmap,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);

}

相关文章

Android性能优化——之控件的优化 前面讲了图像的优化,接下...
前言 上一篇已经讲了如何实现textView中粗字体效果,里面主要...
最近项目重构,涉及到了数据库和文件下载,发现GreenDao这个...
WebView加载页面的两种方式 一、加载网络页面 加载网络页面,...
给APP全局设置字体主要分为两个方面来介绍 一、给原生界面设...
前言 最近UI大牛出了一版新的效果图,按照IOS的效果做的,页...