在API 22和API 28中,对于在layer-list中使用drawable svg的行为存在一些差异,特别是在启动屏幕上。为了解决这个问题,可以尝试以下解决方法:
// 在API 21及更低版本中使用VectorDrawableCompat
Drawable drawable = VectorDrawableCompat.create(getResources(), R.drawable.your_svg_file, getTheme());
// 设置drawable为ImageView的背景
imageView.setBackground(drawable);
public class SvgDrawableAdapter {
public static Drawable getDrawable(Context context, int svgResId) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
// 在API 21及更高版本中,直接加载svg文件
return context.getResources().getDrawable(svgResId, context.getTheme());
} else {
// 在API 21及更低版本中,使用VectorDrawableCompat加载svg文件
return VectorDrawableCompat.create(context.getResources(), svgResId, context.getTheme());
}
}
}
然后,在你的代码中使用适配器类来加载svg文件。
Drawable drawable = SvgDrawableAdapter.getDrawable(context, R.drawable.your_svg_file);
imageView.setBackground(drawable);
这样,你就可以根据不同的API版本来加载svg文件,并保持一致的行为。
希望以上解决方法可以帮助到你。