服务器之家:专注于VPS、云服务器配置技术及软件下载分享
分类导航

PHP教程|ASP.NET教程|Java教程|ASP教程|编程技术|正则表达式|C/C++|IOS|C#|Swift|Android|VB|R语言|JavaScript|易语言|vb.net|

服务器之家 - 编程语言 - Android - android自定义手表效果

android自定义手表效果

2022-11-15 16:06cf8833 Android

这篇文章主要为大家详细介绍了android自定义手表效果,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下

本文实例为大家分享了android自定义手表效果的具体代码,供大家参考,具体内容如下

1.效果图:

android自定义手表效果

2.布局

?
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
<?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"
 >
 
 <Button
  android:id="@+id/btn_start"
  android:layout_width="wrap_content"
  android:layout_height="wrap_content"
  android:text="start" />
 .
 
 <Button
  android:id="@+id/btn_stop"
  android:layout_width="wrap_content"
  android:layout_height="wrap_content"
  android:text="stop" />
 
 <ImageView
  android:id="@+id/iv_clock"
  android:layout_width="wrap_content"
  android:layout_height="wrap_content"
  android:layout_gravity="center"
  android:src="@mipmap/ic_launcher" />
</LinearLayout>

3.自定义view,显示

?
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
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
package com.example.administrator.testz;
 
 
import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.os.AsyncTask;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
 
import java.util.Calendar;
 
/**
 * 优化方案:
 * 表盘课绘制一次
 * 在分线程中进行加载
 */
public class MainActivity extends AppCompatActivity implements View.OnClickListener {
 private Button btnStart, btnStop;
 private ImageView mClockImageView;
 Bitmap.Config config = Bitmap.Config.ARGB_8888;
 int width = 500;
 int height = 500;
 
 private Calendar mCalendar;
 private int mHour, mMinute, mSecond;
 private float mDegrees;
 private float length;
 
 private boolean mIsRunning;
 
 @Override
 protected void onCreate(Bundle savedInstanceState) {
  super.onCreate(savedInstanceState);
  setContentView(R.layout.activity_main);
 
  btnStart = (Button) findViewById(R.id.btn_start);
  btnStop = (Button) findViewById(R.id.btn_stop);
  btnStop.setOnClickListener(this);
  btnStart.setOnClickListener(this);
 
  mClockImageView = (ImageView) findViewById(R.id.iv_clock);
  mClockImageView.setImageBitmap(drawClock());
 
 }
 
 /**
  * 画表盘
  */
 private Bitmap drawClockFace() {
  Bitmap bm = Bitmap.createBitmap(width, height, config);
 
  Canvas canvas = new Canvas(bm);
  Paint paint = new Paint();
  paint.setAntiAlias(true); //锯齿
  paint.setStyle(Paint.Style.STROKE); // 空心
  paint.setStrokeWidth(5);
  paint.setColor(Color.parseColor("#333333"));
 
  // 外层圆
  canvas.drawCircle(width / 2, height / 2, width / 2, paint);
 
  // 內层圆 --》圆心
  paint.setStyle(Paint.Style.FILL);
  canvas.drawCircle(width / 2, height / 2, 10, paint);
 
  // 循环画刻度(旋转画刻度)
  for (int i = 0; i < 12; i++) {
   if (i % 3 == 0) {
    paint.setStrokeWidth(10);
    canvas.drawLine(width / 2, 0, width / 2, 24, paint);
    canvas.rotate(30, width / 2, height / 2);
   } else {
    canvas.drawLine(width / 2, 0, width / 2, 10, paint);
    canvas.rotate(30, width / 2, height / 2);
   }
  }
 
 
  return bm;
 }
 
 private Bitmap drawClock() {
  Bitmap bm = drawClockFace();
 
  Canvas canvas = new Canvas(bm);
 
  Paint paint = new Paint();
  paint.setAntiAlias(true);
  paint.setColor(Color.parseColor("#333333"));
 
  mCalendar = Calendar.getInstance();
  mHour = mCalendar.get(Calendar.HOUR);
  mMinute = mCalendar.get(Calendar.MINUTE);
  mSecond = mCalendar.get(Calendar.SECOND);
 
  //画小时指针
  paint.setStrokeWidth(10);
  mDegrees = mHour * 30 + mMinute / 2;
  length = (width / 2) * 0.7f;
  canvas.save();
  canvas.rotate(mDegrees, width / 2, height / 2);
  canvas.drawLine(width / 2, height / 2, width / 2, height - (height / 2) - length, paint);
  canvas.restore();
//  canvas.rotate(360 - mDegrees, width / 2, height / 2);
 
  //画分钟指针
  paint.setStrokeWidth(4);
  mDegrees = mMinute * 6 + mSecond / 10;
  length = (width / 2) * 0.78f;
  canvas.save();
  canvas.rotate(mDegrees, width / 2, height / 2);
  canvas.drawLine(width / 2, height / 2, width / 2, height - (height / 2) - length, paint);
  canvas.restore();
//  canvas.rotate(360 - mDegrees, width / 2, height / 2);
 
  //画分钟指针
  paint.setStrokeWidth(2);
  mDegrees = mSecond * 6;
  length = (width / 2) * 0.92f;
  canvas.save();
  canvas.rotate(mDegrees, width / 2, height / 2);
  canvas.drawLine(width / 2, height / 2, width / 2, height - (height / 2) - length, paint);
  canvas.restore();
 
  return bm;
 }
 
 @Override
 public void onClick(View v) {
  switch (v.getId()) {
   case R.id.btn_start:
    mIsRunning = true;
    new ClockTask().execute("");
    break;
   case R.id.btn_stop:
    mIsRunning = false;
    break;
  }
 }
 
 @Override
 protected void onDestroy() {
  super.onDestroy();
  mIsRunning = false;
 }
 
 private class ClockTask extends AsyncTask<Object, Object, Object> {
 
  @Override
  protected Object doInBackground(Object... objects) {
   while (mIsRunning) {
    publishProgress("");
    try {
     Thread.sleep(1000);
    } catch (InterruptedException e) {
     e.printStackTrace();
    }
   }
   return null;
  }
 
  @Override
  protected void onProgressUpdate(Object... values) {
   super.onProgressUpdate(values);
   mClockImageView.setImageBitmap(drawClock());
  }
 }
 
 
}

点击运行就可以了,这样手机就可以当手表用了,真的神奇。

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持服务器之家。

原文链接:https://blog.csdn.net/cf8833/article/details/101314452

延伸 · 阅读

精彩推荐