安卓:是否可以对XML可绘制对象进行缩放?

15
这是我想做的事情:我有这个比例尺xml文件:
<?xml version="1.0" encoding="utf-8"?>
<scale xmlns:android="http://schemas.android.com/apk/res/android"
    android:drawable="@drawable/mydrawable" 
    android:scaleGravity="bottom"
    android:scaleHeight="50%" />

我想将其与此可绘制的xml一起使用:

<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
    <item>
        <shape xmlns:android="http://schemas.android.com/apk/res/android"
            android:shape="rectangle">
            <gradient android:startColor="#FFFFFFFF"
                android:endColor="#FFFF0000"
                android:angle="90" />
        </shape>
    </item>
</layer-list>

我将比例尺XML设置为LinearLayout的背景,但是我什么也没看到。我有什么遗漏还是这不可能做到(使用XML)?


1
顺便提一下,顶层元素可以是“形状”,无需用图层列表将其包围。另外,这些文件应该在哪个资源目录中? - Greyson
抱歉,我刚意识到我来晚了。 - Greyson
@michelemarcon:你最终得到了什么? - Behnam
1个回答

4

这是一个与ScaleDrawable对象有关的问题,无法在XML中解决。

但很简单,只需按照以下步骤操作:

// Get the scale drawable from your resources
ScaleDrawable scaleDraw = (ScaleDrawable)getResources().getDrawable(R.drawable.background_scale);
// set the Level to 1 (or anything higher than 0)
scaleDraw.setLevel(1);

// Now assign the drawable where you need it
layout.setBackgroundDrawable(scaleDraw);

我使用这个解决方案测试了你的代码,它运行良好。

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接