badboy1110知識,首先是Bitmap:
從資源文件中得到bitmap有兩種方式:
一:
- Resources r = getResources();
- InputStream is = r.openRawResource(R.drawable.icon);
- BitmapDrawable bmpDraw = new BitmapDrawable(is);
- Bitmap bmp1 = bmpDraw.getBitmap();
二:
- BitmapDrawable drawable = (BitmapDrawable)r.getDrawable(R.drawable.icon)
一下是畫各種圖形,首先寫了一個畫圖形的接口:
- package com.song.ui;
-
- import Android.graphics.Canvas;
-
- public interface drawGraphics
- {
- public void draw(Canvas canvas);
- }
然後花各種圖形的時候實現接口:
畫線:
- package com.song.ui;
-
- import android.graphics.Canvas;
- import android.graphics.Color;
- import android.graphics.Paint;
-
- /**
- * 畫線
- * @author song
- *
- */
- public class DrawLine implements drawGraphics
- {
- //聲明一個畫筆
- private Paint mPaint;
-
- public DrawLine()
- {
- mPaint = new Paint();
- }
-
- @Override
- public void draw(Canvas canvas)
- {
- //消除鋸齒
- mPaint.setAntiAlias(true);
- //直線 顏色:綠色
- mPaint.setColor(Color.GREEN);
- //設置線條粗細
- mPaint.setStrokeWidth(12);
- //畫兩條直線
- canvas.drawLine(90, 40, 140, 90, mPaint);
- canvas.drawLine(290, 90, 340, 40, mPaint);
- }
- }
畫圓:
view plaincopy to clipboardprint?
- package com.song.ui;
-
- import android.graphics.Canvas;
- import android.graphics.Color;
- import android.graphics.Paint;
- import android.graphics.RectF;
-
- /**
- * 畫圓
- * @author song
- *
- */
- public class DrawCircle implements drawGraphics
- {
- private Paint paint ;
- private Paint paint_eye;
-
- public DrawCircle()
- {
- paint= new Paint();
- paint_eye=new Paint();
- }
-
- @Override
- public void draw(Canvas canvas)
- {
- //繪制圓形(圓心X,圓心Y,半徑R,畫筆P)
- paint_eye.setAntiAlias(true);
- paint.setAntiAlias(true);
- RectF rectF= new RectF(90, 60, 340, 240);
- paint_eye.setColor(Color.WHITE);
- paint.setColor(Color.GREEN);
- canvas.drawArc(rectF, 180, 180, true, paint);
- //畫兩個眼睛
- canvas.drawCircle(160, 110, 18, paint_eye);
- canvas.drawCircle(270, 110, 18, paint_eye);
- }
-
- }
畫矩形:
- package com.song.ui;
-
- import android.graphics.Canvas;
- import android.graphics.Color;
- import android.graphics.Paint;
- import android.graphics.RectF;
-
- /**
- * 畫矩形
- *
- * @author song
- *
- */
- public class DrawRect implements drawGraphics
- {
- private Paint paint;
-
- public DrawRect()
- {
- paint = new Paint();
- }
-
- @Override
- public void draw(Canvas canvas)
- {
- // 定義圓角矩形對象
- RectF rectF1 = new RectF(90, 170, 340, 500);
- RectF rectF2 = new RectF(10, 150, 60, 400);
- RectF rectF3 = new RectF(360, 150, 410, 400);
- RectF rectF4 = new RectF(110, 520, 170, 650);
- RectF rectF5 = new RectF(260, 520, 320, 650);
- paint.setAntiAlias(true);
- // 設置畫筆顏色為BLUE
- paint.setColor(Color.GREEN);
- // 在畫布上繪制圓角矩形/圓弧/直線
- canvas.drawRoundRect(rectF1, 20, 20, paint);
- canvas.drawRoundRect(rectF2, 20, 20, paint);
- canvas.drawRoundRect(rectF3, 20, 20, paint);
- canvas.drawRoundRect(rectF4, 20, 20, paint);
- canvas.drawRoundRect(rectF5, 20, 20, paint);
- }
-
- }
這個可以畫出來一個Android的機器人,
工程名:TL03_graphics_test