鍍金池/ 教程/ Android/ Button 畫刷示例
Dialog 顯示圖像
線程 Bezier 曲線
創(chuàng)建應(yīng)用程序框架
引路蜂二維圖形庫簡介及顏色示例
Android 應(yīng)用基本概念
Intents 和 Intent Filters
安裝開發(fā)環(huán)境
Option Menu 畫筆示例
自定義對話框 Transform
數(shù)據(jù)綁定 Data Binding
概述
Broadcast Receiver 短信觸發(fā)示例
發(fā)布應(yīng)用
自定義 Adapter 顯示列表
RadioButton 多邊形及路徑繪制
訪問 Internet 繪製在線地圖
第一個(gè)應(yīng)用 Hello World
Activities
Button 畫刷示例
使用資源 Resources
Context Menu 繪制幾何圖形
用戶界面設(shè)計(jì)
引路蜂二維圖形繪制實(shí)例功能定義

Button 畫刷示例

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

<?xml version=”1.0″ encoding=”utf-8″?>
<LinearLayout xmlns:android=”[http://schemas.android.com/apk/res/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](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> 

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

public class Brushes extends Graphics2DActivity
   implements OnClickListener { 

 private Button btnPattern;
 private Button btnGradients; 

 public void onCreate(Bundle savedInstanceState) {
  super.onCreate(savedInstanceState);
  setContentView(R.layout.brush);
  graphic2dView = (GuidebeeGraphics2DView)
      findViewById(R.id.graphics2dview);
  btnPattern = (Button) findViewById(R.id.btnPattern);
  btnGradients = (Button) findViewById(R.id.btnGradients);
  btnPattern.setOnClickListener(this);
  btnGradients.setOnClickListener(this);
 } 

 @Override
 protected void drawImage() {
  drawPatterns(); 

 } 

 @Override
 public void onClick(View view) {
  if (view == btnPattern) {
   drawPatterns();
  } else {
   drawGradient();
  }
  graphic2dView.refreshCanvas(); 

 } 

 private void drawPatterns() {
  TextureBrush brush1;
  TextureBrush brush2;
  TextureBrush brush3; 

  AffineTransform matrix1 = new AffineTransform();
  AffineTransform matrix2 = new AffineTransform();
  Bitmap bitmap
    = BitmapFactory.decodeResource(getResources(),
    R.drawable.brick);
  int[] rgbData = new int[bitmap.getHeight()
                          * bitmap.getWidth()];
  bitmap.getPixels(rgbData, 0, bitmap.getWidth(), 0, 0,
    bitmap.getWidth(), bitmap.getHeight());
  brush1 = new TextureBrush(rgbData, bitmap.getWidth(),
    bitmap.getHeight()); 

  bitmap = BitmapFactory.decodeResource(getResources(),
    R.drawable.bird);
  rgbData = new int[bitmap.getHeight() * bitmap.getWidth()];
  bitmap.getPixels(rgbData, 0, bitmap.getWidth(), 0, 0,
    bitmap.getWidth(), bitmap.getHeight());
  brush2 = new TextureBrush(rgbData, bitmap.getWidth(),
    bitmap.getHeight());
  brush3 = new TextureBrush(rgbData, bitmap.getWidth(),
    bitmap.getHeight(), 127);
  matrix2.translate(50, 50);
  // Clear the canvas with white color.
  graphics2D.clear(Color.WHITE);
  graphics2D.setAffineTransform(matrix1);
  graphics2D.fillRectangle(brush1,
     new Rectangle(20, 50, 100, 100));
  graphics2D.fillOval(brush2, 10, 10, 80, 80);
  graphics2D.setAffineTransform(matrix2);
  graphics2D.fillOval(brush3, 10, 10, 80, 80); 

 } 

 private void drawGradient() {
  /* The linear gradient color */
  LinearGradientBrush brush1;
  /* The radial gradient color */
  RadialGradientBrush brush2;
  /* The second radial gradient color */
  RadialGradientBrush brush3; 

  char[] engText = "Brush".toCharArray(); 

  FontEx font = FontEx.getSystemFont(); 

  int fontSize = 44;
  int X = 15;
  int Y = 50;
  int[] fractions = new int[] { 13, 242 };
  Color[] colors = new Color[] { new Color(0xffff6600),
    new Color(0xffffff66) };
  brush1 = new LinearGradientBrush(50, 50, 150, 125,
    fractions, colors,
    Brush.NO_CYCLE); 

  fractions = new int[] { 13, 128, 255 };
  colors = new Color[] { new Color(0xffff6600),
    new Color(0xffffff66),
    new Color(0xffff6600) };
  brush2 = new RadialGradientBrush(90, 100, 50,
    fractions, colors); 

  fractions = new int[] { 0, 255 };
  colors = new Color[] { new Color(0xFFFFFF00),
    new Color(0xFF000000) };
  brush3 = new RadialGradientBrush(50, 50, 100,
    fractions, colors);
  // Clear the canvas with white color.
  graphics2D.clear(Color.white);
  graphics2D.fillRectangle(brush1,
    new Rectangle(10, 75, 120, 80)); 

  Pen pen = new Pen(brush2, 8);
  graphics2D.drawOval(pen, 20, 60, 100, 50);
  graphics2D.setDefaultBrush(brush3);
  pen = new Pen(brush2, 2);
  graphics2D.setDefaultPen(pen);
  graphics2D.drawChars(font, fontSize, engText, 0,
    engText.length, X, Y);
 } 

}

http://wiki.jikexueyuan.com/project/android-development-tutorial/images/26.png" alt="" />

介紹了 RadioButton和Button 后,這時(shí)應(yīng)該對使用 Android 提供的控件的用法有了基本的認(rèn)識(shí)。 控件提供了 onClick(),onLongClick(),onFocusChange(),onKey(),onTouch(),onCreateContextMenu()等多種事件以相應(yīng)用戶。用多種方法來處理用戶事件。一種是示例代碼同過 Activity 實(shí)現(xiàn)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);  
   ...
 }

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