如何在Android中建立DigitalSpeedDashboard?


此示例演示瞭如何在Android中建立DigitalSpeedDashboard。

步驟1 - 在Android Studio中建立一個新專案,轉到檔案⇒新建專案,並填寫所有必需的詳細資訊以建立新專案。

步驟2 - 開啟build.gradle並新增如下所示的庫依賴項:

apply plugin: 'com.android.application'
android {
   compileSdkVersion 28
   defaultConfig {
      applicationId "com.example.andy.myapplication"
      minSdkVersion 19
      targetSdkVersion 28
      versionCode 1
      versionName "1.0"
      testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
   }
   configurations {
      cleanedAnnotations
      compile.exclude group: 'org.jetbrains' , module:'annotations'
   }
   buildTypes {
      release {
         minifyEnabled false
         proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
      }
   }
}
dependencies {
   implementation fileTree(dir: 'libs', include: ['*.jar'])
   implementation 'com.android.support:appcompat-v7:28.0.0'
   implementation 'com.github.ngallazzi:DigitalSpeedDashboard:master-SNAPSHOT'
   implementation 'com.android.support.constraint:constraint-layout:1.1.3'
   testImplementation 'junit:junit:4.12'
   androidTestImplementation 'com.android.support.test:runner:1.0.2'
   androidTestImplementation 'com.android.support.test.espresso:espresso-core:3.0.2'
   implementation 'org.jetbrains:annotations-java5:15.0'
}

現在開啟build.gradle(application)並新增以下幾行:

// Top-level build file where you can add configuration options common to all sub-projects/modules.
buildscript {
   repositories {
      google()
      jcenter()
   }
   dependencies {
      classpath 'com.android.tools.build:gradle:3.2.1'
      // NOTE: Do not place your application dependencies here; they belong
      // in the individual module build.gradle files
   }
}
allprojects {
   repositories {
      google()
      jcenter()
      maven { url 'https://jitpack.io' }
   }
}
task clean(type: Delete) {
   delete rootProject.buildDir
}

步驟3 - 將以下程式碼新增到res/layout/activity_main.xml。

<?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"
   android:orientation = "vertical"
   tools:context = ".MainActivity">
   <com.ngallazzi.speedandrpmdashboard.DigitalSpeedDashboard
      android:id = "@+id/srDashboard"
      android:layout_width = "350dp"
      android:layout_height = "350dp"
      app:idleColor = "#35ABABAB"
      android:background = "#000000"
      app:speedColor = "#FFFFFF"/>
   <TextView
      android:id = "@+id/text"
      android:layout_width = "wrap_content"
      android:layout_height = "wrap_content"
      android:text = "Start Dashboard"
      android:textSize = "25sp" />
</LinearLayout>

在上面的程式碼中,我們使用了TextView和digitalSpeedDashboard。當用戶點選TextView時,它將啟動digitalSpeedDashboard。

步驟4 - 將以下程式碼新增到src/MainActivity.java

package com.example.andy.myapplication;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.TextView;
import com.ngallazzi.speedandrpmdashboard.DigitalSpeedDashboard;
public class MainActivity extends AppCompatActivity {
   TextView text;
   int speed;
   boolean time;
   DigitalSpeedDashboard digitalSpeedDashboard;
   @Override
   protected void onCreate(Bundle savedInstanceState) {
      super.onCreate(savedInstanceState);
      setContentView(R.layout.activity_main);
      text = findViewById(R.id.text);
      digitalSpeedDashboard = findViewById(R.id.srDashboard);
      digitalSpeedDashboard.setMaxSpeed(200);
      text.setOnClickListener(new View.OnClickListener() {
         @Override
         public void onClick(View v) {
            new Thread(new Runnable() {
               @Override
               public void run() {
                  time = true;
                  while (time) {
                     speed++;
                     digitalSpeedDashboard.setSpeed(speed);
                     try {
                        Thread.sleep(10);
                     } catch (InterruptedException e) {
                        e.printStackTrace();
                     }
                     if (speed == 200) {
                        time = false;
                        speed = 0;
                     }
                  }
               }
            }).start();
         }
      });
   }
}

讓我們嘗試執行您的應用程式。我假設您已將您的實際Android移動裝置連線到您的計算機。要從Android Studio執行應用程式,請開啟專案的Activity檔案之一,然後單擊工具欄中的執行 圖示。選擇您的移動裝置作為選項,然後檢查您的移動裝置,它將顯示您的預設螢幕:

上面的螢幕是初始螢幕,當用戶點選TextView時,它將啟動數字儀表盤,最終達到200並停止數字儀表盤,如下所示:


點選此處下載專案程式碼

更新於:2019年7月30日

99 次瀏覽

啟動您的職業生涯

透過完成課程獲得認證

開始
廣告