2

我的活动中有一个按钮,它也有一个背景图像。我添加了选择器属性,它适用于 set_pressed 和 set_focused。但是圆角不是按钮的默认状态,因为我已经插入了背景图像还有。请帮助我...

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

4 回答 4

3
  • 添加布局相同大小的按钮

  • 提供背景图像并将圆角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>
于 2013-10-31T07:03:27.507 回答
0

我宁愿在代码中尝试这样的事情(尚未检查):

    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[] radiuses),并且感谢这个具有 4 个不同半径的矩形绘制图像(左上角、右上角、左下角、右下角)

于 2013-10-31T06:22:58.233 回答
0

添加另一个项目以显示默认状态,如下所示:

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

于 2013-10-31T06:10:55.873 回答
0

在最后一项下方添加此内容。

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

它将作为按钮的默认样式。

于 2013-10-31T06:09:47.660 回答