标签:raw add plain _id name pen icon 5.6 密度
一、矢量图简介
使用Meterial Icon
Google meterial Design规范提供了meterial icons,你可以在你的Android app中使用。Vector Asset Studio帮助你选择,导入和设置meterial icon大小,定义透明度和Right-to-Left(RTL)镜像设置;
3.VectorDrawable使用
这里我们就简单使用一个Demo进行演示,显示两个图片和一个动画,目录如下:
apply plugin: ‘com.android.library‘
android {
... ...
defaultConfig {
... ...
vectorDrawables.useSupportLibrary = true
}
... ...
}
dependencies {
... ...
compile ‘com.android.support:appcompat-v7:25.0.1‘
}打包完成后,我们分析下APK打包了矢量图的xml文件;apply plugin: ‘com.android.library‘
android {
... ...
aaptOptions {
additionalParameters "--no-version-vectors"
}
... ...
}打包完成后,我们分析下APK发现xml文件对应生成了对应png图片;<vector xmlns:android="http://schemas.android.com/apk/res/android"
android:width="100dp"
android:height="100dp"
android:viewportHeight="32"
android:viewportWidth="32">
<path
android:fillColor="#8000"
android:pathData="M20.5,9.5c-1.955,0,-3.83,1.268,-4.5,3c-0.67,-1.732,-2.547,-3,-4.5,-3C8.957,9.5,7,11.432,7,14
c0,3.53,3.793,6.257,9,11.5c5.207,-5.242,9,-7.97,9,-11.5C25,11.432,23.043,9.5,20.5,9.5z" />
</vector>flight/src/main/res/drawable/battery.xml文件<vector xmlns:android="http://schemas.android.com/apk/res/android"
android:width="100dp"
android:height="100dp"
android:viewportHeight="24.0"
android:viewportWidth="24.0">
<group
android:name="rotationGroup"
android:pivotX="10.0"
android:pivotY="10.0"
android:rotation="15.0">
<path
android:name="vect"
android:fillAlpha=".3"
android:fillColor="#FF000000"
android:pathData="M15.67,4H14V2h-4v2H8.33C7.6,4 7,4.6 7,5.33V9h4.93L13,7v2h4V5.33C17,4.6 16.4,4 15.67,4z" />
<path
android:name="draw"
android:fillColor="#FF000000"
android:pathData="M13,12.5h2L11,20v-5.5H9L11.93,9H7v11.67C7,21.4 7.6,22 8.33,22h7.33c0.74,0 1.34,-0.6 1.34,-1.33V9h-4v3.5z" />
</group>
</vector>VectorDrawable引用<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto
xmlns:tools="http://schemas.android.com/tools"
... ...
tools:context="com.qunar.flight.FlightActivity">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="flight" />
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:srcCompat="@drawable/heart"/>
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:srcCompat="@drawable/battery"/>
</LinearLayout>注意:如果你不使用support library兼容,使用生成png图片,那么不用添加命名空间,采用android:src属性即可;<vector xmlns:android="http://schemas.android.com/apk/res/android"
android:height="64dp"
android:width="64dp"
android:viewportHeight="600"
android:viewportWidth="600" >
<group
android:name="rotationGroup"
android:pivotX="300.0"
android:pivotY="300.0"
android:rotation="45.0" >
<path
android:name="vectorPath"
android:fillColor="#000000"
android:pathData="M300,70 l 0,-70 70,70 0,0 -70,70z" />
</group>
</vector>flight/src/main/res/anim/rotation.xml<set xmlns:android="http://schemas.android.com/apk/res/android">
<objectAnimator
android:duration="6000"
android:propertyName="rotation"
android:valueFrom="0"
android:valueTo="360" />
</set>flight/src/main/res/anim/path_morph.xml<set xmlns:android="http://schemas.android.com/apk/res/android">
<objectAnimator
android:duration="3000"
android:propertyName="pathData"
android:valueFrom="M300,70 l 0,-70 70,70 0,0 -70,70z"
android:valueTo="M300,70 l 0,-70 70,0 0,140 -70,0 z"
android:valueType="pathType"/>
</set>flight/src/main/drawable/avd.xml<animated-vector xmlns:android="http://schemas.android.com/apk/res/android"
android:drawable="@drawable/vd" >
<target
android:name="rotationGroup"
android:animation="@anim/rotation" />
<target
android:name="vectorPath"
android:animation="@anim/path_morph" />
</animated-vector>AnimatedVectorDrawable引用<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
... ...
tools:context="com.qunar.flight.FlightActivity">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="flight" />
... ...
<ImageView
android:id="@+id/imageview1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/avd"/>
</LinearLayout>flight/src/main/java/com/qunar/flight/FlightActivity.javapublic class FlightActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_flight);
ImageView imageView = (ImageView) findViewById(R.id.imageview1);
Drawable drawable = imageView.getDrawable();
if (drawable instanceof Animatable){
((Animatable) drawable).start();
}
}
}运行效果<animated-vector xmlns:android="http://schemas.android.com/apk/res/android" >
<aapt:attr name="android:drawable">
<vector
android:height="64dp"
android:width="64dp"
android:viewportHeight="600"
android:viewportWidth="600" >
<group
android:name="rotationGroup"
android:pivotX="300.0"
android:pivotY="300.0"
android:rotation="45.0" >
<path
android:name="v"
android:fillColor="#000000"
android:pathData="M300,70 l 0,-70 70,70 0,0 -70,70z" />
</group>
</vector>
</aapt:attr>
<target android:name="rotationGroup"> *
<aapt:attr name="android:animation">
<objectAnimator
android:duration="6000"
android:propertyName="rotation"
android:valueFrom="0"
android:valueTo="360" />
</aapt:attr>
</target>
<target android:name="v" >
<aapt:attr name="android:animation">
<set>
<objectAnimator
android:duration="3000"
android:propertyName="pathData"
android:valueFrom="M300,70 l 0,-70 70,70 0,0 -70,70z"
android:valueTo="M300,70 l 0,-70 70,0 0,140 -70,0 z"
android:valueType="pathType"/>
</set>
</aapt:attr>
</target>
</animated-vector>注意:为了优化重绘性能,每个VectorDrawable对象创建了Bitmap缓存。因此,引用相同的VectorDrawable意味着共享相同的Btimap缓存。如果这些引用大小上不一致,Bitmap将会在大小每次变化的时候重建和重绘。换句话说,如果VectorDrawable使用了不同的大小,为每个大小创建VectorDrawable会效率更高;QProject:https://github.com/Pengchengxiang/QProject 分支:ui/vectordrawable
标签:raw add plain _id name pen icon 5.6 密度
原文地址:http://blog.csdn.net/p106786860/article/details/53843934