Friday, 15 December 2017

HB Blog 150: ScArcGauge View In Android.

ScArcGauge class is a specialized to create an arc gauge.
By default the arc create a closed circle: from 0° to 360°. Note that all angle is usually expressed in degrees and almost methods need to have an delta angle relative to the start angle.
This class extend the ScGauge class.
ScGauge class is studied to be an "helper class" to facilitate the user to create a gauge. The path is generic and must be defined in a inherited class. In this class are exposed many methods to drive the most used properties from the code or directly from the XML. To manage the features will recognized from the class type and its tag so changing, for example, the color of notches you will change the color of all notches tagged. This is useful when you have a custom features configuration that use one more of feature per type. All the custom features added without a defined tag should be managed by the user by himself.
Refer the below link for complete sample code:-

Download Sample Code

Have a look on few code snippets,

//build.gradle
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
apply plugin: 'com.android.application'

android {
    compileSdkVersion 25
    buildToolsVersion "25.0.2"
    defaultConfig {
        applicationId "com.harshalbenake.gaugeview"
        minSdkVersion 15
        targetSdkVersion 25
        versionCode 1
        versionName "1.0"
    }
    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
        }
    }
}

dependencies {
    compile fileTree(dir: 'libs', include: ['*.jar'])
    compile 'com.github.paroca72:sc-gauges:2.5.3'
}

//activity_main.xml
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
<?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"
    android:layout_width="match_parent"
    android:layout_height="match_parent">
    <FrameLayout
        android:layout_width="match_parent"
        android:layout_height="162dp"
        android:background="#354051">

        <com.sccomponents.gauges.ScArcGauge
            android:id="@+id/gauge"
            xmlns:sc="http://schemas.android.com/apk/res-auto"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_gravity="center_horizontal"
            android:padding="30dp"
            sc:scc_angle_start="-180"
            sc:scc_angle_sweep="180"
            sc:scc_progress_color="#1eff00"
            sc:scc_progress_size="15dp"
            sc:scc_stroke_color="#ffffff"
            sc:scc_stroke_size="30dp"
            sc:scc_notches="3"
            sc:scc_notches_length="32dp"
            sc:scc_notches_size="6dp"
            sc:scc_notches_color="#354051"
            sc:scc_text_tokens="1000|2000|3000"
            sc:scc_stroke_colors="#EC4949|#00B4FF|#F7AD36"
            sc:scc_stroke_colors_mode="solid"
            sc:scc_text_align="center"
            sc:scc_text_position="middle"
            sc:scc_text_color="#354051"/>

        <ImageView
            android:id="@+id/indicator"
            android:layout_width="64dp"
            android:layout_height="wrap_content"
            android:layout_gravity="bottom|center_horizontal"
            android:src="@drawable/indicator"
            android:layout_marginLeft="18dp"
            android:layout_marginBottom="29dp"/>

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="0000"
            android:layout_marginBottom="2dp"
            android:id="@+id/counter"
            android:layout_gravity="bottom|center_horizontal"
            android:textColor="#ffffff"
            android:textSize="20dp"/>

    </FrameLayout>

</LinearLayout>

//MainActivity.java
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
package com.harshalbenake.gaugeview;

import android.app.Activity;
import android.os.Bundle;
import android.widget.ImageView;
import android.widget.TextView;
import com.sccomponents.gauges.ScArcGauge;
import com.sccomponents.gauges.ScGauge;

public class MainActivity extends Activity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        // Find the components
        final ScArcGauge gauge = (ScArcGauge) this.findViewById(R.id.gauge);
        assert gauge != null;

        final ImageView indicator = (ImageView) this.findViewById(R.id.indicator);
        assert indicator != null;

        final TextView counter = (TextView) this.findViewById(R.id.counter);
        assert counter != null;

        // Set the center pivot for a right rotation
        indicator.setPivotX(30f);
        indicator.setPivotY(30f);

        // As the progress feature by default the last to be draw I must bring the notches feature
        // on top.
        gauge.bringOnTop(ScGauge.NOTCHES_IDENTIFIER);

        // If you set the value from the xml that not produce an event so I will change the
        // value from code.
        gauge.setHighValue(60);

        // Each time I will change the value I must write it inside the counter text.
        gauge.setOnEventListener(new ScGauge.OnEventListener() {
            @Override
            public void onValueChange(float lowValue, float highValue) {
                // Convert the percentage value in an angle
                float angle = gauge.percentageToAngle(highValue);
                indicator.setRotation(angle);

                // Write the value
                int value = (int) ScGauge.percentageToValue(highValue, 0.0f, 3000.0f);
                counter.setText(value + "");
            }
        });
    }
}

No comments:

Post a Comment