public void onDraw(Canvas canvas) {
c.drawColor(Color.BLACK);
Paint paint = new Paint();
paint.setStyle(Paint.Style.FILL);
paint.setColor(Color.BLUE);
canvas.drawRect(500f, 500f, 1000f, 1000f, paint);
fill_paint.setColor(Color.RED);
canvas.drawOval(new RectF(0f, 0f, 500f, 500f), paint);
}
Bitmap bitmap = Bitmap.createBitmap(width,height,Bitmap.Config.ARGB_8888);
Canvas canvas = new Canvas(bitmap);
Paint paint = new Paint();
paint.setAntiAlias(true);
paint.setStrokeWidth(5);
paint.setStyle(Paint.Style.STROKE);
paint.setColor(Color.argb(255, 255, 255, 255));
Path path = new Path();
path.moveTo(100, 100);
path.lineTo(500, 100);
path.lineTo(500, 500);
path.lineTo(100, 500);
path.lineTo(100, 100);
canvas.drawPath(path, paint);
paint.setColor(0xffffffff);
paint.setStrokeWidth(10);
paint.setStrokeCap(Paint.Cap.SQUARE);
canvas.drawPoint(100,100, paint);
float[] pts = {200, 200, 400, 400};
paint.setStrokeCap(Paint.Cap.ROUND);
canvas.drawPoints(pts, paint);
paint.setStrokeWidth(5);
canvas.drawLine(100,100, 500,500, paint);
paint.setPathEffect(new DashPathEffect(new float[]{ 5.0f, 5.0f }, 0));
paint.setStyle(Paint.Style.STROKE);
canvas.drawLine(600,100, 1000,500, paint);
canvas.drawRect(100,100, 500,500, paint);
canvas.drawCircle(300,300, 200, paint);
paint.setTextSize(50); // Textサイズの指定
paint.setStyle(Style.FILL); // Textを塗潰しにする
paint.setShadowLayer(1.5f,4f,4f,Color.BLACK); // Textに影をつける
canvas.drawText("ABC",x,y,paint); // Textの描画
String text = "ABC";
float tw = paint.measureText(text); // 文字列の幅を求める
float th = paint.getFontMetrics(null); // 文字列の高さを求める
Paint.FontMetrics fontMetrics = paint.getFontMetrics();
float top = fontMetrics.top;
float ascent = fontMetrics.ascent;
float descent = fontMetrics.descent;
float bottom = fontMetrics.bottom;
float th = descent - ascent; // 文字列の高さを求める
canvas.drawBitmap(bitmap, x, y, paint);
Rect srcrect = new Rect(0, 0, bitmap.getWidth()/2, bitmap.getHeight()/2);
Rect dstrect = new Rect(0, 0, bitmap.getWidth(), bitmap.getHeight());
dstrect.offset(100, 100);
paint.setFilterBitmap(true);
canvas.drawBitmap(bitmap, srcrect, dstrect, paint);
Bitmap bmp = Bitmap.createBitmap(32, 32, Bitmap.Config.ARGB_8888);
for (int y = 0;y < 32;y++) {
for (int x = 0;x < 32;x++) {
int xx=x-16;
int yy=y-16;
if (xx*xx+yy*yy<225)
bmp.setPixel(x, y, Color.argb(255, 255, 0, 0));
}
}
Bitmap.Config | 1ピクセルあたりの、byte数/定義内容 |
---|---|
ARGB_8888 | 4byte/αチャネル(8bit)、R(8bit)、G(8bit)、B(8bit) |
ARGB_4444 | 2byte/αチャネル(4bit)、R(4bit)、G(4bit)、B(4bit) |
RGB_565 | 2byte/αチャネルなし、R(5bit)、G(6bit)、B(5bit) |
ALPHA_8 | 1byte/αチャネル(8bit)のみ |
Bitmap bmpx = BitmapFactory.decodeResource(context.getResources(),R.drawable.image_file);
public static void png_out(String fname) {
try {
// 出力ファイルの準備
FileOutputStream fos = new FileOutputStream(new File(fname+".png"));
// PNG形式で出力
bitmap.compress(CompressFormat.PNG, 100, fos);
fos.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
public Bitmap getViewBitmap(View view){
view.setDrawingCacheEnabled(true);
Bitmap cache = view.getDrawingCache();
if(cache == null){
return null;
}
Bitmap bitmap = Bitmap.createBitmap(cache);
view.setDrawingCacheEnabled(false);
return bitmap;
}
private Bitmap capture(int width, int height) {
final int pixels[] = new int[width * height];
final IntBuffer buffer = IntBuffer.wrap(pixels);
buffer.position(0);
GLES20.glReadPixels(0, 0, width, height, GLES20.GL_RGBA, GLES20.GL_UNSIGNED_BYTE, buffer);
return GLES20Utils.createBitmap(pixels, width, height, Bitmap.Config.ARGB_8888);
}