如何以编程方式将可绘制矢量应用于图像?

时间:2019-03-16 11:08:15

标签: android android-vectordrawable

我有一堆SVG图片(5),已在Android Studio中将其转换为矢量可绘制对象。 我需要在适配器中对它们进行硬编码,但我不知道如何申请。

使用常规图像(.png或jpg),这就是将它们放置在数组中的方式。

int[] images = {R.drawable.image_one.png,R.drawable.image_two.png};

并应用于以下图像:

imageView.setImageResource(images[position]);

如何使用vector drawables实现相同的功能(扩展文件以.xml结尾)

2 个答案:

答案 0 :(得分:1)

按如下所示使用getResources()方法

imageView.setImageResource(getResources().getDrawable(R.drawable.image_one));

答案 1 :(得分:0)

png绘制对象和矢量绘制对象都可以这样设置:

  1. 说,在文件系统上,可绘制对象为hello.xmlhello.png
  2. 在两种情况下,您都将其设置为imageView.setImageResource(R.drawable.hello)(不必指定扩展名)

我建议在类型定义旁边使用@DrawableRes批注,以更好地进行编译器检查(@DrawableRes int [] images = ...

相关问题