1) /MyGraphics2D02/src/my/MyGraphics2D02/MyGraphics2D02.java
package my.MyGraphics2D02;
import java.util.ArrayList;
import android.app.Activity;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Color;
import android.os.Bundle;
import android.view.MotionEvent;
import android.view.SurfaceHolder;
import android.view.SurfaceView;
import android.view.Window;
public class MyGraphics2D02 extends Activity {
class GraphicIcon { // 새로운 비트맵 아이콘들을 생성하기 위한 클래스를 추가
private Bitmap bm;
private Coordinates co; // 좌표처리
public GraphicIcon(Bitmap bitmap) {
bm = bitmap;
co = new Coordinates();
}
public Bitmap getGraphic() {
return bm;
}
public Coordinates getCoordinates() {
return co;
}
public class Coordinates {
private int x = 0;
private int y = 0;
public int getX() {
return x;
}
public void setX(int value) {
x = value - bm.getWidth() / 2;
}
public int getY() {
return y;
}
public void setY(int value) {
y = value - bm.getHeight() / 2;
}
}
}
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// Enable extended window features. This is a convenience for calling getWindow().requestFeature().
requestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(new CustomView(this));
}
class CustomView extends SurfaceView implements SurfaceHolder.Callback {
private CustomViewThread CVThread;
// ArrayList Graphics는 GraphicIcon클래스를 사용해 생성한 객체들을 저장
private ArrayList<GraphicIcon> graphics = new ArrayList<GraphicIcon>();
private int x = 70;
private int y = 70;
public CustomView(Context context) {
super(context);
getHolder().addCallback(this);
CVThread = new CustomViewThread(getHolder(), this);
setFocusable(true); // 해당 View가 touch mode에서 Focus가 가도록 지정합니다.
}
@Override
public void onDraw(Canvas canvas) {
Bitmap bm;
canvas.drawColor(Color.parseColor("#dedede"));
GraphicIcon.Coordinates co;
// For문 : ArrayList graphics에 들어 있는 객체들을 하나씩 꺼내어 graphic변수에 저장하여 그림을 그림.
for (GraphicIcon graphic : graphics) {
bm = graphic.getGraphic();
co = graphic.getCoordinates();
canvas.drawBitmap(bm, co.getX(), co.getY(), null);
}
}
@Override
public void surfaceChanged(SurfaceHolder holder, int format, int width, int height) {
// TODO Auto-generated method stub
}
@Override
public void surfaceCreated(SurfaceHolder holder) {
CVThread.setRunning(true);
CVThread.start();
}
@Override
public void surfaceDestroyed(SurfaceHolder holder) {
boolean retry = true;
CVThread.setRunning(false);
while (retry) {
try {
CVThread.join();
retry = false;
} catch (InterruptedException e) {
}
}
}
public boolean onTouchEvent(MotionEvent event) {
synchronized (CVThread.getSurfaceHolder()) { // ConcurrentModificationException 처리
if(event.getAction()==MotionEvent.ACTION_DOWN){ //드레그해도 연속으로 생성되지 않게 함.
GraphicIcon graphic = new GraphicIcon(BitmapFactory.decodeResource(getResources(), R.drawable.icon));
graphic.getCoordinates().setX((int) event.getX());
graphic.getCoordinates().setY((int) event.getY());
graphics.add(graphic);
}
return true;
}
}
}
class CustomViewThread extends Thread {
private SurfaceHolder surfaceholder;
private CustomView customview;
private boolean running = false;
public SurfaceHolder getSurfaceHolder() {
return surfaceholder;
}
public CustomViewThread(SurfaceHolder surfaceHolder, CustomView CustomView) {
surfaceholder = surfaceHolder;
customview = CustomView;
}
public void setRunning(boolean run) {
running = run;
}
@Override
public void run() {
Canvas c;
while (running) {
c = null;
try {
c = surfaceholder.lockCanvas(null);
synchronized (surfaceholder) {
customview.onDraw(c);
}
} finally {
if (c != null) {
surfaceholder.unlockCanvasAndPost(c);
}
}
}
}
}
}
2) Result image
Reference http://www.androidside.com
댓글 없음:
댓글 쓰기