自定义View及在配置文件中增加描述属性

自定义View的类必须继承自View并且重写onDraw(Canvas canvas)方法; 增加自定义的属性描述必须在values中增加attrs.xml来描述View的属性;
标签: Android SDK

代码片段(6)[全屏查看所有代码]

1. [图片] 3f7f41d4g9f496933f377&690.jpg    

2. [代码][XML]代码     

?
1
2
3
4
5
6
7
8
9
10
11
12
<? xml version = "1.0" encoding = "UTF-8" ?>
< resources >
     < declare-styleable name = "ClockView" >
         < attr name = "clockColor" format = "color" />
         < attr name = "visible" format = "boolean" />
         < attr name = "timeZone" format = "dimension" >
             < enum name = "londan" value = "0" />
             < enum name = "beijing" value = "8" />
             < enum name = "newyork" value = "20" />
         </ attr >
     </ declare-styleable >
</ resources >

3. [代码][XML]代码     

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
<? xml version = "1.0" encoding = "utf-8" ?>
< LinearLayout xmlns:android = "http://schemas.android.com/apk/res/android"
     xmlns:joydao = "http://schemas.android.com/apk/res/net.joydao.clock"
     android:orientation = "vertical" android:layout_width = "fill_parent"
     android:layout_height = "fill_parent" >
 
  
 
     < net.joydao.clock.view.ClockView
         android:id = "@+id/joydaoClock" android:layout_width = "wrap_content"
         android:layout_height = "wrap_content" joydao:clockColor = "#ffffff"
         joydao:visible = "false" joydao:timeZone = "beijing" />
 
</ LinearLayout >

4. [文件] ClockView.java ~ 10KB     下载(67)     

?
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
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
package net.joydao.clock.view;
 
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.TimeZone;
 
import net.joydao.clock.R;
 
import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.util.AttributeSet;
import android.util.DisplayMetrics;
import android.view.View;
import android.view.WindowManager;
 
public class ClockView extends View implements Runnable {
    
     private Paint colorCirclePaint;
     private Paint pointPaint;
     private Paint hourMarkPaint;
     private Paint minuteMarkPaint;
     private Paint secondNeedlePaint;
     private Paint minuteNeedlePaint;
     private Paint hourNeedlePaint;
     private Paint textPaint;
     private Paint timePaint;
     private float hourMarkLen;
     private float minuteMarkLen;
     private float clockCircle;
     private float radius;
     private float hourNeedleRadius;
     private float minuteNeedleRadius;
     private float secondNeedleRadius;
     private float cx;
     private float cy;
     private boolean running = false ;
     private int mYear;
     private int mMonth;
     private int mDay;
     private int mHour;
     private int mMinute;
     private int mSecond;
     private int clockColor;
    
     public ClockView(Context context){
         this (context, null );
     }
 
     public ClockView(Context context, AttributeSet attrs) {
         super (context, attrs);
         TypedArray typedArray = context.obtainStyledAttributes(attrs,R.styleable.ClockView);
         clockColor = typedArray.getColor(R.styleable.ClockView_clockColor, Color.WHITE);
         typedArray.recycle();
         pointPaint = new Paint();
         pointPaint.setAntiAlias( true );
         pointPaint.setStyle(Paint.Style.STROKE);
        
         colorCirclePaint = new Paint();
         colorCirclePaint.setTextAlign(Paint.Align.CENTER);
         colorCirclePaint.setAntiAlias( true );
         colorCirclePaint.setStrokeWidth(clockCircle);
         colorCirclePaint.setStyle(Paint.Style.STROKE);
        
         hourMarkPaint = new Paint();
         hourMarkPaint.setTextAlign(Paint.Align.CENTER);
         hourMarkPaint.setAntiAlias( true );
         hourMarkPaint.setStyle(Paint.Style.STROKE);
        
         minuteMarkPaint = new Paint();
         minuteMarkPaint.setTextAlign(Paint.Align.CENTER);
         minuteMarkPaint.setAntiAlias( true );
         minuteMarkPaint.setStyle(Paint.Style.STROKE);
        
         secondNeedlePaint = new Paint();
         secondNeedlePaint.setTextAlign(Paint.Align.CENTER);
         secondNeedlePaint.setAntiAlias( true );
         secondNeedlePaint.setStyle(Paint.Style.STROKE);
        
         minuteNeedlePaint = new Paint();
         minuteNeedlePaint.setTextAlign(Paint.Align.CENTER);
         minuteNeedlePaint.setAntiAlias( true );
         minuteNeedlePaint.setStyle(Paint.Style.STROKE);
        
         hourNeedlePaint = new Paint();
         hourNeedlePaint.setTextAlign(Paint.Align.CENTER);
         hourNeedlePaint.setAntiAlias( true );
         hourNeedlePaint.setStyle(Paint.Style.STROKE);
        
         textPaint = new Paint();
         textPaint.setTextAlign(Paint.Align.CENTER);
         textPaint.setAntiAlias( true );
        
         timePaint = new Paint();
         timePaint.setTextAlign(Paint.Align.CENTER);
         timePaint.setAntiAlias( true );
         start();
     }
    
     public int getClockColor() {
         return clockColor;
     }
 
     public void setClockColor( int clockColor) {
         this .clockColor = clockColor;
         this .postInvalidate();
     }
 
     public void start(){
         running = true ;
         new Thread( this ).start();
     }
    
     public void stop(){
         running = false ;
     }
    
     private void resetTime(){
         Calendar c = Calendar.getInstance(TimeZone.getDefault());
         c.setTime( new Date());
         mYear = c.get(Calendar.YEAR);
         mMonth = c.get(Calendar.MONTH)+ 1 ;
         mDay = c.get(Calendar.DAY_OF_MONTH);
         mHour = c.get(Calendar.HOUR_OF_DAY);
         mMinute = c.get(Calendar.MINUTE);
         mSecond = c.get(Calendar.SECOND);
     }
    
     @Override
     protected void onMeasure( int widthMeasureSpec, int heightMeasureSpec) {
         super .onMeasure(widthMeasureSpec, heightMeasureSpec);
     }
 
     @Override
     protected void onDraw(Canvas canvas) {
         super .onDraw(canvas);
         pointPaint.setColor(clockColor);
         colorCirclePaint.setColor(clockColor);
         hourMarkPaint.setColor(clockColor);
         minuteMarkPaint.setColor(clockColor);
         secondNeedlePaint.setColor(clockColor);
         minuteNeedlePaint.setColor(clockColor);
         hourNeedlePaint.setColor(clockColor);
         textPaint.setColor(clockColor);
         timePaint.setColor(clockColor);
         cx = getWidth()/ 2 ;
         cy = getHeight()/ 2 ;
         int tmp = getWidth()<=getHeight()?getWidth():getHeight();
         radius = tmp/ 2 - 2 *clockCircle;
         hourMarkLen = radius/ 15 ;
         minuteMarkLen = radius/ 30 ;
         clockCircle = radius/ 60 ;
         pointPaint.setStrokeWidth(radius/ 25 );
         textPaint.setTextSize(radius/ 8 );
         timePaint.setTextSize(radius/ 5 );
         hourMarkPaint.setStrokeWidth(radius/ 35 );
         minuteMarkPaint.setStrokeWidth(radius/ 70 );
         secondNeedlePaint.setStrokeWidth(radius/ 70 );
         minuteNeedlePaint.setStrokeWidth(radius/ 35 );
         hourNeedlePaint.setStrokeWidth(radius/ 20 );
         hourNeedleRadius = radius - radius/ 2 ;
         minuteNeedleRadius = radius - radius/ 3 ;
         secondNeedleRadius = radius - radius/ 5 ;
         drawClockPanel(canvas,radius);
         drawNeedle(canvas);
         drawTime(canvas);
     }
    
     private void drawTime(Canvas canvas){
         DateFormat dateFormat = new SimpleDateFormat( "yyyy-MM-dd" );
         DateFormat timeFormat = new SimpleDateFormat( "hh:mm:ss a" );
         String dateText = dateFormat.format( new Date());
         String timeText = timeFormat.format( new Date());
         float textX = cx;
         float timeY = cy+radius/ 2 ;
         float dateY = timeY+ 1 .5f*textPaint.getTextSize();
         canvas.drawText(dateText, textX, dateY, textPaint);
         canvas.drawText(timeText, textX, timeY, timePaint);
     }
    
     private void drawClockPanel(Canvas canvas, float radius){  
         //System.out.println(getWidth()+":"+getHeight());
         //画钟的外圈
         canvas.drawCircle(cx, cy, radius, colorCirclePaint);
         //画钟的圆点
         canvas.drawCircle(cx, cy, radius/ 60 , pointPaint);
         int hourLen = 12 ;
         int minLen = 60 ;
         for ( int index = 0 ;index<hourLen;index++){
             drawMark(canvas,index,cx,cy,radius,radius-hourMarkLen,( 2 *Math.PI/hourLen)*index+Math.PI/ 2 ,hourMarkPaint, true );
         }
         for ( int index = 0 ;index<minLen;index++){
             drawMark(canvas,index,cx,cy,radius,radius-minuteMarkLen,( 2 *Math.PI/minLen)*index+Math.PI/ 2 ,minuteMarkPaint, false );
         }
     }
    
     private void drawNeedle(Canvas canvas){
         double hourAngle = ( 2 *Math.PI/ 12 )*mHour+Math.PI/ 2 +(( 2 *Math.PI)/( 12 * 60 ))*mMinute+(( 2 *Math.PI)/( 12 * 60 * 60 ))*mSecond;
         double minuteAngle = ( 2 *Math.PI/( 12 * 5 ))*mMinute+Math.PI/ 2 +(( 2 *Math.PI)/( 12 * 5 * 60 ))*mSecond;
         double secondAngle = ( 2 *Math.PI/ 60 )*mSecond+Math.PI/ 2 ;
         drawNeedle(canvas,cx,cy,hourAngle,hourNeedleRadius,hourNeedlePaint);
         drawNeedle(canvas,cx,cy,minuteAngle,minuteNeedleRadius,minuteNeedlePaint);
         drawNeedle(canvas,cx,cy,secondAngle,secondNeedleRadius,secondNeedlePaint);
     }
    
     private void drawMark(Canvas canvas, int index, float cx, float cy, float r1, float r2, double angle,Paint paint, boolean drawNumber){
         float startX = ( float )(cx-r2*Math.cos(angle));
         float startY = ( float )(cy-r2*Math.sin(angle));
         float stopX = ( float )(cx-r1*Math.cos(angle));
         float stopY = ( float )(cy-r1*Math.sin(angle));
         float textSize = textPaint.getTextSize();
         float radiusText = r2 - textSize;
         if (index>= 3 && index<= 9 ){
             radiusText = r2-textSize/ 3 ;
         }
         float textX = ( float )(cx-radiusText*Math.cos(angle));
         float textY = ( float )(cy-radiusText*Math.sin(angle));
         if (index == 3 || index == 9 ){
             textY = textY + textSize/ 4 ;
         }
         canvas.drawLine(startX,startY,stopX,stopY, paint);
         if (drawNumber){
             if (index== 0 ){
                 index = 12 ;
             }
             canvas.drawText(String.valueOf(index), textX, textY, textPaint);
         }
     }
    
     private void drawNeedle(Canvas canvas, float cx, float cy, double angle, float radius,Paint paint){
         canvas.drawLine(cx, cy, ( float )(cx-radius*Math.cos(angle)), ( float )(cy-radius*Math.sin(angle)), paint);
     }
 
     public static Screen getScreenPix(Context context) {
         DisplayMetrics dm = new DisplayMetrics();
         WindowManager windowManager = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE);
         windowManager.getDefaultDisplay().getMetrics(dm);
         return new Screen(dm.widthPixels,dm.heightPixels);
     }
    
     public static class Screen{
        
         public int widthPixels;
         public int heightPixels;
        
         public Screen(){
            
         }
        
         public Screen( int widthPixels, int heightPixels){
             this .widthPixels=widthPixels;
             this .heightPixels=heightPixels;
         }
 
         @Override
         public String toString() {
             return "(" +widthPixels+ "," +heightPixels+ ")" ;
         }
        
     }
 
     @Override
     public void run() {
         while (running){
             try {
                 resetTime();
                 postInvalidate();
                 Thread.sleep( 1000 );
             } catch (InterruptedException e) {
                 e.printStackTrace();
             }  
         }
     }
 
}

5. [代码]main.xml     

?
1
2
3
4
5
6
7
8
9
10
11
12
<? xml version = "1.0" encoding = "utf-8" ?>
< LinearLayout xmlns:android = "http://schemas.android.com/apk/res/android"
     xmlns:joydao = "http://schemas.android.com/apk/res/net.joydao.clock"
     android:orientation = "vertical" android:layout_width = "fill_parent"
     android:layout_height = "fill_parent" >
 
     < net.joydao.clock.view.ClockView
         android:id = "@+id/joydaoClock" android:layout_width = "wrap_content"
         android:layout_height = "wrap_content" joydao:clockColor = "#ffffff"
         joydao:visible = "false" joydao:timeZone = "beijing" />
 
</ LinearLayout >

6. [代码]attrs.xml     

?
1
2
3
4
5
6
7
8
9
10
11
12
<? xml version = "1.0" encoding = "UTF-8" ?>
< resources >
     < declare-styleable name = "ClockView" >
         < attr name = "clockColor" format = "color" />
         < attr name = "visible" format = "boolean" />
         < attr name = "timeZone" format = "dimension" >
             < enum name = "londan" value = "0" />
             < enum name = "beijing" value = "8" />
             < enum name = "newyork" value = "20" />
         </ attr >
     </ declare-styleable >
</ resources >
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值