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

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

10年积累的成都网站制作、网站建设、外贸网站建设经验,可以快速应对客户对网站的新想法和需求。提供各种问题对应的解决方案。让选择我们的客户得到更好、更有力的网络服务。我虽然不认识你,你也不认识我。但先网站设计后付款的网站建设流程,更有镇赉免费网站建设让你可以放心的选择与我们合作。

 
 
 
 
  1.  

本例采用另外两种方法来使用自定义对话框,将用这个对话框来最为图形变换(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.  
  2.   xmlns:android=”http://schemas.android.com/apk/res/android”
  3.   android:layout_width=”fill_parent”
  4.   android:layout_height=”fill_parent” 
  5.   >
  6.   android:layout_width=”fill_parent”
  7.   android:layout_height=”fill_parent”>
  8.   android:layout_width=”fill_parent”
  9.   android:layout_height=”fill_parent”
  10.   android:orientation=”vertical”
  11.   >
  12.    android:text=”Primitive”
  13.    android:layout_width=”wrap_content”
  14.    android:layout_height=”wrap_content”>
  15.  
  16.  
  17.  android:layout_width=”wrap_content”
  18.  android:layout_height=”wrap_content”> 
  19. android:text=”Rectangle”
  20. android:id=”@+id/radioRectangle”
  21. android:layout_width=”wrap_content”
  22. android:layout_height=”wrap_content”>
  23. android:text=”Ellipse”
  24. android:id=”@+id/radioEllipse”
  25. android:layout_width=”wrap_content”
  26. android:layout_height=”wrap_content”>
  27. android:text=”Text”
  28. android:id=”@+id/radioText”
  29. android:layout_width=”wrap_content”
  30. android:layout_height=”wrap_content”>
  31.   
  32.    android:text=”Pen”
  33.    android:layout_width=”wrap_content”
  34.    android:layout_height=”wrap_content”>
  35.  
  36.  
  37.  android:layout_width=”wrap_content”
  38.  android:layout_height=”wrap_content”> 
  39. android:text=”Thin”
  40. android:id=”@+id/radioThin”
  41. android:layout_width=”wrap_content”
  42. android:layout_height=”wrap_content”>
  43. android:text=”Thick”
  44. android:id=”@+id/radioThick”
  45. android:layout_width=”wrap_content”
  46. android:layout_height=”wrap_content”>
  47. android:text=”Dashed”
  48. android:id=”@+id/radioDashed”
  49. android:layout_width=”wrap_content”
  50. android:layout_height=”wrap_content”>
  51.  
  52.  
  53.    android:text=”Brush”
  54.    android:layout_width=”wrap_content”
  55.    android:layout_height=”wrap_content”>
  56.  
  57.  
  58.  android:layout_width=”wrap_content”
  59.  android:layout_height=”wrap_content”> 
  60. android:text=”Solid”
  61. android:id=”@+id/radioSolid”
  62. android:layout_width=”wrap_content”
  63. android:layout_height=”wrap_content”>
  64. android:text=”Gradient”
  65. android:id=”@+id/radioGradient”
  66. android:layout_width=”wrap_content”
  67. android:layout_height=”wrap_content”>
  68. android:text=”Texture”
  69. android:id=”@+id/radioTexture”
  70. android:layout_width=”wrap_content”
  71. android:layout_height=”wrap_content”>
  72.  
  73.  
  74.  
  75.    android:text=”Transform”
  76.    android:layout_width=”wrap_content”
  77.    android:layout_height=”wrap_content”>
  78.  
  79.  
  80.  android:layout_width=”wrap_content”
  81.  android:layout_height=”wrap_content”> 
  82. android:text=”Identity”
  83. android:id=”@+id/radioIdentity”
  84. android:layout_width=”wrap_content”
  85. android:layout_height=”wrap_content”>
  86. android:text=”Rotate”
  87. android:id=”@+id/radioRotate”
  88. android:layout_width=”wrap_content”
  89. android:layout_height=”wrap_content”>
  90. android:text=”Scale”
  91. android:id=”@+id/radioScale”
  92. android:layout_width=”wrap_content”
  93. android:layout_height=”wrap_content”>
  94. android:text=”Shear”
  95. android:id=”@+id/radioShear”
  96. android:layout_width=”wrap_content”
  97. android:layout_height=”wrap_content”>
  98.  
  99.  
  100.  
  101.    android:text=”Rendering”
  102.    android:layout_width=”wrap_content”
  103.    android:layout_height=”wrap_content”>
  104.  
  105.  
  106.  android:layout_width=”wrap_content”
  107.  android:layout_height=”wrap_content”> 
  108. android:text=”Stroke”
  109. android:id=”@+id/radioStroke”
  110. android:layout_width=”wrap_content”
  111. android:layout_height=”wrap_content”>
  112. android:text=”Fill”
  113. android:id=”@+id/radioFill”
  114. android:layout_width=”wrap_content”
  115. android:layout_height=”wrap_content”>
  116. android:text=”Stroke and Fill”
  117. android:id=”@+id/radioStrokeFill”
  118. android:layout_width=”wrap_content”
  119. android:layout_height=”wrap_content”>
  120.  
  121.  
  122.  

一种方法是重新定制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 当前名称:Android开发速成简洁教程十八:自定义对话框Transform
    网页URL:http://www.shufengxianlan.com/qtweb/news45/497745.html

    网站建设、网络推广公司-创新互联,是专注品牌与效果的网站制作,网络营销seo公司;服务项目有等

    广告

    声明:本网站发布的内容(图片、视频和文字)以用户投稿、用户转载内容为主,如果涉及侵权请尽快告知,我们将会在第一时间删除。文章观点不代表本网站立场,如需处理请联系客服。电话:028-86922220;邮箱:631063699@qq.com。内容未经允许不得转载,或转载时需注明来源: 创新互联