Color クラス
Paint クラス
Canvas クラス
View クラス
Path クラス
etc..
More...
Color
アルファ、赤、緑、青 (ARGB) の4つの値で色を表現する。
アルファは透明度で、
アルファ = 0 : 透明
アルファ = 255 : 不透明
・色の指定方法
Colorクラスの静的定数
- int color = Color.BLUE; // 青
- int color = Color.argb(127, 255, 0, 255); // 半透明の紫
XML リソースファイルで定義
- <!--xml version="1.0" encoding="utf-8"?-->
- <resources>
- <color name="color1">#7fff00ff</color>
- </resources>
・色の参照
XML ファイルで付けた名前で参照
- android:background="@color/color1";
Java コードで取得
- int color = getResources().getColor(R.color.color1);
getResources() : 現在の Activity に対応する ResourceManager クラスを返す
getColor() : Resource ID に対応する色を Manager に問い合わせる
Paint
グラフィックスの描画で必要なスタイル、色、その他の情報を管理
・描画色を設定
- Paint cPaint = new Paint();
- cPaint.setColor(Color.LTGRAY); // light gray
Canvas
描画対象のキャンバスを表現
・キャンバスに描画する
View.onDraw() メソッドをオーバーライドする
- static public class GraphicsView extends View {
- public GraphicsView(Context context) {
- super(context);
- }
- @Override
- protected void onDraw(Canvas canvas) {
- // Drawing commands
- }
- }
Path
直線、曲線、矩形などのベクター描画コマンドを持つ
・円形パスを定義
- @Override
- protected void onDraw(Canvas canvas) {
- Path circle = new Path();
- circle.addCircle(150, 150, 100, Path.Direction.CW);
- Paint cPaint = new Paint();
- Paint tPaint = new Paint();
- cPaint.setStyle(Paint.Style.STROKE);
- cPaint.setColor(Color.YELLOW);
- tPaint.setColor(Color.WHITE);
- canvas.drawPath(circle, cPaint);
- final String ss = "Happy Birthday To You !!! Happy Birthday To You !!! Happy Birthday To You !!! Happy Birthday To You !!!";
- canvas.drawTextOnPath(ss, circle, 0, 20, tPaint);
- }
Direction.CW : right hand

- // 色設定
- paint.setColor(Color.argb(255,255,0,0));
- // アンチエイリアス
- paint.setAntiAlias(true);
- // 塗りつぶし
- paint.setStyle(Paint.Style.FILL);
- // 塗りつぶしなし
- paint.setStyle(Paint.Style.STROKE);
- // 枠線の幅
- paint.setStrokeWidth(2);
- // 直線
- canvas.drawLine(25,5,25,5+40,paint);
- // パス
- Path path = new Path();
- path.moveTo(55+ 0,5+ 0);
- path.lineTo(55+30,5+ 5);
- path.lineTo(55+10,5+20);
- path.lineTo(55+40,5+25);
- path.lineTo(55+ 0,5+40);
- canvas.drawPath(path, paint);
- // 四角
- canvas.drawRect(new Rect(0, 0, 40, 40), paint);
- canvas.drawRect(0, 0, 40, 40, paint);
- // 角丸四角
- canvas.drawRoundRect(new RectF(0, 0, 40, 40), 10, 10, paint);
- canvas.drawRoundRect(0, 0, 40, 40, 10, 10, paint);
- // 円
- canvas.drawCircle(100, 100, 150, paint);
PathEffect クラスを使うと面白い効果が作れる
0 件のコメント:
コメントを投稿