缩放按钮内的drawable?

时间:2022-11-21 12:43:17

Currently my drawable just scales to it's normal size, I want it to fit inside my button. Here's a picture of how it looks now:

目前我的drawable只是缩放到它的正常大小,我希望它适合我的按钮。这是现在的样子:

缩放按钮内的drawable?

Here is the xml for the button:

这是按钮的xml:

<LinearLayout android:layout_width="wrap_content"
    android:id="@+id/linearLayout2" android:layout_height="40dp"
    android:layout_weight="0.4" android:gravity="right|center_vertical"
    android:paddingRight="5dp">
    <Button android:id="@+id/button1" android:background="@drawable/refresh"
        android:layout_height="25dp" android:layout_width="150dp"
        android:text="@string/buttonSearchAgain" android:drawableRight="@drawable/refresh_48"></Button>
</LinearLayout>

So I want it to look like this:

所以我希望它看起来像这样:

缩放按钮内的drawable?

Is there any way I can shrink my drawable?

有什么办法可以缩小我的画幅吗?

6 个解决方案

#1


2  

Recreate the image. Keep image size in pixels but reduce the size of the arrow and make the rest transparent. That's my bet anyway =)

重新创建图像。保持图像大小以像素为单位,但减小箭头的大小,使其余部分透明。无论如何这是我的赌注=)

#2


4  

You can solve this programmatically: Have a look at the function scaleButtonDrawables in this answer. With that function you can write in onCreate() of your activity:

您可以通过编程方式解决此问题:在此答案中查看函数scaleButtonDrawables。使用该功能,您可以在活动的onCreate()中写入:

final Button button = (Button) findViewById(R.id.button1);
ViewTreeObserver vto = button.getViewTreeObserver();
vto.addOnPreDrawListener(new ViewTreeObserver.OnPreDrawListener() {
            public boolean onPreDraw() {
                button.getViewTreeObserver().removeOnPreDrawListener(this);
                //scale to 90% of button height
                DroidUtils.scaleButtonDrawables(button, 0.9);
                return true;
            }
        });

(You cannot call this directly because button height is not available in onCreate().)

(你不能直接调用它,因为onCreate()中没有按钮高度。)

#3


3  

Since you cannot modify or scale the image over xml you need to set the compound drawable bounds within the code.

由于无法在xml上修改或缩放图像,因此需要在代码中设置复合可绘制边界。

Override the Button class with your own "CustomButton". Then override the method "SetCompoundDrawables(left, top, right, bottom):

使用您自己的“CustomButton”覆盖Button类。然后覆盖方法“SetCompoundDrawables(left,top,right,bottom):

    public override void SetCompoundDrawables(Drawable left, Drawable top, Drawable right, Drawable bottom)
    {
        if (right != null)
        {
            var bounds = right.Bounds;
            right.SetBounds(bounds.Left, bounds.Top, bounds.Right, bounds.Bottom - 70);
        }

        if (left != null)
        {
            var bounds = left.Bounds;
            left.SetBounds(bounds.Left, bounds.Top, bounds.Right, bounds.Bottom - 70);
        }

        base.SetCompoundDrawables(left, top, right, bottom);
    }

Please be aware that this is C# Code since I am using Xamarin. But the java code should be the same (despite of some uppper case namings)

请注意,这是C#代码,因为我使用的是Xamarin。但是java代码应该是相同的(尽管有一些不成熟的案例命名)

#4


1  

I think I found the solution for your problem, very late but it might help others

我想我找到了你的问题的解决方案,很晚,但它可能会帮助其他人

Drawable drawable = getResources().getDrawable(R.drawable.s_vit);
drawable.setBounds(0, 0, (int)(drawable.getIntrinsicWidth()*0.5), 
                     (int)(drawable.getIntrinsicHeight()*0.5));
ScaleDrawable sd = new ScaleDrawable(drawable, 0, scaleWidth, scaleHeight);
Button btn = findViewbyId(R.id.yourbtnID);
btn.setCompoundDrawables(sd.getDrawable(), null, null, null); 

#5


0  

Try

尝试

<Button android:id="@+id/button1" android:background="@drawable/refresh"
    android:layout_height="25dp" android:layout_width="150dp"
    android:text="@string/buttonSearchAgain" android:drawableRight="@drawable/refresh_48"
    android:padding="5dp"></Button>

(So add android:padding="5dp")

(所以添加android:padding =“5dp”)

#6


0  

I made it work! It's a bit messy, and I ended up not using button views at all. The "button" itself is a relativeLayout, so if you want to use this solution you are going to have to fiddle with some come if you want the button to animate. Here is my XML:

我做到了!它有点乱,我最终根本没有使用按钮视图。 “按钮”本身是一个relativeLayout,所以如果你想使用这个解决方案,你将不得不摆弄一些来,如果你想按钮动画。这是我的XML:

<LinearLayout android:layout_width="wrap_content"
    android:id="@+id/linearLayout2" android:layout_height="40dp"
    android:layout_weight="0.4" android:gravity="right|center_vertical"
    android:paddingRight="5dp">
    <RelativeLayout android:layout_height="25dp"
        android:layout_width="150dp" android:id="@+id/relativeLayout1"
        android:clickable="true">
        <ImageView android:id="@+id/imageView1" android:scaleType="fitXY"
            android:adjustViewBounds="true" android:layout_width="fill_parent"
            android:src="@drawable/saywhat" android:layout_height="fill_parent"></ImageView>
        <LinearLayout android:layout_width="fill_parent"
            android:id="@+id/linearLayout3" android:weightSum="1"
            android:layout_height="fill_parent">
            <TextView android:layout_weight="0.6"
                android:layout_height="fill_parent" android:text="TextView"
                android:layout_width="wrap_content" android:id="@+id/textView1"></TextView>
            <LinearLayout android:layout_weight="0.4"
                android:layout_height="fill_parent" android:layout_width="wrap_content"
                android:id="@+id/linearLayout4">
                <ImageView android:id="@+id/imageView2"
                    android:layout_height="wrap_content" android:scaleType="fitXY"
                    android:adjustViewBounds="true" android:layout_width="wrap_content"
                    android:src="@drawable/refresh_48"></ImageView>
            </LinearLayout>
        </LinearLayout>
    </RelativeLayout>
</LinearLayout>

Enjoy :)

请享用 :)

#1


2  

Recreate the image. Keep image size in pixels but reduce the size of the arrow and make the rest transparent. That's my bet anyway =)

重新创建图像。保持图像大小以像素为单位,但减小箭头的大小,使其余部分透明。无论如何这是我的赌注=)

#2


4  

You can solve this programmatically: Have a look at the function scaleButtonDrawables in this answer. With that function you can write in onCreate() of your activity:

您可以通过编程方式解决此问题:在此答案中查看函数scaleButtonDrawables。使用该功能,您可以在活动的onCreate()中写入:

final Button button = (Button) findViewById(R.id.button1);
ViewTreeObserver vto = button.getViewTreeObserver();
vto.addOnPreDrawListener(new ViewTreeObserver.OnPreDrawListener() {
            public boolean onPreDraw() {
                button.getViewTreeObserver().removeOnPreDrawListener(this);
                //scale to 90% of button height
                DroidUtils.scaleButtonDrawables(button, 0.9);
                return true;
            }
        });

(You cannot call this directly because button height is not available in onCreate().)

(你不能直接调用它,因为onCreate()中没有按钮高度。)

#3


3  

Since you cannot modify or scale the image over xml you need to set the compound drawable bounds within the code.

由于无法在xml上修改或缩放图像,因此需要在代码中设置复合可绘制边界。

Override the Button class with your own "CustomButton". Then override the method "SetCompoundDrawables(left, top, right, bottom):

使用您自己的“CustomButton”覆盖Button类。然后覆盖方法“SetCompoundDrawables(left,top,right,bottom):

    public override void SetCompoundDrawables(Drawable left, Drawable top, Drawable right, Drawable bottom)
    {
        if (right != null)
        {
            var bounds = right.Bounds;
            right.SetBounds(bounds.Left, bounds.Top, bounds.Right, bounds.Bottom - 70);
        }

        if (left != null)
        {
            var bounds = left.Bounds;
            left.SetBounds(bounds.Left, bounds.Top, bounds.Right, bounds.Bottom - 70);
        }

        base.SetCompoundDrawables(left, top, right, bottom);
    }

Please be aware that this is C# Code since I am using Xamarin. But the java code should be the same (despite of some uppper case namings)

请注意,这是C#代码,因为我使用的是Xamarin。但是java代码应该是相同的(尽管有一些不成熟的案例命名)

#4


1  

I think I found the solution for your problem, very late but it might help others

我想我找到了你的问题的解决方案,很晚,但它可能会帮助其他人

Drawable drawable = getResources().getDrawable(R.drawable.s_vit);
drawable.setBounds(0, 0, (int)(drawable.getIntrinsicWidth()*0.5), 
                     (int)(drawable.getIntrinsicHeight()*0.5));
ScaleDrawable sd = new ScaleDrawable(drawable, 0, scaleWidth, scaleHeight);
Button btn = findViewbyId(R.id.yourbtnID);
btn.setCompoundDrawables(sd.getDrawable(), null, null, null); 

#5


0  

Try

尝试

<Button android:id="@+id/button1" android:background="@drawable/refresh"
    android:layout_height="25dp" android:layout_width="150dp"
    android:text="@string/buttonSearchAgain" android:drawableRight="@drawable/refresh_48"
    android:padding="5dp"></Button>

(So add android:padding="5dp")

(所以添加android:padding =“5dp”)

#6


0  

I made it work! It's a bit messy, and I ended up not using button views at all. The "button" itself is a relativeLayout, so if you want to use this solution you are going to have to fiddle with some come if you want the button to animate. Here is my XML:

我做到了!它有点乱,我最终根本没有使用按钮视图。 “按钮”本身是一个relativeLayout,所以如果你想使用这个解决方案,你将不得不摆弄一些来,如果你想按钮动画。这是我的XML:

<LinearLayout android:layout_width="wrap_content"
    android:id="@+id/linearLayout2" android:layout_height="40dp"
    android:layout_weight="0.4" android:gravity="right|center_vertical"
    android:paddingRight="5dp">
    <RelativeLayout android:layout_height="25dp"
        android:layout_width="150dp" android:id="@+id/relativeLayout1"
        android:clickable="true">
        <ImageView android:id="@+id/imageView1" android:scaleType="fitXY"
            android:adjustViewBounds="true" android:layout_width="fill_parent"
            android:src="@drawable/saywhat" android:layout_height="fill_parent"></ImageView>
        <LinearLayout android:layout_width="fill_parent"
            android:id="@+id/linearLayout3" android:weightSum="1"
            android:layout_height="fill_parent">
            <TextView android:layout_weight="0.6"
                android:layout_height="fill_parent" android:text="TextView"
                android:layout_width="wrap_content" android:id="@+id/textView1"></TextView>
            <LinearLayout android:layout_weight="0.4"
                android:layout_height="fill_parent" android:layout_width="wrap_content"
                android:id="@+id/linearLayout4">
                <ImageView android:id="@+id/imageView2"
                    android:layout_height="wrap_content" android:scaleType="fitXY"
                    android:adjustViewBounds="true" android:layout_width="wrap_content"
                    android:src="@drawable/refresh_48"></ImageView>
            </LinearLayout>
        </LinearLayout>
    </RelativeLayout>
</LinearLayout>

Enjoy :)

请享用 :)