自拍偷在线精品自拍偷,亚洲欧美中文日韩v在线观看不卡

Android開發(fā)速成簡潔教程十六:Button 畫刷示例

移動開發(fā) Android
介紹了RadioButton和Button 后,這時應(yīng)該對使用Android提供的控件的用法有了基本的認識。在創(chuàng)建自定義控件時,也可以重載onKeyDown(int, KeyEvent),onKeyUp(int, KeyEvent) ,onTouchEvent(MotionEvent)等來處理用戶事件。

將RadioButton 換成Button ,類似的在res\layout 中新建brush.xml:

  1. <?xml version=”1.0″ encoding=”utf-8″?> 
  2. <LinearLayout xmlns:android=”http://schemas.android.com/apk/res/android” 
  3.     android:orientation=”vertical” 
  4.     android:background=”@drawable/white” 
  5.  android:layout_width=”fill_parent” 
  6.  android:layout_height=”fill_parent”> 
  7.     <com.pstreets.graphics2d.GuidebeeGraphics2DView 
  8.      android:id=”@+id/graphics2dview” 
  9.      android:layout_weight=”1″ 
  10.      android:layout_width=”fill_parent” 
  11.      android:layout_height=”wrap_content”/> 
  12.  <LinearLayout xmlns:android=”http://schemas.android.com/apk/res/android” 
  13.   android:layout_width=”wrap_content” android:layout_height=”wrap_content” 
  14.   android:orientation=”horizontal” 
  15.    
  16.   > 
  17.    
  18.    <Button android:text=”Pattern” 
  19.        android:id=”@+id/btnPattern” 
  20.     android:layout_width=”wrap_content” 
  21.     android:textColor=”@color/black” 
  22.     android:checked=”true” 
  23.     android:layout_height=”wrap_content”> 
  24.    </Button> 
  25.    <Button android:text=”Gradients” 
  26.         android:id=”@+id/btnGradients” 
  27.     android:layout_width=”wrap_content” 
  28.     android:textColor=”@color/black” 
  29.     android:layout_height=”wrap_content”> 
  30.    </Button> 
  31.   
  32.  </LinearLayout>  
  33.  
  34. </LinearLayout>  

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

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

 

介紹了RadioButton和Button 后,這時應(yīng)該對使用Android提供的控件的用法有了基本的認識。 控件提供了onClick(),onLongClick(),onFocusChange(),onKey(),onTouch(),onCreateContextMenu() 等多種事件以相應(yīng)用戶。用多種方法來處理用戶事件。一種是示例代碼同過Activity實現(xiàn)OnClickListener接口,再有是采用如下代碼為 Button支持事件處理方法:

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

在創(chuàng)建自定義控件時,也可以重載onKeyDown(int, KeyEvent),onKeyUp(int, KeyEvent) ,onTouchEvent(MotionEvent)等來處理用戶事件。

責(zé)任編輯:閆佳明 來源: imobilebbs
相關(guān)推薦

2013-12-27 14:34:46

Android開發(fā)Android應(yīng)用短信觸發(fā)示例

2013-12-27 12:51:44

Android開發(fā)Android應(yīng)用引路蜂

2013-12-26 15:43:07

Android開發(fā)Android應(yīng)用Activities

2013-12-26 15:10:08

Android開發(fā)應(yīng)用和框架Linux 內(nèi)核

2013-12-26 15:18:09

Android開發(fā)安裝開發(fā)環(huán)境

2013-12-27 14:05:22

Android開發(fā)Android應(yīng)用Dialog

2013-12-27 14:16:43

Android開發(fā)Android應(yīng)用線程

2013-12-26 15:34:19

Android開發(fā)Android應(yīng)用基本概念

2013-12-26 16:59:12

Android開發(fā)Android應(yīng)用數(shù)據(jù)綁定Data Bi

2013-12-26 16:24:13

Android開發(fā)Android應(yīng)用Intents

2013-12-27 13:27:05

Android開發(fā)Android應(yīng)用RadioButton

2013-12-27 16:06:10

Android開發(fā)Android應(yīng)用發(fā)布應(yīng)用

2013-12-26 15:46:30

Android開發(fā)Android應(yīng)用用戶界面設(shè)計

2013-12-26 16:46:21

2013-12-27 15:31:26

Android開發(fā)Android應(yīng)用資源Resources

2013-12-26 17:08:36

Android開發(fā)Android應(yīng)用自定義Adapter顯

2013-12-27 13:00:30

Android開發(fā)Android應(yīng)用Context Men

2013-12-27 12:42:15

Android開發(fā)Android應(yīng)用引路蜂

2013-12-27 14:10:36

Android開發(fā)Android應(yīng)用Transform

2013-12-27 15:11:17

Android開發(fā)訪問Internet繪制在線地圖
點贊
收藏

51CTO技術(shù)棧公眾號