Android简明开发教程十八:自定义对话框 Transform
Android自带的AlertDialog,ProgressDialog,DatePickerDialog,TimePickerDialog 可以用于简单的对话框显示。当还是有这些对话框不能满足应用需要的时候,这时就可以使用一些自定义的对话框。有多种方法可以实现自定义对话框。一是使用Activity作为Dialog,可以通过设置Activity显示Dialog风格,使得该Activity在外观上和Dialog一致:显示在其它Activity前面且半透明。
<Activity android:name=”MyDialogActivity” Android:theme=”@android:style/Theme.Dialog“></Activity>
本例采用另外两种方法来使用自定义对话框,将用这个对话框来最为图形变换(Transform)的选项:
Primitive: Rectange, Ellipse,Text
Pen: Thin,Thick,Dashed
Brush: Solid, Gradient,Texture
Transform: Identity, Rotate, Scale, Shear
Rendering: Stroke, Fill, Stoke and Fill
首先在res/layout 下新建transformoption.xml 作为自定义对话框布局:
<?xml version=”1.0″ encoding=”utf-8″?>
<LinearLayout
xmlns:android=”http://schemas.android.com/apk/res/android”
android:layout_width=”fill_parent”
android:layout_height=”fill_parent”
>
<ScrollView
android:layout_width=”fill_parent”
android:layout_height=”fill_parent”>
<LinearLayout
android:layout_width=”fill_parent”
android:layout_height=”fill_parent”
android:orientation=”vertical”
>
<TextView
android:text=”Primitive”
android:layout_width=”wrap_content”
android:layout_height=”wrap_content”>
</TextView>
<RadioGroup
android:layout_width=”wrap_content”
android:layout_height=”wrap_content”>
<RadioButton
android:text=”Rectangle”
android:id=”@+id/radioRectangle”
android:layout_width=”wrap_content”
android:layout_height=”wrap_content”>
</RadioButton>
<RadioButton
android:text=”Ellipse”
android:id=”@+id/radioEllipse”
android:layout_width=”wrap_content”
android:layout_height=”wrap_content”>
</RadioButton>
<RadioButton
android:text=”Text”
android:id=”@+id/radioText”
android:layout_width=”wrap_content”
android:layout_height=”wrap_content”>
</RadioButton>
</RadioGroup>
<TextView
android:text=”Pen”
android:layout_width=”wrap_content”
android:layout_height=”wrap_content”>
</TextView>
<RadioGroup
android:layout_width=”wrap_content”
android:layout_height=”wrap_content”>
<RadioButton
android:text=”Thin”
android:id=”@+id/radioThin”
android:layout_width=”wrap_content”
android:layout_height=”wrap_content”>
</RadioButton>
<RadioButton
android:text=”Thick”
android:id=”@+id/radioThick”
android:layout_width=”wrap_content”
android:layout_height=”wrap_content”>
</RadioButton>
<RadioButton
android:text=”Dashed”
android:id=”@+id/radioDashed”
android:layout_width=”wrap_content”
android:layout_height=”wrap_content”>
</RadioButton>
</RadioGroup>
<TextView
android:text=”Brush”
android:layout_width=”wrap_content”
android:layout_height=”wrap_content”>
</TextView>
<RadioGroup
android:layout_width=”wrap_content”
android:layout_height=”wrap_content”>
<RadioButton
android:text=”Solid”
android:id=”@+id/radioSolid”
android:layout_width=”wrap_content”
android:layout_height=”wrap_content”>
</RadioButton>
<RadioButton
android:text=”Gradient”
android:id=”@+id/radioGradient”
android:layout_width=”wrap_content”
android:layout_height=”wrap_content”>
</RadioButton>
<RadioButton
android:text=”Texture”
android:id=”@+id/radioTexture”
android:layout_width=”wrap_content”
android:layout_height=”wrap_content”>
</RadioButton>
</RadioGroup>
<TextView
android:text=”Transform”
android:layout_width=”wrap_content”
android:layout_height=”wrap_content”>
</TextView>
<RadioGroup
android:layout_width=”wrap_content”
android:layout_height=”wrap_content”>
<RadioButton
android:text=”Identity”
android:id=”@+id/radioIdentity”
android:layout_width=”wrap_content”
android:layout_height=”wrap_content”>
</RadioButton>
<RadioButton
android:text=”Rotate”
android:id=”@+id/radioRotate”
android:layout_width=”wrap_content”
android:layout_height=”wrap_content”>
</RadioButton>
<RadioButton
android:text=”Scale”
android:id=”@+id/radioScale”
android:layout_width=”wrap_content”
android:layout_height=”wrap_content”>
</RadioButton>
<RadioButton
android:text=”Shear”
android:id=”@+id/radioShear”
android:layout_width=”wrap_content”
android:layout_height=”wrap_content”>
</RadioButton>
</RadioGroup>
<TextView
android:text=”Rendering”
android:layout_width=”wrap_content”
android:layout_height=”wrap_content”>
</TextView>
<RadioGroup
android:layout_width=”wrap_content”
android:layout_height=”wrap_content”>
<RadioButton
android:text=”Stroke”
android:id=”@+id/radioStroke”
android:layout_width=”wrap_content”
android:layout_height=”wrap_content”>
</RadioButton>
<RadioButton
android:text=”Fill”
android:id=”@+id/radioFill”
android:layout_width=”wrap_content”
android:layout_height=”wrap_content”>
</RadioButton>
<RadioButton
android:text=”Stroke and Fill”
android:id=”@+id/radioStrokeFill”
android:layout_width=”wrap_content”
android:layout_height=”wrap_content”>
</RadioButton>
</RadioGroup>
</LinearLayout>
</ScrollView>
</LinearLayout>
一种方法是重新定制AlertDialog ,基本步骤和Android简明开发教程十七:Dialog 显示图像类似,但是在protected Dialog onCreateDialog(int id) ,需要重新设定Dialog的Content View并给RadioButton添加事件处理:
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
|
protected Dialog onCreateDialog( int id) { final Dialog dialog; switch (id) { case OPTION_DIALOG: LayoutInflater li = LayoutInflater.from( this ); View optionView = li.inflate(R.layout.transformoption, null ); AlertDialog.Builder optionDialog = new AlertDialog.Builder( this ); optionDialog.setTitle( "Options" ); optionDialog.setView(optionView); dialog = optionDialog.create(); RadioButton button = (RadioButton) optionView .findViewById(R.id.radioRectangle); button.setOnClickListener( new Button.OnClickListener() { public void onClick(View v) { primitiveIndex = PRIMITIVE_RECTANGLE; drawImage(); dialog.dismiss(); } }); ... button = (RadioButton) optionView .findViewById(R.id.radioStrokeFill); button.setOnClickListener( new Button.OnClickListener() { public void onClick(View v) { renderingIndex = RENDERING_STROKE_AND_FILL; drawImage(); dialog.dismiss(); } }); return dialog; } return null ; } |
第二种是通过派生Dialog ,定义了一个OptionDialog类作为Dialog子类。
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
|
class OptionDialog extends Dialog { public OptionDialog(Context context) { super (context); // TODO Auto-generated constructor stub } protected void onCreate(Bundle savedInstanceState) { super .onCreate(savedInstanceState); setContentView(R.layout.transformoption); setTitle( "Options" ); RadioButton button = (RadioButton) findViewById(R.id.radioRectangle); button.setOnClickListener( new Button.OnClickListener() { public void onClick(View v) { primitiveIndex = PRIMITIVE_RECTANGLE; drawImage(); dismiss(); } }); ... button = (RadioButton) findViewById(R.id.radioStrokeFill); button.setOnClickListener( new Button.OnClickListener() { public void onClick(View v) { renderingIndex = RENDERING_STROKE_AND_FILL; drawImage(); dismiss(); } }); } } |
这两种方法在显示Dialog时有所不同:
1
2
3
4
5
6
7
8
9
10
11
12
13
|
private OptionDialog optionDialog; static final private int OPTION_DIALOG = 1 ; ... optionDialog = new OptionDialog( this ); ... @Override public void onClick(View view) { // optionDialog.show(); showDialog(OPTION_DIALOG); } |
下面是完整代码:
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
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
|
public class Transform extends Graphics2DActivity implements OnClickListener { static int PRIMITIVE_RECTANGLE = 0 ; static int PRIMITIVE_ELLIPSE = 1 ; static int PRIMITIVE_TEXT = 2 ; static int PEN_THIN = 0 ; static int PEN_THICK = 1 ; static int PEN_DASHED = 2 ; static int BRUSH_SOLID = 0 ; static int BRUSH_GRADIENT = 1 ; static int BRUSH_TEXTURE = 2 ; static int TRANSFORM_IDENTITY = 0 ; static int TRANSFORM_ROTATE = 1 ; static int TRANSFORM_SCALE = 2 ; static int TRANSFORM_SHEAR = 3 ; static int RENDERING_STROKE = 0 ; static int RENDERING_FILL = 1 ; static int RENDERING_STROKE_AND_FILL = 2 ; int primitiveIndex; int penIndex; int brushIndex; int transformIndex; int renderingIndex; int [] rgbData; int bitmapWidth; int bitmapHeight; class OptionDialog extends Dialog { public OptionDialog(Context context) { super (context); // TODO Auto-generated constructor stub } protected void onCreate(Bundle savedInstanceState) { super .onCreate(savedInstanceState); setContentView(R.layout.transformoption); setTitle( "Options" ); RadioButton button = (RadioButton) findViewById(R.id.radioRectangle); button.setOnClickListener( new Button.OnClickListener() { public void onClick(View v) { primitiveIndex = PRIMITIVE_RECTANGLE; drawImage(); dismiss(); } }); button = (RadioButton) findViewById(R.id.radioEllipse); button.setOnClickListener( new Button.OnClickListener() { public void onClick(View v) { primitiveIndex = PRIMITIVE_ELLIPSE; drawImage(); dismiss(); } }); button = (RadioButton) findViewById(R.id.radioText); button.setOnClickListener( new Button.OnClickListener() { public void onClick(View v) { primitiveIndex = PRIMITIVE_TEXT; drawImage(); dismiss(); } }); button = (RadioButton) findViewById(R.id.radioThin); button.setOnClickListener( new Button.OnClickListener() { public void onClick(View v) { penIndex = PEN_THIN; drawImage(); dismiss(); } }); button = (RadioButton) findViewById(R.id.radioThick); button.setOnClickListener( new Button.OnClickListener() { public void onClick(View v) { penIndex = PEN_THICK; drawImage(); dismiss(); } }); button = (RadioButton) findViewById(R.id.radioDashed); button.setOnClickListener( new Button.OnClickListener() { public void onClick(View v) { penIndex = PEN_DASHED; drawImage(); dismiss(); } }); button = (RadioButton) findViewById(R.id.radioSolid); button.setOnClickListener( new Button.OnClickListener() { public void onClick(View v) { brushIndex = BRUSH_SOLID; drawImage(); dismiss(); } }); button = (RadioButton) findViewById(R.id.radioGradient); button.setOnClickListener( new Button.OnClickListener() { public void onClick(View v) { brushIndex = BRUSH_GRADIENT; drawImage(); dismiss(); } }); button = (RadioButton) findViewById(R.id.radioTexture); button.setOnClickListener( new Button.OnClickListener() { public void onClick(View v) { brushIndex = BRUSH_TEXTURE; drawImage(); dismiss(); } }); button = (RadioButton) findViewById(R.id.radioIdentity); button.setOnClickListener( new Button.OnClickListener() { public void onClick(View v) { transformIndex = TRANSFORM_IDENTITY; drawImage(); dismiss(); } }); button = (RadioButton) findViewById(R.id.radioRotate); button.setOnClickListener( new Button.OnClickListener() { public void onClick(View v) { transformIndex = TRANSFORM_ROTATE; drawImage(); dismiss(); } }); button = (RadioButton) findViewById(R.id.radioScale); button.setOnClickListener( new Button.OnClickListener() { public void onClick(View v) { transformIndex = TRANSFORM_SCALE; drawImage(); dismiss(); } }); button = (RadioButton) findViewById(R.id.radioShear); button.setOnClickListener( new Button.OnClickListener() { public void onClick(View v) { transformIndex = TRANSFORM_SHEAR; drawImage(); dismiss(); } }); button = (RadioButton) findViewById(R.id.radioStroke); button.setOnClickListener( new Button.OnClickListener() { public void onClick(View v) { renderingIndex = RENDERING_STROKE; drawImage(); dismiss(); } }); button = (RadioButton) findViewById(R.id.radioFill); button.setOnClickListener( new Button.OnClickListener() { public void onClick(View v) { renderingIndex = RENDERING_FILL; drawImage(); dismiss(); } }); button = (RadioButton) findViewById(R.id.radioStrokeFill); button.setOnClickListener( new Button.OnClickListener() { public void onClick(View v) { renderingIndex = RENDERING_STROKE_AND_FILL; drawImage(); dismiss(); } }); } } private OptionDialog optionDialog; private Button btnOptions; static final private int OPTION_DIALOG = 1 ; private AffineTransform at = new AffineTransform(); private int w, h; private IShape shapes[] = new IShape[ 3 ]; private boolean firstTime = true ; private FontEx font = FontEx.getSystemFont(); @Override protected void drawImage() { drawTransform(); } protected Dialog onCreateDialog( int id) { final Dialog dialog; switch (id) { case OPTION_DIALOG: LayoutInflater li = LayoutInflater.from( this ); View optionView = li.inflate(R.layout.transformoption, null ); AlertDialog.Builder optionDialog = new AlertDialog.Builder( this ); optionDialog.setTitle( "Options" ); optionDialog.setView(optionView); dialog = optionDialog.create(); RadioButton button = (RadioButton) optionView .findViewById(R.id.radioRectangle); button.setOnClickListener( new Button.OnClickListener() { public void onClick(View v) { primitiveIndex = PRIMITIVE_RECTANGLE; drawImage(); dialog.dismiss(); } }); button = (RadioButton) optionView.findViewById(R.id.radioEllipse); button.setOnClickListener( new Button.OnClickListener() { public void onClick(View v) { primitiveIndex = PRIMITIVE_ELLIPSE; drawImage(); dialog.dismiss(); } }); button = (RadioButton) optionView.findViewById(R.id.radioText); button.setOnClickListener( new Button.OnClickListener() { public void onClick(View v) { primitiveIndex = PRIMITIVE_TEXT; drawImage(); dialog.dismiss(); } }); button = (RadioButton) optionView.findViewById(R.id.radioThin); button.setOnClickListener( new Button.OnClickListener() { public void onClick(View v) { penIndex = PEN_THIN; drawImage(); dialog.dismiss(); } }); button = (RadioButton) optionView.findViewById(R.id.radioThick); button.setOnClickListener( new Button.OnClickListener() { public void onClick(View v) { penIndex = PEN_THICK; drawImage(); dialog.dismiss(); } }); button = (RadioButton) optionView.findViewById(R.id.radioDashed); button.setOnClickListener( new Button.OnClickListener() { public void onClick(View v) { penIndex = PEN_DASHED; drawImage(); dialog.dismiss(); } }); button = (RadioButton) optionView.findViewById(R.id.radioSolid); button.setOnClickListener( new Button.OnClickListener() { public void onClick(View v) { brushIndex = BRUSH_SOLID; drawImage(); dialog.dismiss(); } }); button = (RadioButton) optionView.findViewById(R.id.radioGradient); button.setOnClickListener( new Button.OnClickListener() { public void onClick(View v) { brushIndex = BRUSH_GRADIENT; drawImage(); dialog.dismiss(); } }); button = (RadioButton) optionView.findViewById(R.id.radioTexture); button.setOnClickListener( new Button.OnClickListener() { public void onClick(View v) { brushIndex = BRUSH_TEXTURE; drawImage(); dialog.dismiss(); } }); button = (RadioButton) optionView.findViewById(R.id.radioIdentity); button.setOnClickListener( new Button.OnClickListener() { public void onClick(View v) { transformIndex = TRANSFORM_IDENTITY; drawImage(); dialog.dismiss(); } }); button = (RadioButton) optionView.findViewById(R.id.radioRotate); button.setOnClickListener( new Button.OnClickListener() { public void onClick(View v) { transformIndex = TRANSFORM_ROTATE; drawImage(); dialog.dismiss(); } }); button = (RadioButton) optionView.findViewById(R.id.radioScale); button.setOnClickListener( new Button.OnClickListener() { public void onClick(View v) { transformIndex = TRANSFORM_SCALE; drawImage(); dialog.dismiss(); } }); button = (RadioButton) optionView.findViewById(R.id.radioShear); button.setOnClickListener( new Button.OnClickListener() { public void onClick(View v) { transformIndex = TRANSFORM_SHEAR; drawImage(); dialog.dismiss(); } }); button = (RadioButton) optionView.findViewById(R.id.radioStroke); button.setOnClickListener( new Button.OnClickListener() { public void onClick(View v) { renderingIndex = RENDERING_STROKE; drawImage(); dialog.dismiss(); } }); button = (RadioButton) optionView.findViewById(R.id.radioFill); button.setOnClickListener( new Button.OnClickListener() { public void onClick(View v) { renderingIndex = RENDERING_FILL; drawImage(); dialog.dismiss(); } }); button = (RadioButton) optionView .findViewById(R.id.radioStrokeFill); button.setOnClickListener( new Button.OnClickListener() { public void onClick(View v) { renderingIndex = RENDERING_STROKE_AND_FILL; drawImage(); dialog.dismiss(); } }); return dialog; } return null ; } public void onCreate(Bundle savedInstanceState) { super .onCreate(savedInstanceState); setContentView(R.layout.transform); graphic2dView = (GuidebeeGraphics2DView) findViewById(R.id.graphics2dview); btnOptions = (Button) findViewById(R.id.btnOption); btnOptions.setOnClickListener( this ); shapes[ 0 ] = new Rectangle( 0 , 0 , 100 , 100 ); shapes[ 1 ] = new Ellipse( 0 , 0 , 100 , 100 ); IShape[] fontShapes = font.getGlyphArray( 96 , "TEXT" .toCharArray(), 0 , 4 , FontEx.TEXT_DIR_LR); shapes[ 2 ] = new Area(fontShapes[ 0 ]); for ( int i = 1 ; i < fontShapes.length; i++) { ((Area) shapes[ 2 ]).add( new Area(fontShapes[i])); } Bitmap bitmap = BitmapFactory.decodeResource( getResources(), R.drawable.brick); bitmapWidth = bitmap.getWidth(); bitmapHeight = bitmap.getHeight(); rgbData = new int [bitmapWidth * bitmapHeight]; bitmap.getPixels(rgbData, 0 , bitmapWidth, 0 , 0 , bitmapWidth, bitmapHeight); w = SharedGraphics2DInstance.CANVAS_WIDTH; h = SharedGraphics2DInstance.CANVAS_HEIGHT; optionDialog = new OptionDialog( this ); } private void setTrans( int transIndex) { // Sets the AffineTransform. switch (transIndex) { case 0 : at.setToIdentity(); at.translate(w / 2 , h / 2 ); break ; case 1 : at.rotate(Math.toRadians( 45 )); break ; case 2 : at.scale( 0.5 , 0.5 ); break ; case 3 : at.shear( 0.5 , 0.0 ); break ; } } private void drawTransform() { graphics2D.clear(Color.WHITE); graphics2D.Reset(); setTrans(transformIndex); graphics2D.setAffineTransform(at); // Initialize the transform. if (firstTime) { at.setToIdentity(); at.translate(w / 2 , h / 2 ); firstTime = false ; } // Sets the Stroke. Pen pen = null ; switch (penIndex) { case 0 : pen = new Pen(Color.BLACK, 1 ); break ; case 1 : pen = new Pen(Color.BLACK, 8 ); break ; case 2 : { int dash[] = { 10 , 10 }; pen = new Pen(Color.BLACK, 1 , Pen.CAP_BUTT, Pen.JOIN_MITER, dash, 0 ); } break ; } // Sets the Paint. Brush brush = null ; switch (brushIndex) { case 0 : brush = new SolidBrush(Color.BLUE); break ; case 1 : { int [] fractions = new int [] { 13 , 242 }; Color[] colors = new Color[] { new Color( 0xffff6600 ), new Color( 0xffffff66 ) }; brush = new LinearGradientBrush( 50 , 50 , 150 , 125 , fractions, colors, Brush.REPEAT); } break ; case 2 : try { brush = new TextureBrush(rgbData, bitmapWidth, bitmapHeight); } catch (Exception e) { } break ; } // Sets the Shape. IShape shape = shapes[primitiveIndex]; Rectangle r = shape.getBounds(); AffineTransform toCenterAt = new AffineTransform(); toCenterAt.concatenate(at); toCenterAt.translate(-(r.width / 2 ), -(r.height / 2 )); graphics2D.setAffineTransform(toCenterAt); // Sets the rendering method. switch (renderingIndex) { case 0 : graphics2D.setDefaultPen(pen); graphics2D.draw( null , shape); break ; case 1 : graphics2D.setDefaultBrush(brush); graphics2D.fill( null , shape); break ; case 2 : graphics2D.setPenAndBrush(pen, brush); graphics2D.fill( null , shape); graphics2D.draw( null , shape); break ; } graphic2dView.refreshCanvas(); } @Override public void onClick(View view) { // optionDialog.show(); showDialog(OPTION_DIALOG); } } |
上面代码中包含了两种方法的代码,实际应用中可以任选其一。
posted on 2011-05-13 13:43 Hibernate4 阅读(195) 评论(0) 编辑 收藏 举报