问题
I'd like to start using vectors for the icons with in the apps I do, but not sure how to deal with the compatibility
I'm not interested in any of the third party compat libraries and happy to wait for googles version (Mentioned here VectorDrawable - is it available somehow for pre-Lollipop versions of Android?).
So basically for now I'm happy to continue to have the images per resolution as well as an additional for > Lollipop. If the device is higher than 21 it will use the vector otherwise it will fall back to the standard png's.
I can have the vector in a drawable-v21 folder, but the drawable-[dpi] folder takes precedence over the version, meaning the vector isn't used.
I'm hoping the vector is used so that when the compat comes available I can simply delete all the png's and know the vectors will be ok as I have been able to test them on a device running Lollipop.
回答1:
From Android 5.0 (API level 21) you can use vector drawable in your app. You can use new Android Studio tool called: Vector Asset Studio. It handles PNG making for older versions automatically. See link below for a complete explanation:
Vector Asset Studio
回答2:
Now google released Android Support Library 23.2 Support Vector Drawables and Animated Vector Drawables !
// Gradle Plugin 2.0+
android {
defaultConfig {
vectorDrawables.useSupportLibrary = true
}
}
Note:
- Vector images all the way back to API 7 (Android 2.1 Eclair).
- Animated vectors are a bit more limited, going only as far back as API 11
回答3:
In order to have backwards compatibility for Vector Drawables here is the following configuration I used for API Level pre-20 devices.
build.gradle
vectorDrawables.useSupportLibrary = true
dependencies{classpath 'com.android.tools.build:gradle:2.1.2'}
Activity
onCreate > AppCompatDelegate.setCompatVectorFromResourcesEnabled(true);
XML ImageView
app:srcCompat="@drawable/vector"
Programmatic ImageView
imageView.setBackgroundResource(R.vector);
vector.xml
<vector xmlns:android="http://schemas.android.com/apk/res/android"
android:width="30dp"
android:height="20dp"
android:viewportHeight="20.0"
android:viewportWidth="30.0">
<path
android:fillColor="#4C5F70"
android:pathData="DATA_HERE"
android:strokeColor="#00000000"
android:strokeWidth="1"/>
</vector>
回答4:
With Android Support Library 23.2, VectorDrawables are now supported from API level 7 onwards and AnimatedVectorDrawables from API level 11 so.
So now you can use
app:srcCompat
instead of
app:src
for using VectorDrawable
s with backward compatibility.
回答5:
EDIT: This was an interim fix until Android studio 1.4 was released. There is no longer a need for this. See Ka Developer's answer instead
Found a solution that while not brilliant does work.
You need to create a lollypop drawables folder for each of the dpi folders and add the same vector to each folder.
Eg create the folders
drawable-hdpi-v21
drawable-xhdpi-v21
drawable-xxhdpi-v21
drawable-xxxhdpi-v21
回答6:
if you use SRC the use this:
you don't need to add that line in every activity, if you added it once in the Application class it will work as well.
public class App extends Application {
static {
AppCompatDelegate.setCompatVectorFromResourcesEnabled(true);
}
REMEMBER: You still need to have enabled the use of the support library in gradle:
android {
defaultConfig {
vectorDrawables.useSupportLibrary = true
}
}
Inside xml use:
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:srcCompat="@drawable/ic_add" />
When you want to use vector drawable as drawableTop, bottom, right ,left then used LayerDrawable otherwise it crash on device lower than 5.0
<level-list xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="@drawable/search"/>
</level-list>
来源:https://stackoverflow.com/questions/31870992/android-vector-compatibility