如何以编程方式更改可绘制形状的大小?

Sha*_*182 5 xml android shapes

如何以编程方式更改宽度和高度,以便形状适合其运行的 Android 设备?90dp 高度适合我的设备,但我希望它适合其他设备。

这是我的 shape.xml 代码:

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/cellfilled" >

<size android:width="0dp"
    android:height="90dp" />

<solid android:color="#00000000" />

<stroke
    android:width="1dp"
    android:color="#ff000000"/>

<padding
    android:bottom="1dp"
    android:left="1dp"
    android:right="1dp"
    android:top="1dp" />
</shape>
Run Code Online (Sandbox Code Playgroud)

xXJ*_*kXx 1

抱歉,我正在打电话。

你可以尝试

设置边界()

首先,如果它不起作用,那么您可以使用以下代码:

// Read your drawable from somewhere 
Drawable dr = getResources().getDrawable(R.drawable.somedrawable); 
Bitmap bitmap = ((BitmapDrawable) dr).getBitmap(); 
// Scale it to 50 x 50 
Drawable d = new BitmapDrawable(getResources(), Bitmap.createScaledBitmap(bitmap, 50, 50, true)); 
// Set your new, scaled drawable "d"
Run Code Online (Sandbox Code Playgroud)

编辑:

您可以在以下代码中获取以 px 为单位的显示大小:

Display display = getWindowManager().getDefaultDisplay();
Point size = new Point();
display.getSize(size);

int width = size.x;
int height = size.y;
Run Code Online (Sandbox Code Playgroud)

然后您可以获得特定显示器的相对大小,并使用上面的答案以编程方式更改可绘制对象的大小。希望有帮助:D