规模drawableLeft与文本按钮 [英] Scale drawableLeft in button with text

查看:127
本文介绍了规模drawableLeft与文本按钮的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有文本的按钮,并在左侧绘制。有没有什么办法可以使绘制规模,这是适当的大小(填按钮的高度),同时保持其宽高比?

从我的布局相关摘录:

 <按钮
            机器人:文字=@字符串/ add_fav
            机器人:ID =@ + ID / event_fav_button
            机器人:layout_width =match_parent
            机器人:layout_height =40dp
            机器人:drawableLeft =@绘制/ star_yellow/>


解决方案

您可以在按键的尺寸,使用的getHeight()和基础调整图像大小的getWidth()方法来获取键,然后用下面的函数的大小来调整图像按钮:

调整位图:

 公共位图getResizedBitmap(位图BM,诠释newHeight,诠释newWidth){INT宽度= bm.getWidth();INT高度= bm.getHeight();浮动scaleWidth =((浮点)newWidth)/宽度;浮动scaleHeight =((浮点)newHeight)/身高;//创建用于操作一个矩阵字模=新的Matrix();//调整位图matrix.postScale(scaleWidth,scaleHeight);//重新创建新的位图位图resizedBitmap = Bitmap.createBitmap(BM,0,0,宽度,高度,矩阵,假);返回resizedBitmap;}


现在使用调整后的图像为您的按钮。
参考

I have a button with text and a drawable on the left. Is there any way I can make that drawable scale to it's appropriate size (fill button's height) while keeping its aspect ratio?

Relevant excerpt from my layout:

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

解决方案

You can resize an image on basis of the dimension of a Button, use getHeight() and getWidth() methods to get the size of the button and use following function to resize image for Button:

Resizing a Bitmap:

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;

}


Now use this resized image for your button. Reference

这篇关于规模drawableLeft与文本按钮的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆