How to set VectorDrawable as an image for ImageView programmatically

后端 未结 8 1524
梦如初夏
梦如初夏 2020-12-13 05:58

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

I can set png and jpg drawable easily but when i want to set V

相关标签:
8条回答
  • 2020-12-13 06:03

    I had a vector in recycler view I was using img.setImageResource(R.drawable.ic_home) which didn't 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 .

    0 讨论(0)
  • 2020-12-13 06:09

    for Java Code use:

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

    and for XML use:

    <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" />
    
    0 讨论(0)
  • 2020-12-13 06:10

    Use this:

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

    instead of ImageButton, ImageView etc.

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

    0 讨论(0)
  • 2020-12-13 06:13

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

    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.

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

    0 讨论(0)
  • 2020-12-13 06:13

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

    <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"
       app:srcCompat="@drawable/ic_vector_image"
        />
    

    java

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

    below code need to add in build.gradle

    android { defaultConfig{ vectorDrawables.useSupportLibrary = true } }

    And it will serve the perspective of app:srcCompat.

    0 讨论(0)
  • 2020-12-13 06:18

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

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

    where the context is a AppCompatActivity.

    0 讨论(0)
提交回复
热议问题