Android描画ステップの1つ:グラフィックの描画
描画の学習を開始します.まず、点線面やパスの描画など、画像の基本要素の描画に関連します.難易度はありません.コードの例は次のとおりです.
- package com.mike.activity;
-
- import android.R;
- import android.app.Activity;
- import android.graphics.Bitmap;
- import android.graphics.Canvas;
- import android.graphics.Color;
- import android.graphics.Paint;
- import android.graphics.Paint.Style;
- import android.graphics.Path;
- import android.graphics.RectF;
- import android.graphics.drawable.BitmapDrawable;
- import android.os.Bundle;
- import android.view.ViewGroup.LayoutParams;
- import android.widget.ImageView;
- import android.widget.LinearLayout;
-
- public class DrawDemoActivity extends Activity {
- /** Called when the activity is first created. */
- @Override
- public void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
-
- /*
- * � :1,ALPHA_8: Alpha , alpha 8 ,
- * 2,ARGB_4444,
- * 3,ARGB_8888,
- * 4,RGB565( ARGB_8888 , )
- *
- * ARGB_8888 : 8 ,
- * param : A:alpha : , , 256
- * R:red
- * G:green
- * B:blue
- *
- *
- * Note: : 。 , 、 ,
- * PS , 。
- : , ,
- , ,
- , 。
- , , 。
- *
- */
- Bitmap bitmap = Bitmap.createBitmap(getWindowManager().getDefaultDisplay().getWidth(),
- getWindowManager().getDefaultDisplay().getHeight(),
- Bitmap.Config.ARGB_8888);//
-
-
-
- Canvas canvas = new Canvas(bitmap); //
-
- Paint paint = new Paint();
- paint.setColor(Color.WHITE);// , Argb ,,
- // int myColor = Color.argb(alpha, red, green, blue);
-
-
- /*
- * :
- * STROKE
- * FILL
- * FILL_AND_STROKE
- */
-
- //1:
- // paint.setStyle(Style.STROKE); , ok~
- // paint.setStrokeWidth(100);
- // canvas.drawPoint(199, 201, paint);
-
-
- //2,
- // paint.setStyle(Style.STROKE); , ok~
- // paint.setStrokeWidth(10);
- // canvas.drawLine(50, 50, 100, 100, paint);
-
- //3,
- // paint.setStyle(Style.FILL_AND_STROKE);
- // paint.setStrokeWidth(10);
- // canvas.drawRect(50, 50, 100, 100, paint);// : RectF
-
- //4,
- // paint.setStyle(Style.STROKE);
- // paint.setStrokeWidth(10);
- // RectF oval = new RectF(10, 10, 400, 200) ;//
- // canvas.drawOval(oval , paint);
-
- //5,
- // paint.setStyle(Style.STROKE);
- // paint.setStrokeWidth(10);
- // canvas.drawCircle(100, 100, 50, paint);
-
- //6, : Path
- // paint.setStyle(Style.STROKE);
- // paint.setStrokeWidth(10);
- // Path path = new Path();
- // path.moveTo(20, 20);//
- // path.lineTo(30, 30);
- // path.lineTo(40, 60);
- // path.lineTo(70, 100);
- // canvas.drawPath(path, paint);
-
- ImageView imageView = new ImageView(this);
-
- LayoutParams p = new LayoutParams(LinearLayout.LayoutParams.FILL_PARENT,LinearLayout.LayoutParams.FILL_PARENT);
- imageView.setLayoutParams(p);
- imageView.setBackgroundDrawable(new BitmapDrawable(bitmap));
-
-
- setContentView(imageView);
-
-
- }
- }