Android开发速成简洁教程十八:自定义对话框Transform

移动开发 Android
Android自带的AlertDialog,ProgressDialog,DatePickerDialog,TimePickerDialog 可以用于简单的对话框显示。当还是有这些对话框不能满足应用需要的时候,这时就可以使用一些自定义的对话框。有多种方法可以实现自定义对话框。

Android自带的 AlertDialog,ProgressDialog,DatePickerDialog,TimePickerDialog 可以用于简单的对话框显示。当还是有这些对话框不能满足应用需要的时候,这时就可以使用一些自定义的对话框。有多种方法可以实现自定义对话框。一是使用 Activity作为Dialog,可以通过设置Activity显示Dialog风格,使得该Activity在外观上和Dialog一致:显示在其它 Activity前面且半透明。 

  1. <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 作为自定义对话框布局:

  1. <?xml version=”1.0″ encoding=”utf-8″?>  
  2.  
  3. <LinearLayout 
  4.   xmlns:android=”http://schemas.android.com/apk/res/android” 
  5.   android:layout_width=”fill_parent” 
  6.   android:layout_height=”fill_parent”  
  7.  
  8.   > 
  9. <ScrollView 
  10.   android:layout_width=”fill_parent” 
  11.   android:layout_height=”fill_parent”> 
  12. <LinearLayout 
  13.   android:layout_width=”fill_parent” 
  14.   android:layout_height=”fill_parent” 
  15.   android:orientation=”vertical” 
  16.   > 
  17. <TextView 
  18.    android:text=”Primitive” 
  19.    android:layout_width=”wrap_content” 
  20.    android:layout_height=”wrap_content”> 
  21.  </TextView> 
  22.  <RadioGroup 
  23.  android:layout_width=”wrap_content” 
  24.  android:layout_height=”wrap_content”>  
  25.  
  26. <RadioButton 
  27. android:text=”Rectangle” 
  28. android:id=”@+id/radioRectangle” 
  29. android:layout_width=”wrap_content” 
  30. android:layout_height=”wrap_content”> 
  31. </RadioButton> 
  32. <RadioButton 
  33. android:text=”Ellipse” 
  34. android:id=”@+id/radioEllipse” 
  35. android:layout_width=”wrap_content” 
  36. android:layout_height=”wrap_content”> 
  37. </RadioButton> 
  38. <RadioButton 
  39. android:text=”Text” 
  40. android:id=”@+id/radioText” 
  41. android:layout_width=”wrap_content” 
  42. android:layout_height=”wrap_content”> 
  43. </RadioButton> 
  44.  </RadioGroup>  
  45.  
  46. <TextView 
  47.    android:text=”Pen” 
  48.    android:layout_width=”wrap_content” 
  49.    android:layout_height=”wrap_content”> 
  50.  </TextView> 
  51.  <RadioGroup 
  52.  android:layout_width=”wrap_content” 
  53.  android:layout_height=”wrap_content”>  
  54.  
  55. <RadioButton 
  56. android:text=”Thin” 
  57. android:id=”@+id/radioThin” 
  58. android:layout_width=”wrap_content” 
  59. android:layout_height=”wrap_content”> 
  60. </RadioButton> 
  61. <RadioButton 
  62. android:text=”Thick” 
  63. android:id=”@+id/radioThick” 
  64. android:layout_width=”wrap_content” 
  65. android:layout_height=”wrap_content”> 
  66. </RadioButton> 
  67. <RadioButton 
  68. android:text=”Dashed” 
  69. android:id=”@+id/radioDashed” 
  70. android:layout_width=”wrap_content” 
  71. android:layout_height=”wrap_content”> 
  72. </RadioButton> 
  73.  </RadioGroup> 
  74.   
  75. <TextView 
  76.    android:text=”Brush” 
  77.    android:layout_width=”wrap_content” 
  78.    android:layout_height=”wrap_content”> 
  79.  </TextView> 
  80.  <RadioGroup 
  81.  android:layout_width=”wrap_content” 
  82.  android:layout_height=”wrap_content”>  
  83.  
  84. <RadioButton 
  85. android:text=”Solid” 
  86. android:id=”@+id/radioSolid” 
  87. android:layout_width=”wrap_content” 
  88. android:layout_height=”wrap_content”> 
  89. </RadioButton> 
  90. <RadioButton 
  91. android:text=”Gradient” 
  92. android:id=”@+id/radioGradient” 
  93. android:layout_width=”wrap_content” 
  94. android:layout_height=”wrap_content”> 
  95. </RadioButton> 
  96. <RadioButton 
  97. android:text=”Texture” 
  98. android:id=”@+id/radioTexture” 
  99. android:layout_width=”wrap_content” 
  100. android:layout_height=”wrap_content”> 
  101. </RadioButton> 
  102.  </RadioGroup> 
  103.   
  104.  <TextView 
  105.    android:text=”Transform” 
  106.    android:layout_width=”wrap_content” 
  107.    android:layout_height=”wrap_content”> 
  108.  </TextView> 
  109.  <RadioGroup 
  110.  android:layout_width=”wrap_content” 
  111.  android:layout_height=”wrap_content”>  
  112.  
  113. <RadioButton 
  114. android:text=”Identity” 
  115. android:id=”@+id/radioIdentity” 
  116. android:layout_width=”wrap_content” 
  117. android:layout_height=”wrap_content”> 
  118. </RadioButton> 
  119. <RadioButton 
  120. android:text=”Rotate” 
  121. android:id=”@+id/radioRotate” 
  122. android:layout_width=”wrap_content” 
  123. android:layout_height=”wrap_content”> 
  124. </RadioButton> 
  125. <RadioButton 
  126. android:text=”Scale” 
  127. android:id=”@+id/radioScale” 
  128. android:layout_width=”wrap_content” 
  129. android:layout_height=”wrap_content”> 
  130. </RadioButton> 
  131. <RadioButton 
  132. android:text=”Shear” 
  133. android:id=”@+id/radioShear” 
  134. android:layout_width=”wrap_content” 
  135. android:layout_height=”wrap_content”> 
  136. </RadioButton> 
  137.  </RadioGroup> 
  138.   
  139.  <TextView 
  140.    android:text=”Rendering” 
  141.    android:layout_width=”wrap_content” 
  142.    android:layout_height=”wrap_content”> 
  143.  </TextView> 
  144.  <RadioGroup 
  145.  android:layout_width=”wrap_content” 
  146.  android:layout_height=”wrap_content”>  
  147.  
  148. <RadioButton 
  149. android:text=”Stroke” 
  150. android:id=”@+id/radioStroke” 
  151. android:layout_width=”wrap_content” 
  152. android:layout_height=”wrap_content”> 
  153. </RadioButton> 
  154. <RadioButton 
  155. android:text=”Fill” 
  156. android:id=”@+id/radioFill” 
  157. android:layout_width=”wrap_content” 
  158. android:layout_height=”wrap_content”> 
  159. </RadioButton> 
  160. <RadioButton 
  161. android:text=”Stroke and Fill” 
  162. android:id=”@+id/radioStrokeFill” 
  163. android:layout_width=”wrap_content” 
  164. android:layout_height=”wrap_content”> 
  165. </RadioButton> 
  166.  </RadioGroup> 
  167.  </LinearLayout> 
  168. </ScrollView> 
  169. </LinearLayout>  

一种方法是重新定制AlertDialog ,基本步骤和Android简明开发教程十七:Dialog 显示图像类似,但是在protected Dialog onCreateDialog(int id) ,需要重新设定Dialog的Content View并给RadioButton添加事件处理:

  1. 1    protected Dialog onCreateDialog(int id) { 
  2. 2    final Dialog dialog; 
  3. 3    switch (id) { 
  4. 4    case OPTION_DIALOG: 
  5. 5     LayoutInflater li 
  6. 6       = LayoutInflater.from(this); 
  7. 7     View optionView 
  8. 8       = li.inflate(R.layout.transformoption, null); 
  9. 9     AlertDialog.Builder optionDialog 
  10. 10      = new AlertDialog.Builder(this); 
  11. 11    optionDialog.setTitle("Options"); 
  12. 12    optionDialog.setView(optionView); 
  13. 13    dialog = optionDialog.create(); 
  14. 14    RadioButton button = (RadioButton) optionView 
  15. 15      .findViewById(R.id.radioRectangle); 
  16. 16    button.setOnClickListener(new Button.OnClickListener() {  
  17. 17    
  18. 18     public void onClick(View v) { 
  19. 19      primitiveIndex = PRIMITIVE_RECTANGLE; 
  20. 20      drawImage(); 
  21. 21      dialog.dismiss();  
  22. 22    
  23. 23     } 
  24. 24    }); 
  25. 25      
  26. 26    ...  
  27. 27    
  28. 28    button = (RadioButton) optionView 
  29. 29      .findViewById(R.id.radioStrokeFill); 
  30. 30    button.setOnClickListener(new Button.OnClickListener() {  
  31. 31    
  32. 32     public void onClick(View v) { 
  33. 33      renderingIndex = RENDERING_STROKE_AND_FILL; 
  34. 34      drawImage(); 
  35. 35      dialog.dismiss();  
  36. 36    
  37. 37     } 
  38. 38    }); 
  39. 39    return dialog; 
  40. 40   }  
  41. 41    
  42. 42   return null;  
  43. 43    
  44. 44  } 

第二种是通过派生Dialog ,定义了一个OptionDialog类作为Dialog子类。

  1. 1   class OptionDialog extends Dialog {  
  2. 2     
  3. 3    public OptionDialog(Context context) { 
  4. 4     super(context); 
  5. 5     // TODO Auto-generated constructor stub 
  6. 6    } 
  7. 7       
  8. 8    protected void onCreate(Bundle savedInstanceState) { 
  9. 9     super.onCreate(savedInstanceState); 
  10. 10    setContentView(R.layout.transformoption); 
  11. 11    setTitle("Options"); 
  12. 12    RadioButton button 
  13. 13       = (RadioButton) findViewById(R.id.radioRectangle); 
  14. 14    button.setOnClickListener(new Button.OnClickListener() {  
  15. 15    
  16. 16     public void onClick(View v) { 
  17. 17      primitiveIndex = PRIMITIVE_RECTANGLE; 
  18. 18      drawImage(); 
  19. 19      dismiss();  
  20. 20    
  21. 21     } 
  22. 22    }); 
  23. 23    ... 
  24. 24    button = (RadioButton) findViewById(R.id.radioStrokeFill); 
  25. 25    button.setOnClickListener(new Button.OnClickListener() {  
  26. 26    
  27. 27     public void onClick(View v) { 
  28. 28      renderingIndex = RENDERING_STROKE_AND_FILL; 
  29. 29      drawImage(); 
  30. 30      dismiss();  
  31. 31    
  32. 32     } 
  33. 33    }); 
  34. 34   }  
  35. 35    
  36. 36  } 

这两种方法在显示Dialog时有所不同:

  1. 1   private OptionDialog optionDialog; 
  2. 2   static final private int OPTION_DIALOG = 1
  3. 3     
  4. 4   ... 
  5. 5   optionDialog = new OptionDialog(this); 
  6. 6     
  7. 7   ... 
  8. 8   @Override 
  9. 9   public void onClick(View view) { 
  10. 10   // optionDialog.show(); 
  11. 11   showDialog(OPTION_DIALOG); 
  12. 12    
  13. 13  } 

下面是完整代码:

  1. 1   public class Transform extends Graphics2DActivity 
  2. 2     implements OnClickListener {  
  3. 3     
  4. 4    static int PRIMITIVE_RECTANGLE = 0
  5. 5    static int PRIMITIVE_ELLIPSE = 1
  6. 6    static int PRIMITIVE_TEXT = 2
  7. 7    static int PEN_THIN = 0
  8. 8    static int PEN_THICK = 1
  9. 9    static int PEN_DASHED = 2
  10. 10   static int BRUSH_SOLID = 0
  11. 11   static int BRUSH_GRADIENT = 1
  12. 12   static int BRUSH_TEXTURE = 2
  13. 13   static int TRANSFORM_IDENTITY = 0
  14. 14   static int TRANSFORM_ROTATE = 1
  15. 15   static int TRANSFORM_SCALE = 2
  16. 16   static int TRANSFORM_SHEAR = 3
  17. 17   static int RENDERING_STROKE = 0
  18. 18   static int RENDERING_FILL = 1
  19. 19   static int RENDERING_STROKE_AND_FILL = 2
  20. 20   int primitiveIndex; 
  21. 21   int penIndex; 
  22. 22   int brushIndex; 
  23. 23   int transformIndex; 
  24. 24   int renderingIndex; 
  25. 25   int[] rgbData; 
  26. 26   int bitmapWidth; 
  27. 27   int bitmapHeight;  
  28. 28    
  29. 29   class OptionDialog extends Dialog {  
  30. 30    
  31. 31    public OptionDialog(Context context) { 
  32. 32     super(context); 
  33. 33     // TODO Auto-generated constructor stub 
  34. 34    }  
  35. 35    
  36. 36    protected void onCreate(Bundle savedInstanceState) { 
  37. 37     super.onCreate(savedInstanceState); 
  38. 38     setContentView(R.layout.transformoption); 
  39. 39     setTitle("Options"); 
  40. 40     RadioButton button 
  41. 41        = (RadioButton) findViewById(R.id.radioRectangle); 
  42. 42     button.setOnClickListener(new Button.OnClickListener() {  
  43. 43    
  44. 44      public void onClick(View v) { 
  45. 45       primitiveIndex = PRIMITIVE_RECTANGLE; 
  46. 46       drawImage(); 
  47. 47       dismiss();  
  48. 48    
  49. 49      } 
  50. 50     }); 
  51. 51     button = (RadioButton) findViewById(R.id.radioEllipse); 
  52. 52     button.setOnClickListener(new Button.OnClickListener() {  
  53. 53    
  54. 54      public void onClick(View v) { 
  55. 55       primitiveIndex = PRIMITIVE_ELLIPSE; 
  56. 56       drawImage(); 
  57. 57       dismiss();  
  58. 58    
  59. 59      } 
  60. 60     }); 
  61. 61     button = (RadioButton) findViewById(R.id.radioText); 
  62. 62     button.setOnClickListener(new Button.OnClickListener() {  
  63. 63    
  64. 64      public void onClick(View v) { 
  65. 65       primitiveIndex = PRIMITIVE_TEXT; 
  66. 66       drawImage(); 
  67. 67       dismiss();  
  68. 68    
  69. 69      } 
  70. 70     });  
  71. 71    
  72. 72     button = (RadioButton) findViewById(R.id.radioThin); 
  73. 73     button.setOnClickListener(new Button.OnClickListener() {  
  74. 74    
  75. 75      public void onClick(View v) { 
  76. 76       penIndex = PEN_THIN; 
  77. 77       drawImage(); 
  78. 78       dismiss();  
  79. 79    
  80. 80      } 
  81. 81     });  
  82. 82    
  83. 83     button = (RadioButton) findViewById(R.id.radioThick); 
  84. 84     button.setOnClickListener(new Button.OnClickListener() {  
  85. 85    
  86. 86      public void onClick(View v) { 
  87. 87       penIndex = PEN_THICK; 
  88. 88       drawImage(); 
  89. 89       dismiss();  
  90. 90    
  91. 91      } 
  92. 92     });  
  93. 93    
  94. 94     button = (RadioButton) findViewById(R.id.radioDashed); 
  95. 95     button.setOnClickListener(new Button.OnClickListener() {  
  96. 96    
  97. 97      public void onClick(View v) { 
  98. 98       penIndex = PEN_DASHED; 
  99. 99       drawImage(); 
  100. 100      dismiss();  
  101. 101   
  102. 102     } 
  103. 103    });  
  104. 104   
  105. 105    button = (RadioButton) findViewById(R.id.radioSolid); 
  106. 106    button.setOnClickListener(new Button.OnClickListener() {  
  107. 107   
  108. 108     public void onClick(View v) { 
  109. 109      brushIndex = BRUSH_SOLID; 
  110. 110      drawImage(); 
  111. 111      dismiss();  
  112. 112   
  113. 113     } 
  114. 114    });  
  115. 115   
  116. 116    button = (RadioButton) findViewById(R.id.radioGradient); 
  117. 117    button.setOnClickListener(new Button.OnClickListener() {  
  118. 118   
  119. 119     public void onClick(View v) { 
  120. 120      brushIndex = BRUSH_GRADIENT; 
  121. 121      drawImage(); 
  122. 122      dismiss();  
  123. 123   
  124. 124     } 
  125. 125    });  
  126. 126   
  127. 127    button = (RadioButton) findViewById(R.id.radioTexture); 
  128. 128    button.setOnClickListener(new Button.OnClickListener() {  
  129. 129   
  130. 130     public void onClick(View v) { 
  131. 131      brushIndex = BRUSH_TEXTURE; 
  132. 132      drawImage(); 
  133. 133      dismiss();  
  134. 134   
  135. 135     } 
  136. 136    });  
  137. 137   
  138. 138    button = (RadioButton) findViewById(R.id.radioIdentity); 
  139. 139    button.setOnClickListener(new Button.OnClickListener() {  
  140. 140   
  141. 141     public void onClick(View v) { 
  142. 142      transformIndex = TRANSFORM_IDENTITY; 
  143. 143      drawImage(); 
  144. 144      dismiss();  
  145. 145   
  146. 146     } 
  147. 147    });  
  148. 148   
  149. 149    button = (RadioButton) findViewById(R.id.radioRotate); 
  150. 150    button.setOnClickListener(new Button.OnClickListener() {  
  151. 151   
  152. 152     public void onClick(View v) { 
  153. 153      transformIndex = TRANSFORM_ROTATE; 
  154. 154      drawImage(); 
  155. 155      dismiss();  
  156. 156   
  157. 157     } 
  158. 158    });  
  159. 159   
  160. 160    button = (RadioButton) findViewById(R.id.radioScale); 
  161. 161    button.setOnClickListener(new Button.OnClickListener() {  
  162. 162   
  163. 163     public void onClick(View v) { 
  164. 164      transformIndex = TRANSFORM_SCALE; 
  165. 165      drawImage(); 
  166. 166      dismiss();  
  167. 167   
  168. 168     } 
  169. 169    });  
  170. 170   
  171. 171    button = (RadioButton) findViewById(R.id.radioShear); 
  172. 172    button.setOnClickListener(new Button.OnClickListener() {  
  173. 173   
  174. 174     public void onClick(View v) { 
  175. 175      transformIndex = TRANSFORM_SHEAR; 
  176. 176      drawImage(); 
  177. 177      dismiss();  
  178. 178   
  179. 179     } 
  180. 180    });  
  181. 181   
  182. 182    button = (RadioButton) findViewById(R.id.radioStroke); 
  183. 183    button.setOnClickListener(new Button.OnClickListener() {  
  184. 184   
  185. 185     public void onClick(View v) { 
  186. 186      renderingIndex = RENDERING_STROKE; 
  187. 187      drawImage(); 
  188. 188      dismiss();  
  189. 189   
  190. 190     } 
  191. 191    });  
  192. 192   
  193. 193    button = (RadioButton) findViewById(R.id.radioFill); 
  194. 194    button.setOnClickListener(new Button.OnClickListener() {  
  195. 195   
  196. 196     public void onClick(View v) { 
  197. 197      renderingIndex = RENDERING_FILL; 
  198. 198      drawImage(); 
  199. 199      dismiss();  
  200. 200   
  201. 201     } 
  202. 202    });  
  203. 203   
  204. 204    button = (RadioButton) findViewById(R.id.radioStrokeFill); 
  205. 205    button.setOnClickListener(new Button.OnClickListener() {  
  206. 206   
  207. 207     public void onClick(View v) { 
  208. 208      renderingIndex = RENDERING_STROKE_AND_FILL; 
  209. 209      drawImage(); 
  210. 210      dismiss();  
  211. 211   
  212. 212     } 
  213. 213    }); 
  214. 214   }  
  215. 215   
  216. 216  }  
  217. 217   
  218. 218  private OptionDialog optionDialog; 
  219. 219  private Button btnOptions;  
  220. 220   
  221. 221  static final private int OPTION_DIALOG = 1;  
  222. 222   
  223. 223  private AffineTransform at = new AffineTransform(); 
  224. 224  private int w, h; 
  225. 225  private IShape shapes[] = new IShape[3];  
  226. 226   
  227. 227  private boolean firstTime = true
  228. 228  private FontEx font = FontEx.getSystemFont();  
  229. 229   
  230. 230  @Override 
  231. 231  protected void drawImage() { 
  232. 232   drawTransform();  
  233. 233   
  234. 234  }  
  235. 235   
  236. 236  protected Dialog onCreateDialog(int id) { 
  237. 237   final Dialog dialog; 
  238. 238   switch (id) { 
  239. 239   case OPTION_DIALOG: 
  240. 240    LayoutInflater li 
  241. 241      = LayoutInflater.from(this); 
  242. 242    View optionView 
  243. 243      = li.inflate(R.layout.transformoption, null); 
  244. 244    AlertDialog.Builder optionDialog 
  245. 245      = new AlertDialog.Builder(this); 
  246. 246    optionDialog.setTitle("Options"); 
  247. 247    optionDialog.setView(optionView); 
  248. 248    dialog = optionDialog.create(); 
  249. 249    RadioButton button = (RadioButton) optionView 
  250. 250      .findViewById(R.id.radioRectangle); 
  251. 251    button.setOnClickListener(new Button.OnClickListener() {  
  252. 252   
  253. 253     public void onClick(View v) { 
  254. 254      primitiveIndex = PRIMITIVE_RECTANGLE; 
  255. 255      drawImage(); 
  256. 256      dialog.dismiss();  
  257. 257   
  258. 258     } 
  259. 259    }); 
  260. 260    button = (RadioButton) 
  261. 261        optionView.findViewById(R.id.radioEllipse); 
  262. 262    button.setOnClickListener(new Button.OnClickListener() {  
  263. 263   
  264. 264     public void onClick(View v) { 
  265. 265      primitiveIndex = PRIMITIVE_ELLIPSE; 
  266. 266      drawImage(); 
  267. 267      dialog.dismiss();  
  268. 268   
  269. 269     } 
  270. 270    }); 
  271. 271    button = (RadioButton) 
  272. 272      optionView.findViewById(R.id.radioText); 
  273. 273    button.setOnClickListener(new Button.OnClickListener() {  
  274. 274   
  275. 275     public void onClick(View v) { 
  276. 276      primitiveIndex = PRIMITIVE_TEXT; 
  277. 277      drawImage(); 
  278. 278      dialog.dismiss();  
  279. 279   
  280. 280     } 
  281. 281    });  
  282. 282   
  283. 283    button = (RadioButton) 
  284. 284       optionView.findViewById(R.id.radioThin); 
  285. 285    button.setOnClickListener(new Button.OnClickListener() {  
  286. 286   
  287. 287     public void onClick(View v) { 
  288. 288      penIndex = PEN_THIN; 
  289. 289      drawImage(); 
  290. 290      dialog.dismiss();  
  291. 291   
  292. 292     } 
  293. 293    });  
  294. 294   
  295. 295    button = (RadioButton) 
  296. 296       optionView.findViewById(R.id.radioThick); 
  297. 297    button.setOnClickListener(new Button.OnClickListener() {  
  298. 298   
  299. 299     public void onClick(View v) { 
  300. 300      penIndex = PEN_THICK; 
  301. 301      drawImage(); 
  302. 302      dialog.dismiss();  
  303. 303   
  304. 304     } 
  305. 305    });  
  306. 306   
  307. 307    button = (RadioButton) 
  308. 308        optionView.findViewById(R.id.radioDashed); 
  309. 309    button.setOnClickListener(new Button.OnClickListener() {  
  310. 310   
  311. 311     public void onClick(View v) { 
  312. 312      penIndex = PEN_DASHED; 
  313. 313      drawImage(); 
  314. 314      dialog.dismiss();  
  315. 315   
  316. 316     } 
  317. 317    });  
  318. 318   
  319. 319    button = (RadioButton) 
  320. 320        optionView.findViewById(R.id.radioSolid); 
  321. 321    button.setOnClickListener(new Button.OnClickListener() {  
  322. 322   
  323. 323     public void onClick(View v) { 
  324. 324      brushIndex = BRUSH_SOLID; 
  325. 325      drawImage(); 
  326. 326      dialog.dismiss();  
  327. 327   
  328. 328     } 
  329. 329    });  
  330. 330   
  331. 331    button = (RadioButton) 
  332. 332        optionView.findViewById(R.id.radioGradient); 
  333. 333    button.setOnClickListener(new Button.OnClickListener() {  
  334. 334   
  335. 335     public void onClick(View v) { 
  336. 336      brushIndex = BRUSH_GRADIENT; 
  337. 337      drawImage(); 
  338. 338      dialog.dismiss();  
  339. 339   
  340. 340     } 
  341. 341    });  
  342. 342   
  343. 343    button = (RadioButton) 
  344. 344        optionView.findViewById(R.id.radioTexture); 
  345. 345    button.setOnClickListener(new Button.OnClickListener() {  
  346. 346   
  347. 347     public void onClick(View v) { 
  348. 348      brushIndex = BRUSH_TEXTURE; 
  349. 349      drawImage(); 
  350. 350      dialog.dismiss();  
  351. 351   
  352. 352     } 
  353. 353    });  
  354. 354   
  355. 355    button = (RadioButton) 
  356. 356        optionView.findViewById(R.id.radioIdentity); 
  357. 357    button.setOnClickListener(new Button.OnClickListener() {  
  358. 358   
  359. 359     public void onClick(View v) { 
  360. 360      transformIndex = TRANSFORM_IDENTITY; 
  361. 361      drawImage(); 
  362. 362      dialog.dismiss();  
  363. 363   
  364. 364     } 
  365. 365    });  
  366. 366   
  367. 367    button = (RadioButton) 
  368. 368        optionView.findViewById(R.id.radioRotate); 
  369. 369    button.setOnClickListener(new Button.OnClickListener() {  
  370. 370   
  371. 371     public void onClick(View v) { 
  372. 372      transformIndex = TRANSFORM_ROTATE; 
  373. 373      drawImage(); 
  374. 374      dialog.dismiss();  
  375. 375   
  376. 376     } 
  377. 377    });  
  378. 378   
  379. 379    button = (RadioButton) 
  380. 380        optionView.findViewById(R.id.radioScale); 
  381. 381    button.setOnClickListener(new Button.OnClickListener() {  
  382. 382   
  383. 383     public void onClick(View v) { 
  384. 384      transformIndex = TRANSFORM_SCALE; 
  385. 385      drawImage(); 
  386. 386      dialog.dismiss();  
  387. 387   
  388. 388     } 
  389. 389    });  
  390. 390   
  391. 391    button = (RadioButton) 
  392. 392       optionView.findViewById(R.id.radioShear); 
  393. 393    button.setOnClickListener(new Button.OnClickListener() {  
  394. 394   
  395. 395     public void onClick(View v) { 
  396. 396      transformIndex = TRANSFORM_SHEAR; 
  397. 397      drawImage(); 
  398. 398      dialog.dismiss();  
  399. 399   
  400. 400     } 
  401. 401    });  
  402. 402   
  403. 403    button = (RadioButton) 
  404. 404        optionView.findViewById(R.id.radioStroke); 
  405. 405    button.setOnClickListener(new Button.OnClickListener() {  
  406. 406   
  407. 407     public void onClick(View v) { 
  408. 408      renderingIndex = RENDERING_STROKE; 
  409. 409      drawImage(); 
  410. 410      dialog.dismiss();  
  411. 411   
  412. 412     } 
  413. 413    });  
  414. 414   
  415. 415    button = (RadioButton) 
  416. 416        optionView.findViewById(R.id.radioFill); 
  417. 417    button.setOnClickListener(new Button.OnClickListener() {  
  418. 418   
  419. 419     public void onClick(View v) { 
  420. 420      renderingIndex = RENDERING_FILL; 
  421. 421      drawImage(); 
  422. 422      dialog.dismiss();  
  423. 423   
  424. 424     } 
  425. 425    });  
  426. 426   
  427. 427    button = (RadioButton) optionView 
  428. 428      .findViewById(R.id.radioStrokeFill); 
  429. 429    button.setOnClickListener(new Button.OnClickListener() {  
  430. 430   
  431. 431     public void onClick(View v) { 
  432. 432      renderingIndex = RENDERING_STROKE_AND_FILL; 
  433. 433      drawImage(); 
  434. 434      dialog.dismiss();  
  435. 435   
  436. 436     } 
  437. 437    }); 
  438. 438    return dialog; 
  439. 439   }  
  440. 440   
  441. 441   return null;  
  442. 442   
  443. 443  }  
  444. 444   
  445. 445  public void onCreate(Bundle savedInstanceState) { 
  446. 446   super.onCreate(savedInstanceState); 
  447. 447   setContentView(R.layout.transform); 
  448. 448   graphic2dView = (GuidebeeGraphics2DView) 
  449. 449      findViewById(R.id.graphics2dview); 
  450. 450   btnOptions = (Button) findViewById(R.id.btnOption); 
  451. 451   btnOptions.setOnClickListener(this); 
  452. 452   shapes[0] = new Rectangle(00100100); 
  453. 453   shapes[1] = new Ellipse(00100100); 
  454. 454   IShape[] fontShapes = font.getGlyphArray(96
  455. 455          "TEXT".toCharArray(), 0
  456. 456     4, FontEx.TEXT_DIR_LR); 
  457. 457   shapes[2] = new Area(fontShapes[0]); 
  458. 458   for (int i = 1; i < fontShapes.length; i++) { 
  459. 459    ((Area) shapes[2]).add(new Area(fontShapes[i])); 
  460. 460   } 
  461. 461   Bitmap bitmap = BitmapFactory.decodeResource( 
  462. 462           getResources(), 
  463. 463     R.drawable.brick); 
  464. 464   bitmapWidth = bitmap.getWidth(); 
  465. 465   bitmapHeight = bitmap.getHeight();  
  466. 466   
  467. 467   rgbData = new int[bitmapWidth * bitmapHeight]; 
  468. 468   bitmap.getPixels(rgbData, 0, bitmapWidth, 00
  469. 469      bitmapWidth, 
  470. 470     bitmapHeight); 
  471. 471   w = SharedGraphics2DInstance.CANVAS_WIDTH; 
  472. 472   h = SharedGraphics2DInstance.CANVAS_HEIGHT; 
  473. 473   optionDialog = new OptionDialog(this);  
  474. 474   
  475. 475  }  
  476. 476   
  477. 477  private void setTrans(int transIndex) { 
  478. 478   // Sets the AffineTransform. 
  479. 479   switch (transIndex) { 
  480. 480   case 0
  481. 481    at.setToIdentity(); 
  482. 482    at.translate(w / 2, h / 2); 
  483. 483    break
  484. 484   case 1
  485. 485    at.rotate(Math.toRadians(45)); 
  486. 486    break
  487. 487   case 2
  488. 488    at.scale(0.50.5); 
  489. 489    break
  490. 490   case 3
  491. 491    at.shear(0.50.0); 
  492. 492    break
  493. 493   } 
  494. 494  }  
  495. 495   
  496. 496  private void drawTransform() { 
  497. 497   graphics2D.clear(Color.WHITE); 
  498. 498   graphics2D.Reset(); 
  499. 499   setTrans(transformIndex); 
  500. 500   graphics2D.setAffineTransform(at);  
  501. 501   
  502. 502   // Initialize the transform. 
  503. 503   if (firstTime) { 
  504. 504    at.setToIdentity(); 
  505. 505    at.translate(w / 2, h / 2);  
  506. 506   
  507. 507    firstTime = false
  508. 508   }  
  509. 509   
  510. 510   // Sets the Stroke. 
  511. 511   Pen pen = null;  
  512. 512   
  513. 513   switch (penIndex) { 
  514. 514   case 0
  515. 515    pen = new Pen(Color.BLACK, 1); 
  516. 516    break
  517. 517   case 1
  518. 518    pen = new Pen(Color.BLACK, 8); 
  519. 519    break
  520. 520   case 2: { 
  521. 521    int dash[] = { 1010 }; 
  522. 522    pen = new Pen(Color.BLACK, 1, Pen.CAP_BUTT, 
  523. 523      Pen.JOIN_MITER, dash, 0); 
  524. 524   } 
  525. 525    break
  526. 526   }  
  527. 527   
  528. 528   // Sets the Paint. 
  529. 529   Brush brush = null;  
  530. 530   
  531. 531   switch (brushIndex) { 
  532. 532   case 0
  533. 533    brush = new SolidBrush(Color.BLUE); 
  534. 534    break
  535. 535   case 1: { 
  536. 536    int[] fractions = new int[] { 13242 }; 
  537. 537    Color[] colors = new Color[] { new Color(0xffff6600), 
  538. 538      new Color(0xffffff66) }; 
  539. 539    brush = new LinearGradientBrush(5050150
  540. 540      125, fractions, 
  541. 541      colors, Brush.REPEAT); 
  542. 542   }  
  543. 543   
  544. 544    break
  545. 545   case 2
  546. 546    try {  
  547. 547   
  548. 548     brush = new TextureBrush(rgbData, 
  549. 549       bitmapWidth, bitmapHeight); 
  550. 550    } catch (Exception e) { 
  551. 551    } 
  552. 552    break
  553. 553   }  
  554. 554   
  555. 555   // Sets the Shape. 
  556. 556   IShape shape = shapes[primitiveIndex]; 
  557. 557   Rectangle r = shape.getBounds(); 
  558. 558   AffineTransform toCenterAt = new AffineTransform(); 
  559. 559   toCenterAt.concatenate(at); 
  560. 560   toCenterAt.translate(-(r.width / 2), -(r.height / 2));  
  561. 561   
  562. 562   graphics2D.setAffineTransform(toCenterAt); 
  563. 563   // Sets the rendering method. 
  564. 564   switch (renderingIndex) { 
  565. 565   case 0
  566. 566    graphics2D.setDefaultPen(pen); 
  567. 567    graphics2D.draw(null, shape); 
  568. 568    break
  569. 569   case 1
  570. 570    graphics2D.setDefaultBrush(brush); 
  571. 571    graphics2D.fill(null, shape); 
  572. 572    break
  573. 573   case 2
  574. 574    graphics2D.setPenAndBrush(pen, brush); 
  575. 575    graphics2D.fill(null, shape); 
  576. 576    graphics2D.draw(null, shape); 
  577. 577    break
  578. 578   } 
  579. 579   graphic2dView.refreshCanvas();  
  580. 580   
  581. 581  }  
  582. 582   
  583. 583  @Override 
  584. 584  public void onClick(View view) { 
  585. 585   // optionDialog.show(); 
  586. 586   showDialog(OPTION_DIALOG);  
  587. 587   
  588. 588  } 
  589. 589 } 

上面代码中包含了两种方法的代码,实际应用中可以任选其一。

责任编辑:闫佳明 来源: imobilebbs
相关推荐

2013-12-26 17:08:36

Android开发Android应用自定义Adapter显

2013-12-26 15:10:08

Android开发应用和框架Linux 内核

2013-12-26 15:43:07

Android开发Android应用Activities

2013-12-26 15:18:09

Android开发安装开发环境

2013-12-27 14:05:22

Android开发Android应用Dialog

2013-12-27 14:16:43

Android开发Android应用线程

2013-12-27 14:34:46

Android开发Android应用短信触发示例

2013-12-26 15:34:19

Android开发Android应用基本概念

2013-12-27 16:06:10

Android开发Android应用发布应用

2013-12-27 13:27:05

Android开发Android应用RadioButton

2013-12-26 15:46:30

Android开发Android应用用户界面设计

2013-12-26 16:24:13

Android开发Android应用Intents

2013-12-27 12:51:44

Android开发Android应用引路蜂

2013-12-27 13:49:22

Android开发Android应用Button

2013-12-26 16:59:12

Android开发Android应用数据绑定Data Bi

2013-12-27 15:31:26

Android开发Android应用资源Resources

2013-12-26 16:46:21

2011-07-01 11:33:00

Qt 模态 非模态

2010-08-05 10:42:41

Android开发Android高级编程

2010-01-28 16:55:26

Android对话框
点赞
收藏

51CTO技术栈公众号