gpt4 book ai didi

android - 在带有文本的按钮中缩放 drawableLeft

转载 作者:塔克拉玛干 更新时间:2023-11-02 21:18:06 24 4
gpt4 key购买 nike

我有一个带有文本的按钮,左侧有一个可绘制对象。有什么方法可以在保持其纵横比的同时使可绘制比例达到适当的大小(填充按钮的高度)?

我的布局的相关摘录:

        <Button 
android:text="@string/add_fav"
android:id="@+id/event_fav_button"
android:layout_width="match_parent"
android:layout_height="40dp"
android:drawableLeft="@drawable/star_yellow"/>

最佳答案

您可以根据按钮的尺寸调整图像大小,使用 getHeight()getWidth() 方法获取按钮的大小并使用以下方法为 Button 调整图像大小的函数:

调整位图大小:

public Bitmap getResizedBitmap(Bitmap bm, int newHeight, int newWidth) {

int width = bm.getWidth();

int height = bm.getHeight();

float scaleWidth = ((float) newWidth) / width;

float scaleHeight = ((float) newHeight) / height;

// create a matrix for the manipulation

Matrix matrix = new Matrix();

// resize the bit map

matrix.postScale(scaleWidth, scaleHeight);

// recreate the new Bitmap

Bitmap resizedBitmap = Bitmap.createBitmap(bm, 0, 0, width, height, matrix, false);

return resizedBitmap;

}

现在将这个调整大小的图像用于您的按钮。 Reference

关于android - 在带有文本的按钮中缩放 drawableLeft,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10623906/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com