android Drawable 如何代码设置图片大小
可以通过以下两种方式设置 android Drawable 的图片大小:
- 通过设置 bounds
Android Drawable 的 bounds 是一个矩形,用来指定 Drawable 的大小和位置。可以通过调用 Drawable 的 setBounds() 方法设置其大小和位置。
例如,以下代码将一个 Drawable 对象的大小设置为宽为 100dp,高为 150dp:
Drawable drawable = getResources().getDrawable(R.drawable.icon);
int width = (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 100, getResources().getDisplayMetrics());
int height = (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 150, getResources().getDisplayMetrics());
drawable.setBounds(0, 0, width, height);
- 通过设置宽高属性
可以通过设置 android:width 和 android:height 属性来指定 Drawable 的宽度和高度。
例如,以下代码将一个 Drawable 对象的宽度设置为 100dp,高度设置为 150dp:
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle"
android:width="100dp"
android:height="150dp">
<solid android:color="#FF0000" />
</shape>
``
原文地址: http://www.cveoy.top/t/topic/hvK9 著作权归作者所有。请勿转载和采集!