"안드로이드 그림판 초간단 예제"의 두 판 사이의 차이

 
(같은 사용자의 중간 판 2개는 보이지 않습니다)
37번째 줄: 37번째 줄:
@Override
@Override
public boolean onTouch(View v, MotionEvent event) {
public boolean onTouch(View v, MotionEvent event) {
int action = event.getAction();
switch( event.getAction() ) {
if(action == MotionEvent.ACTION_MOVE) {
case MotionEvent.ACTION_MOVE:
points.add(new Point(event.getX(), event.getY(), true));
points.add(new Point(event.getX(), event.getY(), true));
invalidate();
invalidate();
break;
case MotionEvent.ACTION_UP:
case MotionEvent.ACTION_DOWN:
points.add(new Point(event.getX(), event.getY(), false));
}
}
else if (action == MotionEvent.ACTION_UP || action == MotionEvent.ACTION_DOWN)
points.add(new Point(event.getX(), event.getY(), false));
return true;
return true;
}
}
52번째 줄: 54번째 줄:
super.onDraw(canvas);
super.onDraw(canvas);
Paint p = new Paint();
Paint p = new Paint();
p.setColor(Color.RED);
p.setColor(Color.MAGENTA);
p.setStrokeWidth(3);
p.setStrokeWidth(3);
for(int i=1; i<points.size();i++) {
for(int i=1; i<points.size(); i++) {
if(!points.get(i).isDraw) continue;
if(!points.get(i).isDraw) continue;
canvas.drawLine(points.get(i-1).x, points.get(i-1).y, points.get(i).x, points.get(i).y, p);
canvas.drawLine(points.get(i-1).x, points.get(i-1).y, points.get(i).x, points.get(i).y, p);
70번째 줄: 72번째 줄:
}
}
</source>
</source>
==실행결과==
[[파일: 안드로이드 그림판 초간단 예제 실행결과.png]]


[[분류: 안드로이드]]
[[분류: 안드로이드]]

2013년 7월 1일 (월) 23:51 기준 최신판

안드로이드 그림판 초간단 예제

1 소스 코드[ | ]

package com.jmnote.android.mypaint;

import java.util.ArrayList;

import android.annotation.SuppressLint;
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.MotionEvent;
import android.view.View;
import android.view.ViewGroup.LayoutParams;

@SuppressLint("DrawAllocation")
public class MainActivity extends Activity {
	class Point {
		float x;
		float y;
		boolean isDraw;
		public Point(float x, float y, boolean isDraw) {
			this.x = x;
			this.y = y;
			this.isDraw = isDraw;
		}
	}
	class MyPaint extends View {
		public MyPaint(Context context) {
			super(context);
			setLayoutParams(new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT));
			this.setOnTouchListener(new OnTouchListener() {
				@Override
				public boolean onTouch(View v, MotionEvent event) {
					switch( event.getAction() ) {
					case MotionEvent.ACTION_MOVE:
						points.add(new Point(event.getX(), event.getY(), true));
						invalidate();
						break;
					case MotionEvent.ACTION_UP:
					case MotionEvent.ACTION_DOWN: 
						points.add(new Point(event.getX(), event.getY(), false));
					}
					return true;
				}
			});
		}
		@Override
		protected void onDraw(Canvas canvas) {
			super.onDraw(canvas);
			Paint p = new Paint();
			p.setColor(Color.MAGENTA);
			p.setStrokeWidth(3);
			for(int i=1; i<points.size(); i++) {
				if(!points.get(i).isDraw) continue;
				canvas.drawLine(points.get(i-1).x, points.get(i-1).y, points.get(i).x, points.get(i).y, p);
			}
		}
	}
	ArrayList<Point> points = new ArrayList<Point>();

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		MyPaint mp = new MyPaint(this);
		setContentView(mp);
	}
}

2 실행결과[ | ]

안드로이드 그림판 초간단 예제 실행결과.png

문서 댓글 ({{ doc_comments.length }})
{{ comment.name }} {{ comment.created | snstime }}