Android描画ステップの1つ:グラフィックの描画

12141 ワード

描画の学習を開始します.まず、点線面やパスの描画など、画像の基本要素の描画に関連します.難易度はありません.コードの例は次のとおりです.

  
  
  
  
  1. package com.mike.activity; 
  2.  
  3. import android.R; 
  4. import android.app.Activity; 
  5. import android.graphics.Bitmap; 
  6. import android.graphics.Canvas; 
  7. import android.graphics.Color; 
  8. import android.graphics.Paint; 
  9. import android.graphics.Paint.Style; 
  10. import android.graphics.Path; 
  11. import android.graphics.RectF; 
  12. import android.graphics.drawable.BitmapDrawable; 
  13. import android.os.Bundle; 
  14. import android.view.ViewGroup.LayoutParams; 
  15. import android.widget.ImageView; 
  16. import android.widget.LinearLayout; 
  17.  
  18. public class DrawDemoActivity extends Activity { 
  19.     /** Called when the activity is first created. */ 
  20.     @Override 
  21.     public void onCreate(Bundle savedInstanceState) { 
  22.         super.onCreate(savedInstanceState); 
  23.  
  24.         /* 
  25.          *  � :1,ALPHA_8:  Alpha , alpha 8 ,  
  26.          *           2,ARGB_4444,  
  27.          *           3,ARGB_8888,  
  28.          *           4,RGB565( ARGB_8888 , ) 
  29.          *  
  30.          * ARGB_8888 : 8 ,  
  31.          * param :      A:alpha : , , 256  
  32.          *           R:red    
  33.          *           G:green     
  34.          *           B:blue 
  35.          *  
  36.          *  
  37.          * Note:  : 。 , 、 , 
  38.          *              PS , 。 
  39.                                                                 : , , 
  40.                                                                 , , 
  41.                                                                 , 。 
  42.                                                                 , , 。 
  43.          *  
  44.          */ 
  45.         Bitmap bitmap = Bitmap.createBitmap(getWindowManager().getDefaultDisplay().getWidth(), 
  46.                 getWindowManager().getDefaultDisplay().getHeight(), 
  47.                 Bitmap.Config.ARGB_8888);//  
  48.          
  49.      
  50.          
  51.     Canvas canvas = new Canvas(bitmap); //  
  52.      
  53.     Paint paint = new Paint(); 
  54.     paint.setColor(Color.WHITE);// , Argb ,,  
  55. //  int myColor = Color.argb(alpha, red, green, blue); 
  56.      
  57.      
  58.     /* 
  59.      *  : 
  60.      *         STROKE   
  61.      *         FILL     
  62.      *         FILL_AND_STROKE   
  63.      */ 
  64.      
  65.     //1:  
  66. //  paint.setStyle(Style.STROKE); , ok~ 
  67. //  paint.setStrokeWidth(100); 
  68. //  canvas.drawPoint(199, 201, paint); 
  69.      
  70.      
  71.     //2,  
  72. //  paint.setStyle(Style.STROKE); , ok~ 
  73. //  paint.setStrokeWidth(10); 
  74. //  canvas.drawLine(50, 50, 100, 100, paint); 
  75.      
  76.     //3,  
  77. //  paint.setStyle(Style.FILL_AND_STROKE); 
  78. //  paint.setStrokeWidth(10); 
  79. //  canvas.drawRect(50, 50, 100, 100, paint);// : RectF  
  80.      
  81.     //4,  
  82. //  paint.setStyle(Style.STROKE); 
  83. //  paint.setStrokeWidth(10); 
  84. //  RectF oval = new RectF(10, 10, 400, 200) ;//  
  85. //  canvas.drawOval(oval , paint); 
  86.      
  87.     //5,  
  88. //  paint.setStyle(Style.STROKE); 
  89. //  paint.setStrokeWidth(10); 
  90. //  canvas.drawCircle(100, 100, 50, paint); 
  91.      
  92.     //6, : Path  
  93. //  paint.setStyle(Style.STROKE); 
  94. //  paint.setStrokeWidth(10); 
  95. //  Path path = new Path(); 
  96. //  path.moveTo(20, 20);//  
  97. //  path.lineTo(30, 30); 
  98. //  path.lineTo(40, 60); 
  99. //  path.lineTo(70, 100); 
  100. //  canvas.drawPath(path, paint); 
  101.      
  102.     ImageView imageView = new ImageView(this); 
  103.      
  104.     LayoutParams p = new LayoutParams(LinearLayout.LayoutParams.FILL_PARENT,LinearLayout.LayoutParams.FILL_PARENT); 
  105.     imageView.setLayoutParams(p); 
  106.     imageView.setBackgroundDrawable(new BitmapDrawable(bitmap)); 
  107.      
  108.      
  109.     setContentView(imageView); 
  110.      
  111.  
  112.     }