【问题标题】:Android XML drawable rounded corners with bitmap tag带有位图标记的 Android XML 可绘制圆角
【发布时间】:2012-08-21 10:54:16
【问题描述】:

我有下一个 XML 可绘制蓝色按钮

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

我有一个带有 1px 宽度渐变的图片 button_blue_bg。

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

如您所见,我的背景没有被圆形边框剪裁。

我如何需要修改 xml 以使背景渐变图像不在边框之外?

我明白为什么会这样,因为我使用了层——所以这就像三明治——但我也在目标 c 上编程,而且它也使用了层。但在 Apple 中,这还不错。

【问题讨论】:

    标签: android xml xml-drawable


    【解决方案1】:

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

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

    【讨论】:

      【解决方案2】:

      9-patch 非常适合您的情况

      【讨论】:

      • 不,不是。由于很多原因,例如:然后我无法设置可变角的角;我也在另外两个地方使用这个 bg_img,所以如果我使用 9-patch - 我每次都需要使用更圆的控制,但我有一些地方我不需要圆角但我需要这个 bg;还有图片的大小和数量。
      猜你喜欢
      • 1970-01-01
      • 2012-02-14
      • 2014-09-02
      • 1970-01-01
      • 1970-01-01
      • 2020-04-28
      • 1970-01-01
      • 2013-04-01
      • 2021-11-20
      相关资源
      最近更新 更多