我在Android中使用较旧的API版本绘制矢量时遇到了一些问题。我需要在每次活动开始时在运行时更改drawables应该加载相应的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);
}
到目前为止,我正在使用图层drawable来选择xml文件并更改svg,但我遇到了兼容性问题,因为layerDrawable.setDrawble()
仅在API级别中可用&gt; = 23
layerDrawable = (LayerDrawable) ContextCompat.getDrawable(this, R.drawable.seekbar_thumb);
Drawable avatar = (Drawable) ContextCompat.getDrawable(this, getUserAvatarId());
layerDrawable.setDrawable(0, avatar);
答案 0 :(得分:0)
解决方法很简单。这个概念是我想要在活动的onCreate()
状态中更改搜索栏的拇指。我的搜索栏的拇指是我发布的那个可绘制文件。基本上,它会加载一个带有白色笔划的SVG文件,具体取决于用户此SVG应该可以更改。
对于Android API&gt; = 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)
的类型为boolean,如果更改了图层的drawable,则返回true;如果不是,则返回false!然后,您需要在这种情况下使用新的drawable文件替换视图seekBar.setThumb(layerDrawable);
答案 1 :(得分:0)
在旧的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 /抽拉/ 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模拟器上: