|
HTML HTML5 HTMLタグ スマートフォン |
CSS CSSプロパティ CSS・HTML便利ツール |
HTML色見本 配色組み合わせツール 特殊文字 |
JAVA Android |
PHP Smarty修飾子 EXCEL |
*このページは web-dou.com のアーカイブです。(2025年 サイト統合)
android.graphicsパッケージの Canvasクラスを使用します。
Canvas.drawRect()やdrawRoundRect()で矩形を描画し、色・塗りつぶしなどは Paintインスタンスに設定します。
package jp.mediawing.android.test;
import android.app.Activity;
import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.os.Bundle;
import android.view.View;
public class TestActivity extends Activity {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(new DrawTest(this));
}
// Viewをextendsしたクラスを作成し描画処理をする
static public class DrawTest extends View {
public DrawTest(Context context) {
super(context);
}
// 描画処理を記述
@Override
protected void onDraw(Canvas canvas) {
Paint paint = new Paint();
paint.setColor(Color.argb(255, 255, 255, 255));
Rect rect = new Rect(10, 20, 30, 40);
canvas.drawRect(rect, paint);
RectF rectF = new RectF(40.5f, 20.5f, 60.5f, 40.5f);
canvas.drawRect(rectF, paint);
paint.setStyle(Paint.Style.STROKE);
paint.setStrokeWidth(12);
canvas.drawRect(10, 50, 30, 80, paint);
}
}
}
| 引数 | 説明 |
|---|---|
| left | 左(x) |
| top | 上(y) |
| right | 右(x) |
| bottom | 下(y) |
| paint | Paintクラスのインスタンス |
| 引数 | 説明 |
|---|---|
| rect | Rectオブジェクト |
| paint | Paintクラスのインスタンス |
| 引数 | 説明 |
|---|---|
| rect | Rectオブジェクト |
| rx | 丸みX |
| ry | 丸みY |
| paint | Paintクラスのインスタンス |