首先是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);
}
}
画圆:
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