简体   繁体   English

在按钮内缩放可绘制对象?

[英]Scaling a drawable inside a button?

Currently my drawable just scales to it's normal size, I want it to fit inside my button.目前我的drawable只是缩放到它的正常大小,我希望它适合我的按钮。 Here's a picture of how it looks now:这是它现在的样子:

在此处输入图像描述

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:所以我希望它看起来像这样:

在此处输入图像描述

Is there any way I can shrink my drawable?有什么办法可以缩小我的drawable?

You can solve this programmatically: Have a look at the function scaleButtonDrawables in this answer .您可以通过编程方式解决此问题:查看此答案中的 function scaleButtonDrawables With that function you can write in onCreate() of your activity:使用 function 您可以在活动的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() 中没有按钮高度。)

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".用您自己的“CustomButton”覆盖按钮 class。 Then override the method "SetCompoundDrawables(left, top, right, bottom):然后重写方法“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.请注意,这是 C# 代码,因为我使用的是 Xamarin。 But the java code should be the same (despite of some uppper case namings)但是 java 代码应该是相同的(尽管有一些大写命名)

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

Recreate the image.重新创建图像。 Keep image size in pixels but reduce the size of the arrow and make the rest transparent.保持图像大小以像素为单位,但减小箭头大小并使 rest 透明。 That's my bet anyway =)无论如何,这是我的赌注=)

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"

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:所以如果你想使用这个解决方案,如果你想让按钮动画,你将不得不摆弄一些来:这是我的 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:)享受:)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM