如何在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并停止数字仪表盘,如下所示:
点击此处下载项目代码
广告