Linux教程網
Linux教程網
Linux教程網
您现在的位置: Linux教程網 >> UnixLinux >  >> Linux 程式設計 >> Linux 程式設計

Android 自製刻度尺控件:可滑動的刻度與實時數值顯示

剛接觸 Android 不久,因為公司專案需要,我寫了一個類似刻度尺的控件,拿出來拋磚引玉,希望大家多給意見。

先說一個小提醒:KeduView 中的浮點數計算我沒有特別處理(因為精度問題,浮點數直接相減可能得到不精確的結果);而 StaffView 中我對浮點數做了處理,在 Arithmetic 類裡封裝了加減乘除與四捨五入方法,以確保顯示的數值準確。

主要程式碼

[java]
  1. package com.hyx.suiyipaint;
  2. import android.app.Activity;
  3. import android.content.Context;
  4. import android.graphics.Bitmap;
  5. import android.graphics.BitmapFactory;
  6. import android.graphics.Canvas;
  7. import android.graphics.Color;
  8. import android.graphics.Matrix;
  9. import android.graphics.Paint;
  10. import android.graphics.Rect;
  11. import android.os.Bundle;
  12. import android.view.KeyEvent;
  13. import android.view.MotionEvent;
  14. import android.view.SurfaceHolder;
  15. import android.view.SurfaceView;
  16. import android.view.View;
  17. import android.widget.ImageView;
  18. import android.widget.LinearLayout;
  19. public class KeduActivity extends Activity {
  20.     private ImageView kedu_tiao;
  21.     private LinearLayout kedu_linear;
  22.     private LinearLayout staff_linear;
  23.     private KeduView kedu;
  24.     private StaffView staff;
  25.     @Override
  26.     protected void onCreate(Bundle savedInstanceState) {
  27.         super.onCreate(savedInstanceState);
  28.         setContentView(R.layout.kedu);
  29.         kedu_linear = (LinearLayout)findViewById(R.id.kedu_linear);
  30.         kedu = new KeduView(this, 0f, 0.1f);
  31.         kedu_linear.addView(kedu);
  32.         staff_linear = (LinearLayout)findViewById(R.id.staff_linear);
  33.         staff = new StaffView(this, 7.5f, 0.5f, "cm");
  34.         staff_linear.addView(staff);
  35.         kedu_tiao = (ImageView)findViewById(R.id.kedu_tiao);
  36.         kedu_tiao.setOnTouchListener(keduListener);
  37.     }
  38.     private ImageView.OnTouchListener keduListener = new ImageView.OnTouchListener(){
  39.         private float initx = 0;
  40.         @Override
  41.         public boolean onTouch(View v, MotionEvent event) {
  42.              switch(event.getAction()){
  43.                  case MotionEvent.ACTION_DOWN:
  44.                      initx = event.getX();
  45.                  break;
  46.                  case MotionEvent.ACTION_MOVE:
  47.                      float lastx = event.getX();
  48.                      if(lastx > initx + 5){
  49.                          kedu.draw(1);
  50.                          initx = lastx;
  51.                      }else if(lastx < initx - 5){
  52.                          kedu.draw(-1);
  53.                          initx = lastx;
  54.                      }
  55.                  break;
  56.              }
  57.              return true;
  58.         }
  59.     };
  60.     // KeduView 與 StaffView 內部類省略部分已在原文中給出,
  61.     // 核心為:SurfaceView + SurfaceHolder.Callback + Runnable,
  62.     // 透過 draw(int direction) 依方向 -1/1/0 移動游標並重繪刻度與數值。
  63.     @Override
  64.     public boolean onKeyDown(int keyCode, KeyEvent event) {
  65.         if(keyCode == KeyEvent.KEYCODE_VOLUME_DOWN){
  66.              staff.isInit = false;
  67.              staff.draw(-1);
  68.              return true;
  69.         }
  70.         if(keyCode == KeyEvent.KEYCODE_VOLUME_UP){
  71.              staff.isInit = false;
  72.              staff.draw(1);
  73.              return true;
  74.         }
  75.         return super.onKeyDown(keyCode, event);
  76.     }
  77. }

說明一下兩個內部View的運作方式:KeduView 與 StaffView 都繼承 SurfaceView 並實現 SurfaceHolder.Callback 與 Runnable,在 surfaceCreated 時啟動執行緒調用 draw(0) 完成初始繪製;draw(int direction) 根據方向參數(-1 向左、1 向右、0 不動)移動游標坐標、更新數值,再重新繪製背景、游標、刻度數字與結果文字,最後解鎖畫布提交。StaffView 還額外封裝了 Arithmetic 的加減與四捨五入,避免浮點數精度帶來的誤差。

布局文件

[xml]
  1. <?xml version="1.0" encoding="utf-8"?>
  2. <LinearLayout
  3.   xmlns:android="https://schemas.android.com/apk/res/android"
  4.   android:orientation="vertical"
  5.   android:layout_width="fill_parent"
  6.   android:layout_height="fill_parent"
  7.   android:gravity="center"
  8.   android:background="#fff">
  9.     <LinearLayout
  10.         android:id="@+id/kedu_linear"
  11.         android:layout_width="294dp"
  12.         android:layout_height="101dp"/>
  13.     <ImageView
  14.         android:layout_height="wrap_content"
  15.         android:layout_width="wrap_content"
  16.         android:id="@+id/kedu_tiao"
  17.         android:src="@drawable/kedu_wheel_01"
  18.         android:layout_margin="20dp"/>
  19.     <LinearLayout
  20.         android:id="@+id/staff_linear"
  21.         android:layout_width="294dp"
  22.         android:layout_height="101dp"/>
  23. </LinearLayout>

運行後即可看到可滑動的刻度尺效果:拖動滾輪或使用音量鍵,都能讓游標左右移動並實時更新顯示的數值,適合需要類似儀表、量尺交互的場景。

Copyright © Linux教程網 All Rights Reserved