package org.yasrun.test4;
import java.util.ArrayList;
import java.util.List;
import android.app.Activity;
import android.content.Context;
import android.content.res.Resources;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Paint.Style;
import android.os.Bundle;
import android.view.MotionEvent;
import android.view.View;
/**
* 起動クラス
*/
public class Test4Activity extends Activity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle icicle) {
super.onCreate(icicle);
/* 描画クラスのインスタンスを生成 */
MyView mView = new MyView(getApplication());
/* 現在のViewに設定 */
setContentView(mView);
}
}
class Point {
public Point(int x, int y) {
this.X = x;
this.Y = y;
}
public int X = 0;
public int Y = 0;
}
/**
* 描画用のクラス
*/
class MyView extends View {
private Paint myPaint = new Paint();
private Paint myPaint2 = new Paint();
private Bitmap myBitmap;
private List<Point> list = new ArrayList<Point>();
/* 画像のx座標 */
int x;
/* 画像のy座標 */
int y;
/**
* コンストラクタ
*
* @param c
*/
public MyView(Context c) {
super(c);
/* イベントが取得できるようにFocusを有効にする */
setFocusable(true);
/* Resourceインスタンスの生成 */
Resources res = this.getContext().getResources();
/* 画像の読み込み(res/drawable/gclue.png) */
myBitmap = BitmapFactory.decodeResource(res, R.drawable.icon);
/* x,y座標の初期化 */
x = 0;
y = 0;
list.add(new Point(x + 24, y + 24));
myPaint2.setColor(Color.YELLOW);
myPaint2.setStyle(Style.STROKE);
}
/**
* 描画処理
*/
protected void onDraw(Canvas canvas) {
/* 背景色を設定 */
canvas.drawColor(Color.BLUE);
/* 線描画 */
boolean first = true;
int prevX = 0;
int prevY = 0;
for (Point p : list) {
if (!first) {
canvas.drawLine(prevX, prevY, p.X, p.Y, myPaint2);
} else {
first = false;
}
prevX = p.X;
prevY = p.Y;
}
/* Bitmapイメージの描画 */
canvas.drawBitmap(myBitmap, x, y, myPaint);
}
/**
* タッチイベント
*/
public boolean onTouchEvent(MotionEvent event) {
// タッチした時に実行
// 曲線にする場合はACTION_DOWNではなくACTION_MOVEにする。
if (event.getAction() == MotionEvent.ACTION_DOWN) {
/* X,Y座標の取得 */
x = (int) event.getX();
y = (int) event.getY();
list.add(new Point(x + 24, y + 24));
/* 再描画の指示 */
invalidate();
}
return true;
}
}
最終更新:2011年03月07日 23:28