0

I have an ImageButton and want to scale the button in different sizes which means an image is not the right choice. So I was thinking of an ImageButton with a custom Shape as source.

I want the button to look like this:

enter image description here

I started with this, but got stuck with the rotate stuff:

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
    android:shape="rectangle" >
    
    <stroke
        android:width="2dp"
        android:color="@color/red" />
    <rotate
        android:fromDegrees="90"
        android:toDegrees="135" >
        <shape android:shape="line" >
            <stroke
                android:width="2dp"
                android:color="@color/red" >
            </stroke>
        </shape>
    </rotate>

    <padding
        android:bottom="7dp"
        android:left="7dp"
        android:right="7dp"
        android:top="7dp" />

</shape>

This however does not rotate as I expect it to. Maybe I am completely wrong and need a push in the right direction. :-) Thanks.

4

3 回答 3

0

您是否考虑过使用 Bitmap 并根据您的需要使用 Bitmap.createScaledBitmap(...) 方法对其进行缩放。然后您可以轻松地将其转换为 BitmapDrawable,然后使用 (name-of-your-button).setBackgroundDrawable(nameOfBitmapDrawable); 将此 BitmapDrawable 指定为按钮的背景。

因此,您所要做的就是将该图像(您帖子中的那个)保存为可绘制的(可能是透明的PNG),然后将该可绘制的转换为位图。从那里,您只需按照我上面概述的步骤操作即可。希望这可以帮助。

于 2012-07-10T10:04:01.883 回答
0

我通过自定义Button实现解决了这个问题并覆盖了如下所示的onDraw()方法:

@Override
public void onDraw(Canvas canvas)
{
    width = getMeasuredWidth();
    height = getMeasuredHeight();

    // diagonal top left | bottom right
    canvas.drawLine(0, 0, width, height, mPaint);

    // diagonal top right | bottom left
    canvas.drawLine(width, 0, 0, height, mPaint);

    // horizontal top
    canvas.drawLine(0, 0, width, 0, mPaint);

    // horizontal bottom
    canvas.drawLine(0, height, width, height, mPaint);

    // vertical left
    canvas.drawLine(0, 0, 0, height, mPaint);

    //vertical right
    canvas.drawLine(width, 0, width, height, mPaint);
}

你必须mPaint根据你想要的笔画的颜色和大小等来设置自己。

于 2012-07-17T08:22:48.773 回答
0
<shape xmlns:android="http://schemas.android.com/apk/res/android" 
             android:shape="rectangle"
             >
  <gradient android:angle="270"
            android:endColor="#000000"
            android:centerColor="#00ccff" 
            android:startColor="#000000"
           /> 
  <corners android:radius="5dp" /> 
  </shape>
Try this 
于 2012-07-10T14:21:03.130 回答