package com.bhcom.exercise;
import java.util.Random;
import android.graphics.Canvas;
import android.util.Log;
import android.view.SurfaceHolder;
import android.app.Activity;
import android.os.Bundle;
import android.view.Window;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Color;
import android.graphics.Matrix;
import android.graphics.Paint;
import android.graphics.Rect;
import android.view.MotionEvent;
import android.view.SurfaceView;
public class GraphicMain extends Activity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(new Panel(this));
}
}
//////////
class Panel extends SurfaceView implements SurfaceHolder.Callback {
private int mode = 0;
private Bitmap mBitmap;
private ViewThread mThread;
private int mX;
private int mY;
private int meX;
private int meY;
private int rad; //my variable for radius and movable x and ys
public Panel(Context context) {
super(context);
mBitmap = BitmapFactory.decodeResource(getResources(), R.drawable.icon);
getHolder().addCallback(this);
mThread = new ViewThread(this);
}
public void doDraw(Canvas canvas) {
/*because of the thread this is looping like draw() in Processing*/
//simple way to make circle grow then go small again
//최초시작시 아무것도 하지 않음.
if (mY == 0 || mX == 0) {
return;
}
if (mode == 1) {
rad--;
}else {
rad++;
}
//background black
Paint paint = new Paint();
Random rnd = new Random();
// paint.setARGB(255, rnd.nextInt(255), rnd.nextInt(255), rnd.nextInt(255));
paint.setColor(Color.RED);
// paint.setFlags(Paint.ANTI_ALIAS_FLAG);
paint.setStrokeWidth(5);
Log.i("1", "rad : "+rad);
canvas.rotate(rad*8, mX, mY);
canvas.drawPoint(mX+(rad/4), mY+rad/4, paint);
}
@Override
public void surfaceChanged(SurfaceHolder holder, int format, int width, int height) {
// TODO Auto-generated method stub
}
@Override
public void surfaceCreated(SurfaceHolder holder) {
mX = 0;
mY = 0;
if (!mThread.isAlive()) {
mThread = new ViewThread(this);
mThread.setRunning(true);
mThread.start();
}
}
@Override
public void surfaceDestroyed(SurfaceHolder holder) {
if (mThread.isAlive()) {
mThread.setRunning(false);
}
}
@Override
public boolean onTouchEvent(MotionEvent event) {
if (mode == 1) {
mode = 0;
}else {
mode = 1;
}
rad = 0;
mX = (int) event.getX() - mBitmap.getWidth() / 2;
mY = (int) event.getY() - mBitmap.getHeight() / 2;
return super.onTouchEvent(event);
}
}
/////////
class ViewThread extends Thread {
private Panel mPanel;
private SurfaceHolder mHolder;
private boolean mRun = false;
public ViewThread(Panel panel) {
mPanel = panel;
mHolder = mPanel.getHolder();
}
public void setRunning(boolean run) {
mRun = run;
}
@Override
public void run() {
Canvas canvas = null;
while (mRun) {
canvas = mHolder.lockCanvas();
if (canvas != null) {
mPanel.doDraw(canvas);
mHolder.unlockCanvasAndPost(canvas);
}
}
}
}
아.. 돌아가는 소스 라인으로 하고싶었는데 할줄 몰라서 결국엔 포인트로..... 쳇..
아.. 돌아가는 소스 라인으로 하고싶었는데 할줄 몰라서 결국엔 포인트로..... 쳇..
'개발 > 안드로이드 소스분석' 카테고리의 다른 글
box2d - particle system demo (0) | 2011.08.29 |
---|---|
2dEngine - sample (0) | 2011.08.24 |
[안드로이드]apiDemos파해치기 6. App - QuickContactsDemos (0) | 2011.06.30 |
[안드로이드]apiDemos파해치기 5. App - persistent State (0) | 2011.06.21 |
[안드로이드]apiDemos파해치기 4. App - forwarding (0) | 2011.06.20 |