Sets a drawable as the content of this ImageView. Allows the use of vector drawable when running on older versions of the platform.
androidx.appcompat.widget.AppCompatImageView. A ImageView which supports compatible features on older versions of the platform, including: Allows dynamic tint of its background via the background tint methods in ViewCompat . Allows setting of the background tint using R.
app:srcCompat
is the most foolproof method of integrating vector drawables into your app.Vector drawables allow you to replace multiple png assets with a single vector graphic, defined in XML. While previously limited to Lollipop and higher devices
Note
As of Android Support Library 23.3.0, support vector drawables can only be loaded via app:srcCompat
.
you need to add vectorDrawables.useSupportLibrary = true to your build.gradle
file
// Gradle Plugin 2.0+
android {
defaultConfig {
vectorDrawables.useSupportLibrary = true
}
}
android:src
Sets a drawable as the content of this ImageView.It will display in its original size. No automatic scaling .
If you are using android:src="@drawable/some_vector"
without vectorDrawables.useSupportLibrary = true
in build.gradle file and your app have vector images (vector drawable), then while building the apk file Android gradle plugin generates a lot of *.png files for different screens (hdpi, xhdpi...) from each of your vector drawable (only for API =< 19). The result - bigger size of apk.
When using app:srcCompat="@drawable/some_vector"
with vectorDrawables.useSupportLibrary = true
android uses vector drawable files without generating *.png
files.
You can check this with Android Studio apk analyzer tool. Just build apk with and without vectorDrawables.useSupportLibrary = true
.
I think this is the main difference.
Use:
app:srcCompat="@drawable/backImage"
The srcCompat attribute is actually defined within AppCompat library. Important: you will need to add the appropriate namespace for this.
xmlns:app="http://schemas.android.com/apk/res-auto"
Note
What you are getting seems to be just a lint error that can be ignored. I have tried and gotten the same error, but it is working correctly.
You can use tools:ignore="MissingPrefix"
to avoid seeing this error, temporarily.
I hope this helps.
When using AppCompat
with ImageView
(or subclasses such as ImageButton
and FloatingActionButton
), you’ll be able to use the new app:srcCompat
attribute to reference vector drawables on older versions of the platform (as well as any other drawable available to android:src
).
android.support.v7.appcompat.R.attr.srcCompat:
srcCompat
Sets a drawable as the content of this ImageView. Allows the use of vector drawable when running on older versions of the platform.
May be a reference to another resource, in the form
"@[+][package:]type/name"
or a theme attribute in the form"?[package:]type/name"
.
Don't forget adding xmlns:app="http://schemas.android.com/apk/res-auto"
when use app:srcCompat
.
app:srcCompat="some_resource"
is refer that it is AppCompatActivity src which comes in support library while
android:src="some_resource"
refers to simple activity.
Vectors and animated vectors were only supported in recent versions of the framework. srcCompat can be used with the compatibility library to make them work, but this only works with the certain views in the support library. Notice that app: is used instead of android:. This means its not part of the framework, but a parameter defined by your app.
When using AppCompat
with ImageView
(or subclasses such as ImageButton
and FloatingActionButton
), you’ll be able to use the new app:srcCompat
attribute to reference vector drawables (as well as any other drawable available to android:src
). And 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. You’ll find that directly referencing vector drawables outside of app:srcCompat
will fail prior to Lollipop.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With