2010年3月27日土曜日

Android簡単サンプル【パス描画】

【ソース】
package com.naozary.android.example.simple;

import android.app.Activity;
import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Path;
import android.os.Bundle;
import android.view.View;

public class simple extends Activity {
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        // setContentView(R.layout.main);
        setContentView(new GraphicsTestView(this));

    }
}

class GraphicsTestView extends View {
    public GraphicsTestView(Context context) {
        super(context);
    }

    @Override
    protected void onDraw(Canvas canvas) {
        Paint paint = new Paint();
        paint.setColor(
                Color.argb(
                        0xff,        // 透明度
                        0,            // Red
                        0xff,        // Green
                        0            // Blue
                )
        );
        paint.setStyle(Paint.Style.STROKE);
      
        Path path = new Path();
      
        // 三角形を作成する
        path.moveTo(40,120);        // Cuurent Pointを指定
        path.lineTo(90, 20);        // 絶対位置で直線を追加
        path.lineTo(140,120);        // 絶対位置で直線を追加
        path.close();
      
        // 四角形を作成する
        path.moveTo(160,20);        // Cuurent Pointを指定
        path.rLineTo(0, 100);        // 相対移動で線を追加
        path.rLineTo(100,0);        // 相対移動で線を追加
        path.rLineTo(0,-100);        // 相対移動で線を追加
        path.close();
      
      
        // ベジェ曲線(3次ベジェ曲線)を作成する
        path.moveTo(20,200);      
        path.rCubicTo(
                30,    // The x-coordinate of the 1st control point on a cubic curve
                -50,        // The y-coordinate of the 1st control point on a cubic curve
                200,        // The x-coordinate of the 2nd control point on a cubic curve
                -50,    // The y-coordinate of the 2nd control point on a cubic curve
                250,    // The x-coordinate of the end point on a cubic curve
                0        // The y-coordinate of the end point on a cubic curve
        );
        path.close();
      
      
      

       
        path.moveTo(10,250);
        path.rCubicTo(50, -40, 150, 40, 250, 0);
     


        path.moveTo(10,300);
        path.rCubicTo(100, -60, 150, 60, 250, 0);

      
        path.moveTo(10,380);
        path.rCubicTo(100, -60, 150, -60, 250, 0);
      
      
        canvas.drawPath(path, paint);

      
    }
}

【実行結果】

0 件のコメント:

コメントを投稿