Android开发速成简洁教程十六:Button 画刷示例

移动开发 Android
介绍了RadioButton和Button 后,这时应该对使用Android提供的控件的用法有了基本的认识。在创建自定义控件时,也可以重载onKeyDown(int, KeyEvent),onKeyUp(int, KeyEvent) ,onTouchEvent(MotionEvent)等来处理用户事件。

将RadioButton 换成Button ,类似的在res\layout 中新建brush.xml:

<?xml version=”1.0″ encoding=”utf-8″?> 
<LinearLayout xmlns:android=”http://schemas.android.com/apk/res/android” 
    android:orientation=”vertical” 
    android:background=”@drawable/white” 
 android:layout_width=”fill_parent” 
 android:layout_height=”fill_parent”> 
    <com.pstreets.graphics2d.GuidebeeGraphics2DView 
     android:id=”@+id/graphics2dview” 
     android:layout_weight=”1″ 
     android:layout_width=”fill_parent” 
     android:layout_height=”wrap_content”/> 
 <LinearLayout xmlns:android=”http://schemas.android.com/apk/res/android” 
  android:layout_width=”wrap_content” android:layout_height=”wrap_content” 
  android:orientation=”horizontal” 
   
  > 
   
   <Button android:text=”Pattern” 
       android:id=”@+id/btnPattern” 
    android:layout_width=”wrap_content” 
    android:textColor=”@color/black” 
    android:checked=”true” 
    android:layout_height=”wrap_content”> 
   </Button> 
   <Button android:text=”Gradients” 
        android:id=”@+id/btnGradients” 
    android:layout_width=”wrap_content” 
    android:textColor=”@color/black” 
    android:layout_height=”wrap_content”> 
   </Button> 
  
 </LinearLayout>  
 
</LinearLayout>  
  • 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.

修改Brushes.java ,完整代码如下:

1   public class Brushes extends Graphics2DActivity 
2      implements OnClickListener {  
3     
4    private Button btnPattern; 
5    private Button btnGradients;  
6     
7    public void onCreate(Bundle savedInstanceState) { 
8     super.onCreate(savedInstanceState); 
9     setContentView(R.layout.brush); 
10    graphic2dView = (GuidebeeGraphics2DView) 
11        findViewById(R.id.graphics2dview); 
12    btnPattern = (Button) findViewById(R.id.btnPattern); 
13    btnGradients = (Button) findViewById(R.id.btnGradients); 
14    btnPattern.setOnClickListener(this); 
15    btnGradients.setOnClickListener(this); 
16   }  
17    
18   @Override 
19   protected void drawImage() { 
20    drawPatterns();  
21    
22   }  
23    
24   @Override 
25   public void onClick(View view) { 
26    if (view == btnPattern) { 
27     drawPatterns(); 
28    } else { 
29     drawGradient(); 
30    } 
31    graphic2dView.refreshCanvas();  
32    
33   }  
34    
35   private void drawPatterns() { 
36    TextureBrush brush1; 
37    TextureBrush brush2; 
38    TextureBrush brush3;  
39    
40    AffineTransform matrix1 = new AffineTransform(); 
41    AffineTransform matrix2 = new AffineTransform(); 
42    Bitmap bitmap 
43      = BitmapFactory.decodeResource(getResources(), 
44      R.drawable.brick); 
45    int[] rgbData = new int[bitmap.getHeight() 
46                            * bitmap.getWidth()]; 
47    bitmap.getPixels(rgbData, 0, bitmap.getWidth(), 00
48      bitmap.getWidth(), bitmap.getHeight()); 
49    brush1 = new TextureBrush(rgbData, bitmap.getWidth(), 
50      bitmap.getHeight());  
51    
52    bitmap = BitmapFactory.decodeResource(getResources(), 
53      R.drawable.bird); 
54    rgbData = new int[bitmap.getHeight() * bitmap.getWidth()]; 
55    bitmap.getPixels(rgbData, 0, bitmap.getWidth(), 00
56      bitmap.getWidth(), bitmap.getHeight()); 
57    brush2 = new TextureBrush(rgbData, bitmap.getWidth(), 
58      bitmap.getHeight()); 
59    brush3 = new TextureBrush(rgbData, bitmap.getWidth(), 
60      bitmap.getHeight(), 127); 
61    matrix2.translate(5050); 
62    // Clear the canvas with white color. 
63    graphics2D.clear(Color.WHITE); 
64    graphics2D.setAffineTransform(matrix1); 
65    graphics2D.fillRectangle(brush1, 
66       new Rectangle(2050100100)); 
67    graphics2D.fillOval(brush2, 10108080); 
68    graphics2D.setAffineTransform(matrix2); 
69    graphics2D.fillOval(brush3, 10108080);  
70    
71   }  
72    
73   private void drawGradient() { 
74    /* The linear gradient color */ 
75    LinearGradientBrush brush1; 
76    /* The radial gradient color */ 
77    RadialGradientBrush brush2; 
78    /* The second radial gradient color */ 
79    RadialGradientBrush brush3;  
80    
81    char[] engText = "Brush".toCharArray();  
82    
83    FontEx font = FontEx.getSystemFont();  
84    
85    int fontSize = 44
86    int X = 15
87    int Y = 50
88    int[] fractions = new int[] { 13242 }; 
89    Color[] colors = new Color[] { new Color(0xffff6600), 
90      new Color(0xffffff66) }; 
91    brush1 = new LinearGradientBrush(5050150125
92      fractions, colors, 
93      Brush.NO_CYCLE);  
94    
95    fractions = new int[] { 13128255 }; 
96    colors = new Color[] { new Color(0xffff6600), 
97      new Color(0xffffff66), 
98      new Color(0xffff6600) }; 
99    brush2 = new RadialGradientBrush(9010050
100     fractions, colors);  
101   
102   fractions = new int[] { 0255 }; 
103   colors = new Color[] { new Color(0xFFFFFF00), 
104     new Color(0xFF000000) }; 
105   brush3 = new RadialGradientBrush(5050100
106     fractions, colors); 
107   // Clear the canvas with white color. 
108   graphics2D.clear(Color.white); 
109   graphics2D.fillRectangle(brush1, 
110     new Rectangle(107512080));  
111   
112   Pen pen = new Pen(brush2, 8); 
113   graphics2D.drawOval(pen, 206010050); 
114   graphics2D.setDefaultBrush(brush3); 
115   pen = new Pen(brush2, 2); 
116   graphics2D.setDefaultPen(pen); 
117   graphics2D.drawChars(font, fontSize, engText, 0
118     engText.length, X, Y); 
119  }  
120   
121 } 
  • 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.

 

介绍了RadioButton和Button 后,这时应该对使用Android提供的控件的用法有了基本的认识。 控件提供了onClick(),onLongClick(),onFocusChange(),onKey(),onTouch(),onCreateContextMenu() 等多种事件以相应用户。用多种方法来处理用户事件。一种是示例代码同过Activity实现OnClickListener接口,再有是采用如下代码为 Button支持事件处理方法:

// Create an anonymous implementation of OnClickListenerprivate 
    OnClickListener mCorkyListener = new OnClickListener() {    
       public void onClick(View v) {      
       // do something when the button is clicked   
       } 
    };    
    protected void onCreate(Bundle savedValues) {    
       ...   
       // Capture our button from layout    
       Button button = (Button)findViewById(R.id.corky);    
       // Register the onClick listener with the implementation above   
       button.setOnClickListener(mCorkyListener);   
       ... 
     } 
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.
  • 11.
  • 12.
  • 13.
  • 14.

在创建自定义控件时,也可以重载onKeyDown(int, KeyEvent),onKeyUp(int, KeyEvent) ,onTouchEvent(MotionEvent)等来处理用户事件。

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

2013-12-27 14:34:46

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

2013-12-27 12:51:44

Android开发Android应用引路蜂

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 13:27:05

Android开发Android应用RadioButton

2013-12-27 16:06:10

Android开发Android应用发布应用

2013-12-26 15:46:30

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

2013-12-26 15:34:19

Android开发Android应用基本概念

2013-12-26 16:59:12

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

2013-12-26 16:24:13

Android开发Android应用Intents

2013-12-27 15:31:26

Android开发Android应用资源Resources

2013-12-26 16:46:21

2013-12-27 13:00:30

Android开发Android应用Context Men

2013-12-26 17:08:36

Android开发Android应用自定义Adapter显

2013-12-27 14:10:36

Android开发Android应用Transform

2013-12-27 15:11:17

Android开发访问Internet绘制在线地图

2013-12-27 12:42:15

Android开发Android应用引路蜂
点赞
收藏

51CTO技术栈公众号