0
点赞
收藏
分享

微信扫一扫

android自定义button点击效果



1. package
2.
3. import
4. import
5. import
6. import
7. import
8. import
9. import
10. import
11. import
12. import
13. import
14. import
15.
16. public class AndroidTest extends
17. private static final String TAG = "AndroidTest";
18.
19. private static
20. private
21. private
22. private boolean mbClickFlg = false;
23.
24. @Override
25. public void
26. super.onCreate(savedInstanceState);
27. // setContentView(R.layout.main);
28.
29. // 设置画面布局
30. new LinearLayout(this.getBaseContext());
31. ll.setOrientation(LinearLayout.VERTICAL);
32.
33. // button
34. new Button(this.getBaseContext());
35. "Draw");
36. //
37. 0xff000000);
38. 16);
39. new
40. LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
41.
42. 5;
43. 5;
44. ll.addView(mBtn, lp);
45.
46. // 设置画图view
47. new
48. LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT);
49. 1;
50. new Rect(10, 40, 90, 66);
51. new MyView(this.getBaseContext(), rect);
52. new
53.
54. public void
55. // TODO Auto-generated method stub
56. "mMyView clicked.");
57. if
58. "Click Me");
59. else
60. "Again...");
61. }
62. mbClickFlg = !mbClickFlg;
63. mMyView.postInvalidate();
64. }
65.
66. });
67.
68. ll.addView(mMyView, lp1);
69. setContentView(ll);
70.
71. // 设置监听按钮点击事件
72. new
73.
74. public void
75. // TODO Auto-generated method stub
76. mMyView.drawLine();
77. mMyView.postInvalidate();
78. }
79.
80. });
81.
82. }
83.
84. // 自定义画图类,继承自View类
85. public class MyView extends
86. private
87. private boolean bDrawLineFlg = false;
88. private
89. private String mStrText = "having";
90.
91. MyView(Context context) {
92. super(context);
93. // 生成paint
94. new
95. }
96.
97. MyView(Context context, Rect rect) {
98. super(context);
99. // 生成paint
100. new
101. mRect = rect;
102. }
103.
104. @Override
105. protected void
106. // TODO Auto-generated method stub
107. super.onDraw(canvas);
108.
109. // 填充整个画布
110. canvas.drawColor(Color.GRAY);
111. // canvas.drawRGB(255, 255, 255); // 也可以
112.
113.
114.
115. // 画图
116. if
117. "drawing");
118.
119. // Draw top 6 lines
120. 0xFFFFC0CB);
121. 2, mRect.top, mRect.right - 2, mRect.top, paint);
122. 1, mRect.top + 1, mRect.right -1, mRect.top + 1, paint);
123. for (int i = 2; i < 6; i++) {
124. canvas.drawLine(mRect.left, mRect.top + i, mRect.right, mRect.top + i, paint);
125. }
126.
127. // Draw middle 14 lines
128. 0xFFFFB6C1);
129. for (int i = 6; i < 20; i++) {
130. canvas.drawLine(mRect.left, mRect.top + i, mRect.right, mRect.top + i, paint);
131. }
132.
133. // Draw bottom 6 lines
134. 0xFFFFC0CB);
135. for (int i = 20; i < 24; i++) {
136. canvas.drawLine(mRect.left, mRect.top + i, mRect.right, mRect.top + i, paint);
137. }
138. 1, mRect.top + 24, mRect.right - 1, mRect.top + 24, paint);
139. 2, mRect.top + 25, mRect.right - 2, mRect.top + 25, paint);
140.
141. // draw text
142. 0xFF0000FF);
143. 16);
144. true); // 消除锯齿
145. // 消除锯齿
146. try{
147. float widths[] = new float[mStrText.length()];
148. paint.getTextWidths(mStrText, widths);
149. float textWidth = 0.0f;
150. for(int i = 0; i < widths.length; i++) {
151. textWidth += widths[i];
152. "widths[0] = "
153. }
154. "textWidth = "
155. float
156. "textSize = "
157. canvas.drawText(mStrText,
158. 2,
159. 2 - 3, // add offset to y position
160. paint);
161. catch(Exception e) {
162. e.printStackTrace();
163. }
164. }
165.
166. }
167.
168. public void
169. // 设置画线标志
170. bDrawLineFlg = !bDrawLineFlg;
171. if
172. "Hide");
173. else
174. "Draw");
175. }
176. "to be draw");
177. }
178.
179. public void
180. mStrText = text;
181. }
182. }
183. }

举报

相关推荐

0 条评论