Skip to content

Commit c18cfc4

Browse files
committed
add vector drawalbe demo
add vector drawalbe demo
1 parent ad89522 commit c18cfc4

28 files changed

+597
-0
lines changed

vectordemo/.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
/build

vectordemo/build.gradle

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
apply plugin: 'com.android.application'
2+
3+
android {
4+
compileSdkVersion 26
5+
6+
7+
8+
defaultConfig {
9+
applicationId "com.paike.zjc.vectordemo"
10+
minSdkVersion 21
11+
targetSdkVersion 26
12+
versionCode 1
13+
versionName "1.0"
14+
15+
testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
16+
17+
vectorDrawables.useSupportLibrary = true
18+
19+
}
20+
21+
buildTypes {
22+
release {
23+
minifyEnabled false
24+
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
25+
}
26+
}
27+
28+
}
29+
30+
dependencies {
31+
implementation fileTree(dir: 'libs', include: ['*.jar'])
32+
33+
implementation 'com.android.support:appcompat-v7:26.1.0'
34+
implementation 'com.android.support.constraint:constraint-layout:1.0.2'
35+
testImplementation 'junit:junit:4.12'
36+
androidTestImplementation 'com.android.support.test:runner:1.0.1'
37+
androidTestImplementation 'com.android.support.test.espresso:espresso-core:3.0.1'
38+
}

vectordemo/proguard-rules.pro

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
# Add project specific ProGuard rules here.
2+
# You can control the set of applied configuration files using the
3+
# proguardFiles setting in build.gradle.
4+
#
5+
# For more details, see
6+
# http://developer.android.com/guide/developing/tools/proguard.html
7+
8+
# If your project uses WebView with JS, uncomment the following
9+
# and specify the fully qualified class name to the JavaScript interface
10+
# class:
11+
#-keepclassmembers class fqcn.of.javascript.interface.for.webview {
12+
# public *;
13+
#}
14+
15+
# Uncomment this to preserve the line number information for
16+
# debugging stack traces.
17+
#-keepattributes SourceFile,LineNumberTable
18+
19+
# If you keep the line number information, uncomment this to
20+
# hide the original source file name.
21+
#-renamesourcefileattribute SourceFile
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
package com.paike.zjc.vectordemo;
2+
3+
import android.content.Context;
4+
import android.support.test.InstrumentationRegistry;
5+
import android.support.test.runner.AndroidJUnit4;
6+
7+
import org.junit.Test;
8+
import org.junit.runner.RunWith;
9+
10+
import static org.junit.Assert.*;
11+
12+
/**
13+
* Instrumented test, which will execute on an Android device.
14+
*
15+
* @see <a href="http://d.android.com/tools/testing">Testing documentation</a>
16+
*/
17+
@RunWith(AndroidJUnit4.class)
18+
public class ExampleInstrumentedTest {
19+
@Test
20+
public void useAppContext() throws Exception {
21+
// Context of the app under test.
22+
Context appContext = InstrumentationRegistry.getTargetContext();
23+
24+
assertEquals("com.paike.zjc.vectordemo", appContext.getPackageName());
25+
}
26+
}
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
<?xml version="1.0" encoding="utf-8"?>
2+
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
3+
package="com.paike.zjc.vectordemo">
4+
5+
<application
6+
android:allowBackup="true"
7+
android:icon="@mipmap/ic_launcher"
8+
android:label="@string/app_name"
9+
android:roundIcon="@mipmap/ic_launcher_round"
10+
android:supportsRtl="true"
11+
android:theme="@style/AppTheme">
12+
<activity android:name=".MainActivity">
13+
<intent-filter>
14+
<action android:name="android.intent.action.MAIN" />
15+
16+
<category android:name="android.intent.category.LAUNCHER" />
17+
</intent-filter>
18+
</activity>
19+
</application>
20+
21+
</manifest>
Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
1+
package com.paike.zjc.vectordemo;
2+
3+
import android.graphics.Color;
4+
import android.graphics.drawable.Animatable;
5+
import android.support.v7.app.AppCompatActivity;
6+
import android.os.Bundle;
7+
import android.view.View;
8+
import android.widget.Button;
9+
import android.widget.ImageView;
10+
11+
import java.io.FileOutputStream;
12+
13+
14+
/**
15+
* Vector Drawable Demo
16+
* 1.Vector的drawable文件放在drawable文件夹
17+
* 2.定义Vector的动画放在animator文件夹
18+
* 3.定义Vector真正动画执行到Vector drawalbe对应path动画 放在drawable文件夹
19+
* xml根标签:animated-vector
20+
* 4.gralde需添加
21+
* defaultConfig {
22+
* vectorDrawables.useSupportLibrary = true
23+
* }
24+
* 5.布局需设置特定的属性
25+
* 如:
26+
* ImageView app:srcCompat="@drawable/test"
27+
*
28+
* 参考:
29+
* 例子:
30+
* https://blog.csdn.net/gjnm820/article/details/78590313
31+
* 外文例子:(大干货)
32+
* https://www.androiddesignpatterns.com/2016/11/introduction-to-icon-animation-techniques.html
33+
* Vector pathdata属性介绍:
34+
* https://blog.csdn.net/n4167/article/details/79184286
35+
*
36+
*
37+
* 备注:
38+
* VectorDrawable minSDK>=21
39+
*
40+
*/
41+
public class MainActivity extends AppCompatActivity implements View.OnClickListener {
42+
43+
private Button btn1,btn2;
44+
private ImageView iv;
45+
46+
@Override
47+
protected void onCreate(Bundle savedInstanceState) {
48+
super.onCreate(savedInstanceState);
49+
setContentView(R.layout.activity_main);
50+
51+
initView();
52+
53+
54+
}
55+
56+
private void initView() {
57+
btn1 = findViewById(R.id.button);
58+
btn2 = findViewById(R.id.button2);
59+
iv = findViewById(R.id.imageView);
60+
61+
btn1.setOnClickListener(this);
62+
btn2.setOnClickListener(this);
63+
}
64+
65+
@Override
66+
public void onClick(View v) {
67+
switch (v.getId()){
68+
case R.id.button:{
69+
if(iv.getDrawable() instanceof Animatable){
70+
((Animatable)iv.getDrawable()).start();
71+
}
72+
73+
}break;
74+
case R.id.button2:{
75+
if(iv.getDrawable() instanceof Animatable){
76+
((Animatable)iv.getDrawable()).stop();
77+
}
78+
79+
iv.getDrawable().setTint(Color.BLUE);
80+
}break;
81+
default:{
82+
83+
}break;
84+
}
85+
}
86+
}
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
<?xml version="1.0" encoding="utf-8"?>
2+
<set xmlns:android="http://schemas.android.com/apk/res/android">
3+
4+
<!-- trimPathStart 从头开始截取, trimPathEnd 从尾开始截取 -->
5+
<objectAnimator
6+
android:duration="3000"
7+
android:propertyName="trimPathStart"
8+
android:repeatCount="infinite"
9+
android:repeatMode="reverse"
10+
android:valueFrom="0"
11+
android:valueTo="1"
12+
android:valueType="floatType">
13+
14+
</objectAnimator>
15+
16+
<!-- strokeColor:线条颜色 fillColor:填充颜色-->
17+
<objectAnimator
18+
android:propertyName="fillColor"
19+
android:valueFrom="@android:color/holo_red_dark"
20+
android:valueTo="@android:color/holo_blue_bright"
21+
android:repeatCount="infinite"
22+
android:repeatMode="reverse"
23+
android:duration="3000"
24+
android:interpolator="@android:interpolator/overshoot"
25+
android:valueType="intType">
26+
</objectAnimator>
27+
28+
29+
<!-- 属性动画 pathData 路径变换( □ -> ○ ) -->
30+
<!-- 动态VectorDrawable不支持从Strings.xml中读取<pathData>数据,所以将数据拷贝于此 (兼容问题)-->
31+
<!--<objectAnimator xmlns:android="http://schemas.android.com/apk/res/android"
32+
android:propertyName="pathData"
33+
android:valueFrom="M 48,54 L 31,42 15,54 21,35 6,23 25,23 32,4 40,23 58,23 42,35 z"
34+
android:valueTo="M 48,54 L 31,54 15,54 10,35 6,23 25,10 32,4 40,10 58,23 54,35 z"
35+
android:valueType="pathType"
36+
android:duration="3000"/>-->
37+
38+
39+
40+
41+
42+
43+
44+
</set>
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
<vector xmlns:android="http://schemas.android.com/apk/res/android"
2+
xmlns:aapt="http://schemas.android.com/aapt"
3+
android:width="108dp"
4+
android:height="108dp"
5+
android:viewportHeight="108"
6+
android:viewportWidth="108">
7+
<path
8+
android:fillType="evenOdd"
9+
android:pathData="M32,64C32,64 38.39,52.99 44.13,50.95C51.37,48.37 70.14,49.57 70.14,49.57L108.26,87.69L108,109.01L75.97,107.97L32,64Z"
10+
android:strokeColor="#00000000"
11+
android:strokeWidth="1">
12+
<aapt:attr name="android:fillColor">
13+
<gradient
14+
android:endX="78.5885"
15+
android:endY="90.9159"
16+
android:startX="48.7653"
17+
android:startY="61.0927"
18+
android:type="linear">
19+
<item
20+
android:color="#44000000"
21+
android:offset="0.0" />
22+
<item
23+
android:color="#00000000"
24+
android:offset="1.0" />
25+
</gradient>
26+
</aapt:attr>
27+
</path>
28+
<path
29+
android:fillColor="#FFFFFF"
30+
android:fillType="nonZero"
31+
android:pathData="M66.94,46.02L66.94,46.02C72.44,50.07 76,56.61 76,64L32,64C32,56.61 35.56,50.11 40.98,46.06L36.18,41.19C35.45,40.45 35.45,39.3 36.18,38.56C36.91,37.81 38.05,37.81 38.78,38.56L44.25,44.05C47.18,42.57 50.48,41.71 54,41.71C57.48,41.71 60.78,42.57 63.68,44.05L69.11,38.56C69.84,37.81 70.98,37.81 71.71,38.56C72.44,39.3 72.44,40.45 71.71,41.19L66.94,46.02ZM62.94,56.92C64.08,56.92 65,56.01 65,54.88C65,53.76 64.08,52.85 62.94,52.85C61.8,52.85 60.88,53.76 60.88,54.88C60.88,56.01 61.8,56.92 62.94,56.92ZM45.06,56.92C46.2,56.92 47.13,56.01 47.13,54.88C47.13,53.76 46.2,52.85 45.06,52.85C43.92,52.85 43,53.76 43,54.88C43,56.01 43.92,56.92 45.06,56.92Z"
32+
android:strokeColor="#00000000"
33+
android:strokeWidth="1" />
34+
</vector>
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
<vector xmlns:android="http://schemas.android.com/apk/res/android"
2+
android:width="24dp"
3+
android:height="24dp"
4+
android:viewportWidth="24.0"
5+
android:viewportHeight="24.0">
6+
<path
7+
android:name="line4"
8+
android:fillColor="#FFFF0000"
9+
android:pathData="M15,15L3,15v2h12v-2z"/>
10+
11+
<path
12+
android:name="line2"
13+
android:fillColor="#FFFF0000"
14+
android:pathData="M15,7L3,7v2h12L15,7z"/>
15+
16+
<path
17+
android:name="line3"
18+
android:fillColor="#FFFF0000"
19+
android:pathData="M3,13h18v-2L3,11v2z"/>
20+
21+
<path
22+
android:name="line5"
23+
android:fillColor="#FFFF0000"
24+
android:pathData="M3,21h18v-2L3,19v2z"/>
25+
26+
<path
27+
android:name="line1"
28+
android:fillColor="#FFFF0000"
29+
android:pathData="M3,3v2h18L21,3L3,3z"/>
30+
31+
<!-- <path
32+
android:name="all"
33+
android:fillColor="#FFFF0000"
34+
android:pathData="M15,15L3,15v2h12v-2zM15,7L3,7v2h12L15,7zM3,13h18v-2L3,11v2zM3,21h18v-2L3,19v2zM3,3v2h18L21,3L3,3z"/>-->
35+
36+
</vector>

0 commit comments

Comments
 (0)