├── app
├── .gitignore
├── src
│ └── main
│ │ ├── res
│ │ ├── mipmap-hdpi
│ │ │ └── ic_launcher.png
│ │ ├── mipmap-mdpi
│ │ │ └── ic_launcher.png
│ │ ├── mipmap-xhdpi
│ │ │ └── ic_launcher.png
│ │ ├── mipmap-xxhdpi
│ │ │ └── ic_launcher.png
│ │ ├── drawable
│ │ │ └── ic_heart_24dp.xml
│ │ ├── values
│ │ │ ├── strings.xml
│ │ │ ├── styles.xml
│ │ │ └── colors.xml
│ │ └── layout
│ │ │ ├── include_vertical_space.xml
│ │ │ ├── activity_main.xml
│ │ │ └── include_hrm_counter.xml
│ │ ├── AndroidManifest.xml
│ │ └── java
│ │ └── com
│ │ └── txusballesteros
│ │ └── demo
│ │ └── MainActivity.java
├── proguard-rules.pro
└── build.gradle
├── snake
├── .gitignore
├── proguard-rules.pro
├── src
│ └── main
│ │ ├── AndroidManifest.xml
│ │ ├── res
│ │ └── values
│ │ │ └── attrs.xml
│ │ └── java
│ │ └── com
│ │ └── txusballesteros
│ │ └── SnakeView.java
└── build.gradle
├── release.jks
├── assets
└── demo.gif
├── gradle.properties
├── .travis.yml
├── settings.gradle
├── .gitignore
└── README.md
/app/.gitignore:
--------------------------------------------------------------------------------
1 | /build
2 |
--------------------------------------------------------------------------------
/snake/.gitignore:
--------------------------------------------------------------------------------
1 | /build
2 |
--------------------------------------------------------------------------------
/release.jks:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/txusballesteros/snake/HEAD/release.jks
--------------------------------------------------------------------------------
/assets/demo.gif:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/txusballesteros/snake/HEAD/assets/demo.gif
--------------------------------------------------------------------------------
/app/src/main/res/mipmap-hdpi/ic_launcher.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/txusballesteros/snake/HEAD/app/src/main/res/mipmap-hdpi/ic_launcher.png
--------------------------------------------------------------------------------
/app/src/main/res/mipmap-mdpi/ic_launcher.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/txusballesteros/snake/HEAD/app/src/main/res/mipmap-mdpi/ic_launcher.png
--------------------------------------------------------------------------------
/app/src/main/res/mipmap-xhdpi/ic_launcher.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/txusballesteros/snake/HEAD/app/src/main/res/mipmap-xhdpi/ic_launcher.png
--------------------------------------------------------------------------------
/app/src/main/res/mipmap-xxhdpi/ic_launcher.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/txusballesteros/snake/HEAD/app/src/main/res/mipmap-xxhdpi/ic_launcher.png
--------------------------------------------------------------------------------
/app/src/main/res/drawable/ic_heart_24dp.xml:
--------------------------------------------------------------------------------
1 |
6 |
9 |
10 |
--------------------------------------------------------------------------------
/app/proguard-rules.pro:
--------------------------------------------------------------------------------
1 | # Add project specific ProGuard rules here.
2 | # By default, the flags in this file are appended to flags specified
3 | # in /Users/txus/Android/android-sdk-macosx/tools/proguard/proguard-android.txt
4 | # You can edit the include path and order by changing the proguardFiles
5 | # directive in build.gradle.
6 | #
7 | # For more details, see
8 | # http://developer.android.com/guide/developing/tools/proguard.html
9 |
10 | # Add any project specific keep options here:
11 |
12 | # If your project uses WebView with JS, uncomment the following
13 | # and specify the fully qualified class name to the JavaScript interface
14 | # class:
15 | #-keepclassmembers class fqcn.of.javascript.interface.for.webview {
16 | # public *;
17 | #}
18 |
--------------------------------------------------------------------------------
/snake/proguard-rules.pro:
--------------------------------------------------------------------------------
1 | # Add project specific ProGuard rules here.
2 | # By default, the flags in this file are appended to flags specified
3 | # in /Users/txus/Android/android-sdk-macosx/tools/proguard/proguard-android.txt
4 | # You can edit the include path and order by changing the proguardFiles
5 | # directive in build.gradle.
6 | #
7 | # For more details, see
8 | # http://developer.android.com/guide/developing/tools/proguard.html
9 |
10 | # Add any project specific keep options here:
11 |
12 | # If your project uses WebView with JS, uncomment the following
13 | # and specify the fully qualified class name to the JavaScript interface
14 | # class:
15 | #-keepclassmembers class fqcn.of.javascript.interface.for.webview {
16 | # public *;
17 | #}
18 |
--------------------------------------------------------------------------------
/gradle.properties:
--------------------------------------------------------------------------------
1 | # Project-wide Gradle settings.
2 |
3 | # IDE (e.g. Android Studio) users:
4 | # Gradle settings configured through the IDE *will override*
5 | # any settings specified in this file.
6 |
7 | # For more details on how to configure your build environment visit
8 | # http://www.gradle.org/docs/current/userguide/build_environment.html
9 |
10 | # Specifies the JVM arguments used for the daemon process.
11 | # The setting is particularly useful for tweaking memory settings.
12 | # Default value: -Xmx10248m -XX:MaxPermSize=256m
13 | # org.gradle.jvmargs=-Xmx2048m -XX:MaxPermSize=512m -XX:+HeapDumpOnOutOfMemoryError -Dfile.encoding=UTF-8
14 |
15 | # When configured, Gradle will run in incubating parallel mode.
16 | # This option should only be used with decoupled projects. More details, visit
17 | # http://www.gradle.org/docs/current/userguide/multi_project_builds.html#sec:decoupled_projects
18 | # org.gradle.parallel=true
--------------------------------------------------------------------------------
/.travis.yml:
--------------------------------------------------------------------------------
1 | sudo: required
2 | language: android
3 | env:
4 | matrix:
5 | - ANDROID_TARGET=android-23 ANDROID_ABI=armeabi-v7a
6 | android:
7 | components:
8 | # The BuildTools version used by your project
9 | - build-tools-23.0.1
10 |
11 | # The SDK version used to compile your project
12 | - $ANDROID_TARGET
13 |
14 | # Additional components
15 | - extra-google-google_play_services
16 | - extra-google-m2repository
17 | - extra-android-m2repository
18 | # - addon-google_apis-google-22
19 |
20 | # Specify at least one system image,
21 | # if you need to run emulator(s) during your tests
22 | # - sys-img-armeabi-v7a-android-22
23 | # - sys-img-x86-android-17
24 |
25 | # Emulator Management: Create, Start and Wait
26 | before_script:
27 | # - echo no | android create avd --force -n test -t $ANDROID_TARGET --abi $ANDROID_ABI
28 | # - emulator -avd test -no-skin -no-audio -no-window &
29 | # - android-wait-for-emulator
30 | # - adb shell input keyevent 82 &
31 |
32 | script: gradle build -x lint
33 |
--------------------------------------------------------------------------------
/settings.gradle:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright Txus Ballesteros 2015 (@txusballesteros)
3 | *
4 | * This file is part of some open source application.
5 | *
6 | * Licensed to the Apache Software Foundation (ASF) under one
7 | * or more contributor license agreements. See the NOTICE file
8 | * distributed with this work for additional information
9 | * regarding copyright ownership. The ASF licenses this file
10 | * to you under the Apache License, Version 2.0 (the
11 | * "License"); you may not use this file except in compliance
12 | * with the License. You may obtain a copy of the License at
13 | *
14 | * http://www.apache.org/licenses/LICENSE-2.0
15 | *
16 | * Unless required by applicable law or agreed to in writing,
17 | * software distributed under the License is distributed on an
18 | * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
19 | * KIND, either express or implied. See the License for the
20 | * specific language governing permissions and limitations
21 | * under the License.
22 | *
23 | * Contact: Txus Ballesteros
24 | */
25 | include ':app', ':snake'
26 |
--------------------------------------------------------------------------------
/snake/src/main/AndroidManifest.xml:
--------------------------------------------------------------------------------
1 |
2 |
26 |
27 |
--------------------------------------------------------------------------------
/app/src/main/res/values/strings.xml:
--------------------------------------------------------------------------------
1 |
2 |
26 |
27 | Snake
28 |
29 |
--------------------------------------------------------------------------------
/app/src/main/res/values/styles.xml:
--------------------------------------------------------------------------------
1 |
2 |
26 |
27 |
28 |
29 |
--------------------------------------------------------------------------------
/app/src/main/res/values/colors.xml:
--------------------------------------------------------------------------------
1 |
2 |
26 |
27 | #ffbf080B
28 | #80bf080B
29 |
--------------------------------------------------------------------------------
/app/src/main/res/layout/include_vertical_space.xml:
--------------------------------------------------------------------------------
1 |
2 |
26 |
27 |
28 |
31 |
32 |
--------------------------------------------------------------------------------
/.gitignore:
--------------------------------------------------------------------------------
1 | # Copyright: Benjamin Weiss (keyboardsurfer) https://github.com/keyboardsurfer
2 | # Under CC-BY-SA V3.0 (https://creativecommons.org/licenses/by-sa/3.0/legalcode)
3 |
4 | # built application files
5 | *.apk
6 | *.ap_
7 |
8 | # lint folder
9 | lint
10 |
11 | # files for the dex VM
12 | *.dex
13 |
14 | # Java class files
15 | *.class
16 |
17 | # generated files
18 | bin/
19 | gen/
20 | classes/
21 | gen-external-apklibs/
22 |
23 | # maven output folder
24 | target
25 |
26 | # Local configuration file (sdk path, etc)
27 | local.properties
28 |
29 | # Eclipse project files
30 | .classpath
31 | .project
32 | .metadata
33 | .settings
34 |
35 | # IntelliJ files
36 | .idea
37 | *.iml
38 |
39 | # OSX files
40 | .DS_Store
41 |
42 | # Windows files
43 | Thumbs.db
44 |
45 | # vi swap files
46 | *.swp
47 |
48 | # backup files
49 | *.bak
50 |
51 | # gradle directory
52 | .gradle
53 | gradlew
54 | gradlew.bat
55 | gradle/
56 | build/
57 | captures/
58 |
59 | #for oh-my-zsh jira plugin (https://github.com/robbyrussell/oh-my-zsh/wiki/Plugins#jira)
60 | .jira-url
61 | atlassian-ide-plugin.xml
62 |
63 | # Custom: deploy for BB Playbook and device-info files
64 | device-pass.novcs
65 | device-ip.novcs
66 | bar-deploy.sh
67 |
68 | # Hidden Mac OS files
69 | .DS_Store
70 |
71 | HockeySDK.jar.properties
72 | ant.properties
73 | proguard
74 |
75 | *.jar.properties
76 |
77 | /local.properties
78 |
79 | /AndroidManifest.xml.bak
80 |
81 | /local.properties
82 |
83 | /AndroidManifest.xml.bak
84 |
85 | # newrelic
86 | .nrwork
87 |
88 | keystore
89 |
--------------------------------------------------------------------------------
/app/build.gradle:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright Txus Ballesteros 2015 (@txusballesteros)
3 | *
4 | * This file is part of some open source application.
5 | *
6 | * Licensed to the Apache Software Foundation (ASF) under one
7 | * or more contributor license agreements. See the NOTICE file
8 | * distributed with this work for additional information
9 | * regarding copyright ownership. The ASF licenses this file
10 | * to you under the Apache License, Version 2.0 (the
11 | * "License"); you may not use this file except in compliance
12 | * with the License. You may obtain a copy of the License at
13 | *
14 | * http://www.apache.org/licenses/LICENSE-2.0
15 | *
16 | * Unless required by applicable law or agreed to in writing,
17 | * software distributed under the License is distributed on an
18 | * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
19 | * KIND, either express or implied. See the License for the
20 | * specific language governing permissions and limitations
21 | * under the License.
22 | *
23 | * Contact: Txus Ballesteros
24 | */
25 | apply plugin: 'com.android.application'
26 |
27 | android {
28 | compileSdkVersion 23
29 | buildToolsVersion "23.0.2"
30 |
31 | defaultConfig {
32 | applicationId "com.txusballesteros.demo"
33 | minSdkVersion 14
34 | targetSdkVersion 23
35 | versionCode 2
36 | versionName "1.1.0"
37 | }
38 | buildTypes {
39 | release {
40 | minifyEnabled false
41 | proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
42 | }
43 | }
44 | }
45 |
46 | dependencies {
47 | compile project(':snake')
48 | compile 'com.android.support:appcompat-v7:23.1.1'
49 | }
50 |
--------------------------------------------------------------------------------
/app/src/main/AndroidManifest.xml:
--------------------------------------------------------------------------------
1 |
2 |
26 |
28 |
29 |
34 |
38 |
39 |
40 |
41 |
42 |
43 |
44 |
45 |
--------------------------------------------------------------------------------
/snake/src/main/res/values/attrs.xml:
--------------------------------------------------------------------------------
1 |
2 |
26 |
27 |
28 |
29 |
30 |
31 |
32 |
33 |
34 |
35 |
36 |
37 |
38 |
39 |
40 |
41 |
42 |
43 |
44 |
45 |
--------------------------------------------------------------------------------
/app/src/main/res/layout/activity_main.xml:
--------------------------------------------------------------------------------
1 |
2 |
26 |
33 |
34 |
35 |
36 |
37 |
38 |
53 |
54 |
--------------------------------------------------------------------------------
/app/src/main/res/layout/include_hrm_counter.xml:
--------------------------------------------------------------------------------
1 |
2 |
26 |
32 |
33 |
39 |
40 |
43 |
44 |
48 |
49 |
54 |
55 |
60 |
61 |
--------------------------------------------------------------------------------
/README.md:
--------------------------------------------------------------------------------
1 | Snake View
2 | =====================
3 |
4 | 
5 |
6 | Snake library is a simple and animation line chart for Android.
7 |
8 | ## Latest Version
9 |
10 | [  ](https://bintray.com/txusballesteros/maven/Snake/_latestVersion)  [](https://android-arsenal.com/details/1/2675)
11 |
12 | ## How to use
13 |
14 | ### Configuring your project dependencies
15 |
16 | Add the library dependency to your build.gradle file.
17 |
18 | ```groovy
19 | dependencies {
20 | ...
21 | compile 'com.txusballesteros:snake:1.0'
22 | }
23 | ```
24 |
25 | ### Adding the view to your layout
26 |
27 | Add the view to your xml layout file.
28 |
29 | ```xml
30 |
34 | ```
35 |
36 | ### Setting the values
37 |
38 | Setting the minimum and maximum values of the scale of the chart.
39 |
40 | ```java
41 | final SnakeView snakeView = (SnakeView)findViewById(R.id.snake);
42 | snakeView.setMinValue(0);
43 | snakeView.setMaxValue(100);
44 | ```
45 |
46 | Adding values to the chart.
47 |
48 | ```java
49 | final SnakeView snakeView = (SnakeView)findViewById(R.id.snake);
50 | snakeView.addValue(100);
51 | ```
52 |
53 | ## License
54 |
55 | Copyright Txus Ballesteros 2015 (@txusballesteros)
56 |
57 | This file is part of some open source application.
58 |
59 | Licensed to the Apache Software Foundation (ASF) under one
60 | or more contributor license agreements. See the NOTICE file
61 | distributed with this work for additional information
62 | regarding copyright ownership. The ASF licenses this file
63 | to you under the Apache License, Version 2.0 (the
64 | "License"); you may not use this file except in compliance
65 | with the License. You may obtain a copy of the License at
66 |
67 | http://www.apache.org/licenses/LICENSE-2.0
68 |
69 | Unless required by applicable law or agreed to in writing,
70 | software distributed under the License is distributed on an
71 | "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
72 | KIND, either express or implied. See the License for the
73 | specific language governing permissions and limitations
74 | under the License.
75 |
76 | Contact: Txus Ballesteros
--------------------------------------------------------------------------------
/app/src/main/java/com/txusballesteros/demo/MainActivity.java:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright Txus Ballesteros 2015 (@txusballesteros)
3 | *
4 | * This file is part of some open source application.
5 | *
6 | * Licensed to the Apache Software Foundation (ASF) under one
7 | * or more contributor license agreements. See the NOTICE file
8 | * distributed with this work for additional information
9 | * regarding copyright ownership. The ASF licenses this file
10 | * to you under the Apache License, Version 2.0 (the
11 | * "License"); you may not use this file except in compliance
12 | * with the License. You may obtain a copy of the License at
13 | *
14 | * http://www.apache.org/licenses/LICENSE-2.0
15 | *
16 | * Unless required by applicable law or agreed to in writing,
17 | * software distributed under the License is distributed on an
18 | * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
19 | * KIND, either express or implied. See the License for the
20 | * specific language governing permissions and limitations
21 | * under the License.
22 | *
23 | * Contact: Txus Ballesteros
24 | */
25 | package com.txusballesteros.demo;
26 |
27 | import android.os.Bundle;
28 | import android.os.Handler;
29 | import android.support.v7.app.AppCompatActivity;
30 | import android.widget.TextView;
31 |
32 | import com.txusballesteros.SnakeView;
33 |
34 | public class MainActivity extends AppCompatActivity {
35 | private float[] values = new float[] { 60, 70, 80, 90, 100,
36 | 150, 150, 160, 170, 175, 180,
37 | 170, 140, 130, 110, 90, 80, 60};
38 | private TextView text;
39 | private SnakeView snakeView;
40 | private int position = 0;
41 | private boolean stop = false;
42 |
43 | @Override
44 | protected void onCreate(Bundle savedInstanceState) {
45 | super.onCreate(savedInstanceState);
46 | setContentView(R.layout.activity_main);
47 | getWindow().setBackgroundDrawable(null);
48 | text = (TextView)findViewById(R.id.text);
49 | snakeView = (SnakeView)findViewById(R.id.snake);
50 | }
51 |
52 | @Override
53 | protected void onStart() {
54 | super.onStart();
55 | stop = false;
56 | generateValue();
57 | }
58 |
59 | @Override
60 | protected void onStop() {
61 | super.onStop();
62 | stop = true;
63 | }
64 |
65 | private void generateValue() {
66 | if (position < (values.length - 1)) {
67 | position++;
68 | } else {
69 | position = 0;
70 | }
71 | float value = values[position];
72 | snakeView.addValue(value);
73 | text.setText(Integer.toString((int)value));
74 | new Handler().postDelayed(new Runnable() {
75 | @Override
76 | public void run() {
77 | if (!stop) {
78 | generateValue();
79 | }
80 | }
81 | }, 500);
82 | }
83 | }
84 |
--------------------------------------------------------------------------------
/snake/build.gradle:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright Txus Ballesteros 2015 (@txusballesteros)
3 | *
4 | * This file is part of some open source application.
5 | *
6 | * Licensed to the Apache Software Foundation (ASF) under one
7 | * or more contributor license agreements. See the NOTICE file
8 | * distributed with this work for additional information
9 | * regarding copyright ownership. The ASF licenses this file
10 | * to you under the Apache License, Version 2.0 (the
11 | * "License"); you may not use this file except in compliance
12 | * with the License. You may obtain a copy of the License at
13 | *
14 | * http://www.apache.org/licenses/LICENSE-2.0
15 | *
16 | * Unless required by applicable law or agreed to in writing,
17 | * software distributed under the License is distributed on an
18 | * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
19 | * KIND, either express or implied. See the License for the
20 | * specific language governing permissions and limitations
21 | * under the License.
22 | *
23 | * Contact: Txus Ballesteros
24 | */
25 | apply plugin: 'com.android.library'
26 | apply plugin: 'com.jfrog.bintray'
27 | apply plugin: 'com.github.dcendents.android-maven'
28 |
29 | android {
30 | compileSdkVersion 23
31 | buildToolsVersion "23.0.1"
32 |
33 | defaultConfig {
34 | minSdkVersion 14
35 | targetSdkVersion 23
36 | versionCode 1
37 | versionName "1.0"
38 | }
39 |
40 | buildTypes {
41 | release {
42 | minifyEnabled false
43 | proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
44 | }
45 | }
46 | }
47 |
48 | version = parent.ext.libraryVersion
49 | group = parent.ext.mavenGroupId
50 | def siteUrl = parent.ext.siteUrl
51 | def gitUrl = parent.ext.gitUrl
52 |
53 | Properties properties = new Properties()
54 | properties.load(project.rootProject.file('local.properties').newDataInputStream())
55 |
56 | bintray {
57 | user = properties.getProperty("bintray.user")
58 | key = properties.getProperty("bintray.apikey")
59 | configurations = ['archives']
60 | pkg {
61 | repo = "maven"
62 | name = "Snake"
63 | websiteUrl = siteUrl
64 | vcsUrl = gitUrl
65 | licenses = ["Apache-2.0"]
66 | publish = true
67 | }
68 | }
69 |
70 | install {
71 | repositories.mavenInstaller {
72 | pom {
73 | project {
74 | packaging 'aar'
75 | name 'Snake'
76 | url siteUrl
77 | licenses {
78 | license {
79 | name 'The Apache Software License, Version 2.0'
80 | url 'http://www.apache.org/licenses/LICENSE-2.0.txt'
81 | }
82 | }
83 | developers {
84 | developer {
85 | id 'txusballesteros'
86 | name 'Txus Ballesteros'
87 | email 'txus.ballesteros@gmail.com'
88 | }
89 | }
90 | scm {
91 | connection gitUrl
92 | developerConnection gitUrl
93 | url siteUrl
94 | }
95 | }
96 | }
97 | }
98 | }
99 |
100 | task sourcesJar(type: Jar) {
101 | from android.sourceSets.main.java.srcDirs
102 | classifier = 'sources'
103 | }
104 |
105 | task javadoc(type: Javadoc) {
106 | source = android.sourceSets.main.java.srcDirs
107 | classpath += project.files(android.getBootClasspath().join(File.pathSeparator))
108 | }
109 |
110 | task javadocJar(type: Jar, dependsOn: javadoc) {
111 | classifier = 'javadoc'
112 | from javadoc.destinationDir
113 | }
114 |
115 | artifacts {
116 | archives javadocJar
117 | archives sourcesJar
118 | }
--------------------------------------------------------------------------------
/snake/src/main/java/com/txusballesteros/SnakeView.java:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright Txus Ballesteros 2015 (@txusballesteros)
3 | *
4 | * This file is part of some open source application.
5 | *
6 | * Licensed to the Apache Software Foundation (ASF) under one
7 | * or more contributor license agreements. See the NOTICE file
8 | * distributed with this work for additional information
9 | * regarding copyright ownership. The ASF licenses this file
10 | * to you under the Apache License, Version 2.0 (the
11 | * "License"); you may not use this file except in compliance
12 | * with the License. You may obtain a copy of the License at
13 | *
14 | * http://www.apache.org/licenses/LICENSE-2.0
15 | *
16 | * Unless required by applicable law or agreed to in writing,
17 | * software distributed under the License is distributed on an
18 | * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
19 | * KIND, either express or implied. See the License for the
20 | * specific language governing permissions and limitations
21 | * under the License.
22 | *
23 | * Contact: Txus Ballesteros
24 | */
25 | package com.txusballesteros;
26 |
27 | import android.animation.ValueAnimator;
28 | import android.annotation.TargetApi;
29 | import android.content.Context;
30 | import android.content.res.TypedArray;
31 | import android.graphics.Canvas;
32 | import android.graphics.Paint;
33 | import android.graphics.Path;
34 | import android.graphics.RectF;
35 | import android.util.AttributeSet;
36 | import android.util.TypedValue;
37 | import android.view.View;
38 | import android.view.animation.LinearInterpolator;
39 |
40 | import java.util.ArrayList;
41 | import java.util.List;
42 | import java.util.Queue;
43 | import java.util.concurrent.ConcurrentLinkedQueue;
44 |
45 | public class SnakeView extends View {
46 | private final static int DEFAULT_MAXIMUM_NUMBER_OF_VALUES_FOR_DESIGNER = 3;
47 | private final static int DEFAULT_MAXIMUM_NUMBER_OF_VALUES_FOR_RUNTIME = 10;
48 | private final static int DEFAULT_STROKE_COLOR = 0xff78c257;
49 | private final static int DEFAULT_FILL_COLOR = 0x8078c257;
50 | private final static int DEFAULT_STROKE_WIDTH_IN_DP = 3;
51 | public static final int DEFAULT_ANIMATION_DURATION = 200;
52 | public static final float BEZIER_FINE_FIT = 0.5f;
53 | public static final int DEF_STYLE_ATTR = 0;
54 | public static final int DEF_STYLE_RES = 0;
55 | public static final float DEFAULT_MIN_VALUE = 0f;
56 | public static final float DEFAULT_MAX_VALUE = 1f;
57 | public static final int MINIMUM_NUMBER_OF_VALUES = 3;
58 | public static final int SCALE_MODE_FIXED = 0;
59 | public static final int SCALE_MODE_AUTO = 1;
60 | public static final int CHART_STILE_STROKE = 0;
61 | public static final int CHART_STILE_FILL = 1;
62 | public static final int CHART_STILE_FILL_STROKE = 2;
63 | private int maximumNumberOfValues = DEFAULT_MAXIMUM_NUMBER_OF_VALUES_FOR_RUNTIME;
64 | private int strokeColor = DEFAULT_STROKE_COLOR;
65 | private int strokeWidthInPx = (int)dp2px(DEFAULT_STROKE_WIDTH_IN_DP);
66 | private int fillColor = DEFAULT_FILL_COLOR;
67 | private RectF drawingArea;
68 | private Paint paint;
69 | private Paint fillPaint;
70 | private Queue valuesCache;
71 | private List previousValuesCache;
72 | private List currentValuesCache;
73 | private int animationDuration = DEFAULT_ANIMATION_DURATION;
74 | private float animationProgress = 1.0f;
75 | private float scaleInX = 0f;
76 | private float scaleInY = 0f;
77 | private float minValue = DEFAULT_MIN_VALUE;
78 | private float maxValue = DEFAULT_MAX_VALUE;
79 | private int scaleMode = SCALE_MODE_FIXED;
80 | private int chartStyle = CHART_STILE_STROKE;
81 |
82 |
83 | public void setMaximumNumberOfValues(int maximumNumberOfValues) {
84 | if (maximumNumberOfValues < MINIMUM_NUMBER_OF_VALUES) {
85 | throw new IllegalArgumentException("The maximum number of values cannot be less than three.");
86 | }
87 | this.maximumNumberOfValues = maximumNumberOfValues;
88 | calculateScales();
89 | initializeCaches();
90 | }
91 |
92 | public void setScaleMode(int scaleMode) {
93 | this.scaleMode = scaleMode;
94 | calculateScales();
95 | initializeCaches();
96 | }
97 |
98 | public void setMinValue(float minValue) {
99 | this.minValue = minValue;
100 | calculateScales();
101 | initializeCaches();
102 | }
103 |
104 | public void setMaxValue(float maxValue) {
105 | this.maxValue = maxValue;
106 | calculateScales();
107 | initializeCaches();
108 | }
109 |
110 | public void addValue(float value) {
111 | if (scaleMode == SCALE_MODE_FIXED) {
112 | if (value < minValue || value > maxValue) {
113 | throw new IllegalArgumentException("The value is out of min or max limits.");
114 | }
115 | }
116 | previousValuesCache = cloneCache();
117 | if (valuesCache.size() == maximumNumberOfValues) {
118 | valuesCache.poll();
119 | }
120 | valuesCache.add(value);
121 | currentValuesCache = cloneCache();
122 | if (scaleMode == SCALE_MODE_AUTO) {
123 | calculateScales();
124 | }
125 | playAnimation();
126 | }
127 |
128 | public void clear() {
129 | initializeCaches();
130 | invalidate();
131 | }
132 |
133 | public SnakeView(Context context) {
134 | super(context);
135 | initializeView();
136 | }
137 |
138 | public SnakeView(Context context, AttributeSet attrs) {
139 | super(context, attrs);
140 | configureAttributes(attrs);
141 | initializeView();
142 | }
143 |
144 | public SnakeView(Context context, AttributeSet attrs, int defStyleAttr) {
145 | super(context, attrs, defStyleAttr);
146 | configureAttributes(attrs);
147 | initializeView();
148 | }
149 |
150 | @TargetApi(21)
151 | public SnakeView(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) {
152 | super(context, attrs, defStyleAttr, defStyleRes);
153 | configureAttributes(attrs);
154 | initializeView();
155 | }
156 |
157 | private void configureAttributes(AttributeSet attrs) {
158 | TypedArray attributes = getContext().getTheme()
159 | .obtainStyledAttributes(attrs, R.styleable.SnakeView,
160 | DEF_STYLE_ATTR, DEF_STYLE_RES);
161 | scaleMode = attributes.getInteger(R.styleable.SnakeView_scaleMode, scaleMode);
162 | strokeColor = attributes.getColor(R.styleable.SnakeView_strokeColor, strokeColor);
163 | fillColor = attributes.getColor(R.styleable.SnakeView_fillColor, fillColor);
164 | strokeWidthInPx = attributes.getDimensionPixelSize(R.styleable.SnakeView_strokeWidth, strokeWidthInPx);
165 | chartStyle = attributes.getInteger(R.styleable.SnakeView_chartStyle, chartStyle);
166 | if (scaleMode == SCALE_MODE_FIXED) {
167 | minValue = attributes.getFloat(R.styleable.SnakeView_minValue, DEFAULT_MIN_VALUE);
168 | maxValue = attributes.getFloat(R.styleable.SnakeView_maxValue, DEFAULT_MAX_VALUE);
169 | }
170 | int defaultMaximumNumberOfValues = DEFAULT_MAXIMUM_NUMBER_OF_VALUES_FOR_RUNTIME;
171 | if (isInEditMode()) {
172 | defaultMaximumNumberOfValues = DEFAULT_MAXIMUM_NUMBER_OF_VALUES_FOR_DESIGNER;
173 | }
174 | maximumNumberOfValues = attributes.getInteger(R.styleable.SnakeView_maximumNumberOfValues,
175 | defaultMaximumNumberOfValues);
176 | animationDuration = attributes.getInteger(R.styleable.SnakeView_animationDuration,
177 | DEFAULT_ANIMATION_DURATION);
178 | if (maximumNumberOfValues < MINIMUM_NUMBER_OF_VALUES) {
179 | throw new IllegalArgumentException("The maximum number of values cannot be less than three.");
180 | }
181 | attributes.recycle();
182 | }
183 |
184 | private void initializeView() {
185 | initializePaint();
186 | initializeCaches();
187 | }
188 |
189 | private void initializePaint() {
190 | paint = new Paint();
191 | paint.setFlags(Paint.ANTI_ALIAS_FLAG);
192 | paint.setColor(strokeColor);
193 | paint.setStyle(Paint.Style.STROKE);
194 | if (chartStyle == CHART_STILE_STROKE) {
195 | paint.setStrokeCap(Paint.Cap.ROUND);
196 | }
197 | paint.setStrokeWidth(strokeWidthInPx);
198 | fillPaint = new Paint();
199 | fillPaint.setFlags(Paint.ANTI_ALIAS_FLAG);
200 | fillPaint.setColor(fillColor);
201 | fillPaint.setStyle(Paint.Style.FILL);
202 | }
203 |
204 | private void initializeCaches() {
205 | if (isInEditMode()) {
206 | initializeCacheForDesigner();
207 | } else {
208 | initializeCacheForRuntime();
209 | }
210 | previousValuesCache = cloneCache();
211 | currentValuesCache = cloneCache();
212 | }
213 |
214 | private void initializeCacheForDesigner() {
215 | valuesCache = new ConcurrentLinkedQueue<>();
216 | for (int counter = 0; counter < maximumNumberOfValues; counter++) {
217 | if (counter % 2 == 0) {
218 | valuesCache.add(minValue);
219 | } else {
220 | valuesCache.add(maxValue);
221 | }
222 | }
223 | }
224 |
225 | private void initializeCacheForRuntime() {
226 | valuesCache = new ConcurrentLinkedQueue<>();
227 | for (int counter = 0; counter < maximumNumberOfValues; counter++) {
228 | valuesCache.add(minValue);
229 | }
230 | }
231 |
232 | private List cloneCache() {
233 | return new ArrayList<>(valuesCache);
234 | }
235 |
236 | private float dp2px(float value) {
237 | return TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, value,
238 | getResources().getDisplayMetrics());
239 | }
240 |
241 | @Override
242 | protected void onSizeChanged(int width, int height, int oldWidth, int oldHeight) {
243 | calculateDrawingArea(width, height);
244 | calculateScales();
245 | }
246 |
247 | private void calculateDrawingArea(int width, int height) {
248 | int left = (strokeWidthInPx * 2) + getPaddingLeft();
249 | int top = (strokeWidthInPx * 2) + getPaddingTop();
250 | int right = width - getPaddingRight() - strokeWidthInPx;
251 | int bottom = height - getPaddingBottom() - strokeWidthInPx;
252 | drawingArea = new RectF(left, top, right, bottom);
253 | }
254 |
255 | @Override
256 | protected void onDraw(Canvas canvas) {
257 | super.onDraw(canvas);
258 | if (!valuesCache.isEmpty()) {
259 | if (chartStyle == CHART_STILE_FILL || chartStyle == CHART_STILE_FILL_STROKE) {
260 | Path fillPath = buildPath(true);
261 | canvas.drawPath(fillPath, fillPaint);
262 | }
263 |
264 | if (chartStyle == CHART_STILE_STROKE || chartStyle == CHART_STILE_FILL_STROKE) {
265 | Path strokePath = buildPath(false);
266 | canvas.drawPath(strokePath, paint);
267 | }
268 | }
269 | }
270 |
271 | private Path buildPath(boolean forFill) {
272 | int strokePadding = (strokeWidthInPx / 2);
273 | Path path = new Path();
274 | if (forFill) {
275 | path.moveTo(drawingArea.left, drawingArea.bottom + strokePadding);
276 | }
277 | float previousX = drawingArea.left;
278 | float previousY = drawingArea.bottom;
279 | for (int index = 0; index < currentValuesCache.size(); index++) {
280 | float previousValue = previousValuesCache.get(index);
281 | float currentValue = currentValuesCache.get(index);
282 | float pathValue = previousValue + ((currentValue - previousValue) * animationProgress);
283 | float x = drawingArea.left + (scaleInX * index);
284 | float y = drawingArea.bottom - ((pathValue - minValue) * scaleInY);
285 | if (index == 0) {
286 | if (forFill) {
287 | path.lineTo(x, y);
288 | } else {
289 | path.moveTo(x, y);
290 | }
291 | } else {
292 | float bezierControlX = previousX + ((x - previousX) * BEZIER_FINE_FIT);
293 | float controlPointX1 = bezierControlX;
294 | float controlPointY1 = previousY;
295 | float controlPointX2 = bezierControlX;
296 | float controlPointY2 = y;
297 | float endPointX = x;
298 | float endPointY = y;
299 | path.cubicTo(controlPointX1, controlPointY1,
300 | controlPointX2, controlPointY2,
301 | endPointX, endPointY);
302 | }
303 | previousX = x;
304 | previousY = y;
305 | }
306 | if (forFill) {
307 | path.lineTo(drawingArea.right, drawingArea.bottom + strokePadding);
308 | }
309 | return path;
310 | }
311 |
312 | private void calculateScales() {
313 | if (drawingArea != null) {
314 | if (scaleMode == SCALE_MODE_AUTO) {
315 | for (int index = 0; index < currentValuesCache.size(); index++) {
316 | float previousValue = previousValuesCache.get(index);
317 | float currentValue = currentValuesCache.get(index);
318 | if (index == 0) {
319 | minValue = Math.min(currentValue, previousValue);
320 | maxValue = Math.max(currentValue, previousValue);
321 | } else {
322 | minValue = Math.min(minValue, currentValue);
323 | maxValue = Math.max(maxValue, currentValue);
324 | minValue = Math.min(minValue, previousValue);
325 | maxValue = Math.max(maxValue, previousValue);
326 | }
327 | }
328 | }
329 | scaleInX = (drawingArea.width() / (maximumNumberOfValues - 1));
330 | scaleInY = (drawingArea.height() / (maxValue - minValue));
331 | } else {
332 | scaleInY = 0f;
333 | scaleInX = 0f;
334 | }
335 | }
336 |
337 | private void playAnimation() {
338 | ValueAnimator animator = ValueAnimator.ofFloat(0f, 1f);
339 | animator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
340 | @Override
341 | public void onAnimationUpdate(ValueAnimator animation) {
342 | SnakeView.this.animationProgress = (float)animation.getAnimatedValue();
343 | invalidate();
344 | }
345 | });
346 | animator.setDuration(animationDuration);
347 | animator.setInterpolator(new LinearInterpolator());
348 | animator.start();
349 | }
350 | }
--------------------------------------------------------------------------------