使用svg
示例
<vector
xmlns:android="http://schemas.android.com/apk/res/android"
android:width="24dp"
android:height="24dp"
android:viewportHeight="480.0"
android:viewportWidth="640.0">
<path
android:fillColor="#FF0000"
android:pathData="M309,180h260v166h-260z"
android:strokeColor="#000000"
android:strokeWidth="5" />
</vector>
- svg file可以从工具制作如在线制作SvgE-edit
- 使用和drawable资源一样的引用
- 大小应调整 android:width="120dp" 的大小,宽高比例要相同,
若调整imageview的大小,则图片还是拉伸的,锯齿效果,
很好理解,先绘制出矢量图形,绘制成功后就是点式的了,再改变view的大小相当于拉伸点式图形所以效果是有锯齿的了。
2使用动画
示例
vectordrawable.xml
<?xml version="1.0" encoding="utf-8"?>
<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="v"
android:fillColor="#000000"
android:pathData="M300,70 l 0,-70 70,70 0,0 -70,70z" />
</group>
</vector>
animatedvectordrawable.xml
<?xml version="1.0" encoding="utf-8"?>
<animated-vector xmlns:android="http://schemas.android.com/apk/res/android"
android:drawable="@drawable/vectordrawable">
<target
android:name="rotationGroup"
android:animation="@animator/rotation" />
<target
android:name="v"
android:animation="@animator/path_morph" />
</animated-vector>
- android:drawable="@drawable/vectordrawable" 矢量资源
- target android:name="rotationGroup" 矢量资源 vectordrawable.xml中定义的name 即 animation定义的动画,是针对该name的
- android:animation="@animator/rotation" 代码如下:
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android">
<objectAnimator
android:duration="6000"
android:propertyName="rotation"
android:valueFrom="0"
android:valueTo="360" />
</set>
动画资源说明
- res/anim 放置 View Animation There are two types of animations that you can do with the view animation framework: Tween animation: Creates an animation by performing a series of transformations on a single image with an Animation Frame animation: or creates an animation by showing a sequence of images in order with an AnimationDrawable.
- res/aminator 放置属性动画 Property Animation
代码如下:
package com.andyidea.guidedemo;
import android.content.Intent;
import android.graphics.drawable.Animatable;
import android.graphics.drawable.AnimatedStateListDrawable;
import android.graphics.drawable.Drawable;
import android.graphics.drawable.RippleDrawable;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.view.animation.Animation;
import android.widget.ImageView;
public class VectorActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.layout);
// AnimatedStateListDrawable
findViewById(R.id.jiv1).setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
ImageView iv = (ImageView) findViewById(R.id.jiv1);
Drawable drawable = iv.getDrawable();
if (drawable instanceof Animatable) {
Animatable able = (Animatable) drawable;
able.start();
}
}
});
}
}
这就是 AnimatedVectorDrawable 放置在drawable资源目录下