Android API 16及以上版本中的矢量图层列表绘制

4

我在旧版Android上使用矢量图时遇到了一些问题。我需要在每次活动启动时更改可绘制对象,应该加载相应的SVG文件。

这是我的图层列表:

<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">

<item >
    <shape android:shape="oval">
        <size android:height="50dp" android:width="50dp"/>
        <!-- fill color -->
        <solid android:color="@color/white" />
    </shape>
</item>
<item
    android:id="@+id/avatar"
    android:drawable="@drawable/dog" //I need to change this at run time
    android:bottom="10dp"
    android:left="10dp"
    android:right="10dp"
    android:top="10dp"/>

在我的活动中,我正在使用

static {
    AppCompatDelegate.setCompatVectorFromResourcesEnabled(true);
}

目前我正在使用层叠绘图来选择XML文件并更改SVG,但我遇到了兼容性问题,因为layerDrawable.setDrawble()仅适用于API级别> = 23

 layerDrawable = (LayerDrawable) ContextCompat.getDrawable(this, R.drawable.seekbar_thumb);
 Drawable avatar = (Drawable) ContextCompat.getDrawable(this, getUserAvatarId());
 layerDrawable.setDrawable(0, avatar);

你尝试过使用“app:srcCompat”而不是“android:drawable”吗?这就是矢量图形必须添加到视图中的方式,我还没有尝试过在XML资源文件中使用。 - Gonzalo
1
对于旧版本的Android,您不能在其中使用VectorDrawable的layerDrawable。请改用代码实现。虽然困难和烦人,但是仍然是可能的。至于使用VectorDrawable,您需要使用AppCompatResources.getDrawable()来获取它。 - android developer
@安卓开发者,你能提供一个示例吗? - Vasco
@VasilVasilev 我现在没有时间。也许这个链接可以帮到你:https://dev59.com/pmIj5IYBdhLWcg3wkl2U#44981662? - android developer
@VasilVasilev 好的,我已经提供了答案,并在Android 4.4模拟器上进行了测试。顺便说一下,我认为我对放置VectorDrawable的位置弄错了。不应该放在anydpi中,而应该放在nodpi中。否则它找不到它。 - android developer
显示剩余2条评论
2个回答

1
这个问题的解决方案很简单。我的想法是在活动的onCreate()状态中更改进度条拇指。我的进度条拇指是一个可绘制文件,就像我发布的那个SVG文件一样,基本上,它加载了一个带有白色描边的SVG文件,具体取决于用户,这个SVG应该是可变的。
对于Android API >= 16,解决方案如下:
layerDrawable = (LayerDrawable) ContextCompat.getDrawable(this, R.drawable.seekbar_thumb);
Drawable avatar = VectorDrawableCompat.create(getResources(), getUserAvatarId(), null);
layerDrawable.mutate(); //not share its state with other drawables
layerDrawable.setDrawableByLayerId(R.id.avatar, avatar);
layerDrawable.invalidateSelf(); //inform the layer drawable when it needs to redraw
seekBar.setThumb(layerDrawable);

函数 setDrawableById(int, drawable) 的类型为布尔型,如果图层的可绘制对象已更改,则返回 true,否则返回 false!在这种情况下,您需要使用新的可绘制文件替换视图,例如 seekBar.setThumb(layerDrawable);


1

在旧版的Android系统中,无法将LayerDrawable与其中的VectorDrawable一起使用。

您可以通过编程实现。例如:

MainActivity.kt

    ...
    val border = ShapeDrawable()
    border.paint.color = Color.WHITE
    val background = ShapeDrawable()
    background.paint.color = Color.BLACK
    val vectorDrawable = AppCompatResources.getDrawable(this, R.drawable.ic_android_black_24dp)!!
    val layers = arrayOf<Drawable>(background, border, vectorDrawable)
    val layerDrawable = LayerDrawable(layers)
    layerDrawable.setLayerInset(0, 0, 0, 0, 0)
    layerDrawable.setLayerInset(1, 1, 0, 1, 1)
    layerDrawable.setLayerInset(2, 0, 0, 0, 10)
    imageView.setImageDrawable(layerDrawable)

res/drawable/nodpi/ic_android_black_24dp.xml

<vector xmlns:android="http://schemas.android.com/apk/res/android" android:width="24dp" android:height="24dp"
        android:viewportHeight="24.0" android:viewportWidth="24.0">
    <path
        android:fillColor="#FF000000"
        android:pathData="M6,18c0,0.55 0.45,1 1,1h1v3.5c0,0.83 0.67,1.5 1.5,1.5s1.5,-0.67 1.5,-1.5L11,19h2v3.5c0,0.83 0.67,1.5 1.5,1.5s1.5,-0.67 1.5,-1.5L16,19h1c0.55,0 1,-0.45 1,-1L18,8L6,8v10zM3.5,8C2.67,8 2,8.67 2,9.5v7c0,0.83 0.67,1.5 1.5,1.5S5,17.33 5,16.5v-7C5,8.67 4.33,8 3.5,8zM20.5,8c-0.83,0 -1.5,0.67 -1.5,1.5v7c0,0.83 0.67,1.5 1.5,1.5s1.5,-0.67 1.5,-1.5v-7c0,-0.83 -0.67,-1.5 -1.5,-1.5zM15.53,2.16l1.3,-1.3c0.2,-0.2 0.2,-0.51 0,-0.71 -0.2,-0.2 -0.51,-0.2 -0.71,0l-1.48,1.48C13.85,1.23 12.95,1 12,1c-0.96,0 -1.86,0.23 -2.66,0.63L7.85,0.15c-0.2,-0.2 -0.51,-0.2 -0.71,0 -0.2,0.2 -0.2,0.51 0,0.71l1.31,1.31C6.97,3.26 6,5.01 6,7h12c0,-1.99 -0.97,-3.75 -2.47,-4.84zM10,5L9,5L9,4h1v1zM15,5h-1L14,4h1v1z"/>
</vector>

build.gradle

...
android {
    compileSdkVersion 27
    defaultConfig {
        applicationId "lb.com.myapplication"
        minSdkVersion 15
        targetSdkVersion 27
        versionCode 1
        versionName "1.0"
        testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
        vectorDrawables.useSupportLibrary = true
    }
 ...

在Android 4.4.2模拟器上的结果:

enter image description here


感谢您提供的答案,它也很有帮助。正如您所说,由于您正在以编程方式创建图层可绘制对象,这有点困难和烦人。不过,我已经找到了解决方案,并发布了一个答案,如果您想看一下的话。再次感谢! - Vasco

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