簡體   English   中英

當按鈕具有背景圖像時,圓角不起作用

[英]Rounded corners not working when button has background image

我的活動中有一個按鈕,它也有一個背景圖像。我已經添加了選擇器屬性,它適用於set_pressed和set_focused.But圓角不會出現在按鈕的默認狀態,因為我已經插入了背景圖像also.pls幫我...

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>
  • 添加布局相同大小的按鈕

  • 提供背景圖像並設置圓角xml作為按鈕的背景..如果它工作接受答案。


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

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

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

它將作為按鈕的默認樣式。

添加另一個項目以顯示默認狀態,如下所示:

<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" />

我寧願在代碼中嘗試這樣的東西(尚未檢查):

    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個不同的半徑(左上角,右上角,左下角,下邊角色)

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM