如何以编程方式将VectorDrawable设置为ImageView的图像

时间:2022-11-21 12:06:10

I want to set some vectorDrawables to a ImageView in Android Studio.

我想在Android Studio中将一些vectorDrawables设置为ImageView。

I can set png and jpg drawable easily but when i want to set VectorDrawable, it does not work on imageview.

我可以轻松设置png和jpg drawable,但是当我想设置VectorDrawable时,它在imageview上不起作用。

img.setImageResource(R.drawable.ic_home);

ic_home is VectorDrawable and this code doesn't work.

ic_home是VectorDrawable,此代码不起作用。

8 个解决方案

#1


56  

If you want to use vector drawables (less OR greater than API 21) just do the following:

如果您想使用矢量drawable(比API 21更少OR),请执行以下操作:

Set the image programmatically (e.g. in your activity):

以编程方式设置图像(例如在您的活动中):

imageView.setImageResource(R.drawable.ic_left_arrow_blue); 

or by XML:

或者通过XML:

app:srcCompat="@drawable/your_vector_name"

In your app's build.gradle you need to include:

在你的app的build.gradle中你需要包括:

android {
    defaultConfig {
        vectorDrawables.useSupportLibrary = true
    }
}

And for vector support for less then API 21, add the following to onCreate:

对于少于API 21的向量支持,将以下内容添加到onCreate:

AppCompatDelegate.setCompatVectorFromResourcesEnabled(true);  

#2


17  

As per official android developer blog, no changes for setImageResource() method at runtime for vectorDrawables.

根据官方的android开发人员博客,vectorDrawables在运行时没有对setImageResource()方法进行任何更改。

If you’re changing drawables at runtime, you’ll be able to use the same setImageResource() method as before - no changes there. Using AppCompat and app:srcCompat is the most foolproof method of integrating vector drawables into your app.

如果你在运行时更改drawables,你将能够像以前一样使用相同的setImageResource()方法 - 没有更改。使用AppCompat和app:srcCompat是将矢量绘图集成到应用程序中最简单的方法。

For more details, check out this nice article AppCompat — Age of the vectors by Google Developer.

有关更多详细信息,请查看这篇精彩的文章AppCompat - Google Developer的向量年龄。

#3


9  

if you are concerned with the backward compatibility then you should use AppCompatImageView instead pf image view. go through the code below.

如果您关注向后兼容性,那么您应该使用AppCompatImageView而不是pf图像视图。仔细阅读下面的代码。

<android.support.v7.widget.AppCompatImageView
    android:id="@+id/iv_about"
    android:layout_width="150dp"
    android:layout_height="150dp"
    android:layout_centerHorizontal="true"
    android:layout_centerVertical="true"
    />

java

AppCompatImageView image = (AppCompatImageView) findViewById(R.id.iv_about);
image.setImageResource();

And it will serve the perspective of app:srcCompat.

它将提供app:srcCompat的视角。

#4


6  

For those who want to load a vector drawable programmatically for other uses, such as setting a drawableLeft or otherwise, you can use:

对于那些想要以编程方式加载矢量drawable用于其他用途的人,例如设置drawableLeft或其他用途,您可以使用:

Drawable drawable = AppCompatResources.getDrawable(context, drawableRes);

where the context is a AppCompatActivity.

上下文是AppCompatActivity。

#5


2  

I had a vector in recycler view i was using img.setImageResource(R.drawable.ic_home) which didnt worked properly like some other image get formed in some item of recycler view then i used img.setImageDrawable(activity.getResources().getDrawable(R.drawable.ic_home)) this worked .

我在Recycler视图中有一个向量我正在使用img.setImageResource(R.drawable.ic_home),它没有正常工作,就像其他图像在回收器视图的某个项目中形成然后我使用了img.setImageDrawable(activity.getResources()。getDrawable (R.drawable.ic_home))这个工作。

#6


2  

for Java Code use:

for Java Code使用:

formate_img.setImageResource(R.drawable.ic_text);//ic_text is a Vector Image

and for XML use:

并用于XML:

<android.support.v7.widget.AppCompatImageView
        android:id="@+id/search_btn"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="20dp"
        android:layout_weight="1"
        ads:srcCompat="@drawable/ic_barcode" //for Vector Image
        tools:ignore="VectorDrawableCompat" />

#7


1  

Use this:

android.support.v7.widget.AppCompatImageButton, 
android.support.v7.widget.AppCompatImageView,
android.support.v7.widget.AppCompatTextView 

instead of ImageButton, ImageView etc.

而不是ImageButton,ImageView等。

If vector type image is used. Mainly for custom views.

如果使用矢量类型图像。主要用于自定义视图。

#8


0  

Delete two folders form your drawable folder then rebuild your project it will work properly如何以编程方式将VectorDrawable设置为ImageView的图像

从您的drawable文件夹中删除两个文件夹然后重建您的项目它将正常工作

#1


56  

If you want to use vector drawables (less OR greater than API 21) just do the following:

如果您想使用矢量drawable(比API 21更少OR),请执行以下操作:

Set the image programmatically (e.g. in your activity):

以编程方式设置图像(例如在您的活动中):

imageView.setImageResource(R.drawable.ic_left_arrow_blue); 

or by XML:

或者通过XML:

app:srcCompat="@drawable/your_vector_name"

In your app's build.gradle you need to include:

在你的app的build.gradle中你需要包括:

android {
    defaultConfig {
        vectorDrawables.useSupportLibrary = true
    }
}

And for vector support for less then API 21, add the following to onCreate:

对于少于API 21的向量支持,将以下内容添加到onCreate:

AppCompatDelegate.setCompatVectorFromResourcesEnabled(true);  

#2


17  

As per official android developer blog, no changes for setImageResource() method at runtime for vectorDrawables.

根据官方的android开发人员博客,vectorDrawables在运行时没有对setImageResource()方法进行任何更改。

If you’re changing drawables at runtime, you’ll be able to use the same setImageResource() method as before - no changes there. Using AppCompat and app:srcCompat is the most foolproof method of integrating vector drawables into your app.

如果你在运行时更改drawables,你将能够像以前一样使用相同的setImageResource()方法 - 没有更改。使用AppCompat和app:srcCompat是将矢量绘图集成到应用程序中最简单的方法。

For more details, check out this nice article AppCompat — Age of the vectors by Google Developer.

有关更多详细信息,请查看这篇精彩的文章AppCompat - Google Developer的向量年龄。

#3


9  

if you are concerned with the backward compatibility then you should use AppCompatImageView instead pf image view. go through the code below.

如果您关注向后兼容性,那么您应该使用AppCompatImageView而不是pf图像视图。仔细阅读下面的代码。

<android.support.v7.widget.AppCompatImageView
    android:id="@+id/iv_about"
    android:layout_width="150dp"
    android:layout_height="150dp"
    android:layout_centerHorizontal="true"
    android:layout_centerVertical="true"
    />

java

AppCompatImageView image = (AppCompatImageView) findViewById(R.id.iv_about);
image.setImageResource();

And it will serve the perspective of app:srcCompat.

它将提供app:srcCompat的视角。

#4


6  

For those who want to load a vector drawable programmatically for other uses, such as setting a drawableLeft or otherwise, you can use:

对于那些想要以编程方式加载矢量drawable用于其他用途的人,例如设置drawableLeft或其他用途,您可以使用:

Drawable drawable = AppCompatResources.getDrawable(context, drawableRes);

where the context is a AppCompatActivity.

上下文是AppCompatActivity。

#5


2  

I had a vector in recycler view i was using img.setImageResource(R.drawable.ic_home) which didnt worked properly like some other image get formed in some item of recycler view then i used img.setImageDrawable(activity.getResources().getDrawable(R.drawable.ic_home)) this worked .

我在Recycler视图中有一个向量我正在使用img.setImageResource(R.drawable.ic_home),它没有正常工作,就像其他图像在回收器视图的某个项目中形成然后我使用了img.setImageDrawable(activity.getResources()。getDrawable (R.drawable.ic_home))这个工作。

#6


2  

for Java Code use:

for Java Code使用:

formate_img.setImageResource(R.drawable.ic_text);//ic_text is a Vector Image

and for XML use:

并用于XML:

<android.support.v7.widget.AppCompatImageView
        android:id="@+id/search_btn"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="20dp"
        android:layout_weight="1"
        ads:srcCompat="@drawable/ic_barcode" //for Vector Image
        tools:ignore="VectorDrawableCompat" />

#7


1  

Use this:

android.support.v7.widget.AppCompatImageButton, 
android.support.v7.widget.AppCompatImageView,
android.support.v7.widget.AppCompatTextView 

instead of ImageButton, ImageView etc.

而不是ImageButton,ImageView等。

If vector type image is used. Mainly for custom views.

如果使用矢量类型图像。主要用于自定义视图。

#8


0  

Delete two folders form your drawable folder then rebuild your project it will work properly如何以编程方式将VectorDrawable设置为ImageView的图像

从您的drawable文件夹中删除两个文件夹然后重建您的项目它将正常工作