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

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

 
 
 
 
  1.  
  2.     android:orientation=”vertical” 
  3.     android:background=”@drawable/white” 
  4.  android:layout_width=”fill_parent” 
  5.  android:layout_height=”fill_parent”> 
  6.     
  7.      android:id=”@+id/graphics2dview” 
  8.      android:layout_weight=”1″ 
  9.      android:layout_width=”fill_parent” 
  10.      android:layout_height=”wrap_content”/> 
  11.  
  12.   android:layout_width=”wrap_content” android:layout_height=”wrap_content” 
  13.   android:orientation=”horizontal” 
  14.    
  15.   > 
  16.    
  17.    
  18.        android:id=”@+id/btnPattern” 
  19.     android:layout_width=”wrap_content” 
  20.     android:textColor=”@color/black” 
  21.     android:checked=”true” 
  22.     android:layout_height=”wrap_content”> 
  23.     
  24.    
  25.         android:id=”@+id/btnGradients” 
  26.     android:layout_width=”wrap_content” 
  27.     android:textColor=”@color/black” 
  28.     android:layout_height=”wrap_content”> 
  29.     
  30.   
  31.    
  32.  
  33.   

修改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(), 0, 0, 
  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(), 0, 0, 
  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(50, 50); 
  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(20, 50, 100, 100)); 
  67. 67    graphics2D.fillOval(brush2, 10, 10, 80, 80); 
  68. 68    graphics2D.setAffineTransform(matrix2); 
  69. 69    graphics2D.fillOval(brush3, 10, 10, 80, 80);  
  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[] { 13, 242 }; 
  89. 89    Color[] colors = new Color[] { new Color(0xffff6600), 
  90. 90      new Color(0xffffff66) }; 
  91. 91    brush1 = new LinearGradientBrush(50, 50, 150, 125, 
  92. 92      fractions, colors, 
  93. 93      Brush.NO_CYCLE);  
  94. 94    
  95. 95    fractions = new int[] { 13, 128, 255 }; 
  96. 96    colors = new Color[] { new Color(0xffff6600), 
  97. 97      new Color(0xffffff66), 
  98. 98      new Color(0xffff6600) }; 
  99. 99    brush2 = new RadialGradientBrush(90, 100, 50, 
  100. 100     fractions, colors);  
  101. 101   
  102. 102   fractions = new int[] { 0, 255 }; 
  103. 103   colors = new Color[] { new Color(0xFFFFFF00), 
  104. 104     new Color(0xFF000000) }; 
  105. 105   brush3 = new RadialGradientBrush(50, 50, 100, 
  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(10, 75, 120, 80));  
  111. 111   
  112. 112   Pen pen = new Pen(brush2, 8); 
  113. 113   graphics2D.drawOval(pen, 20, 60, 100, 50); 
  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 后,这时应该对使用Android提供的控件的用法有了基本的认识。 控件提供了onClick(),onLongClick(),onFocusChange(),onKey(),onTouch(),onCreateContextMenu() 等多种事件以相应用户。用多种方法来处理用户事件。一种是示例代码同过Activity实现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.      } 

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

网站名称:Android开发速成简洁教程十六:Button画刷示例
浏览路径:http://www.hantingmc.com/qtweb/news5/339955.html

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

广告

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