------

[ AD ] Port Monitor ( Try to use a Best WebSite Monitoring Tool )

------

메인 소스
public class CanvasLissajous extends Activity {


	CanvasView mCanvasView;
	private TextView mTextView;
	
	Random mRandom;
	
	//int A=13;
	//int B=19;
	
	
	
	//private int A=10000;
	//private int B=20000;
    Timer mTimer = new Timer();
    TimerTask mTimerTask = new TimerTask() {

		@Override
		public void run() {
			// TODO Auto-generated method stub
			mCanvasView.A = mRandom.nextInt(Def.RANGE);
			if (mCanvasView.A == 0) mCanvasView.A =1;
			
			mCanvasView.B = mRandom.nextInt(Def.RANGE);
			if ( mCanvasView.B == 0) mCanvasView.B =1 ;
			/*
			mCanvasView.A++;
			if ( mCanvasView.A > 500) mCanvasView.A=1 ;
			mCanvasView.B++;
			if ( mCanvasView.B > 500) mCanvasView.B=1;
			*/
		}
    };

    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        this.requestWindowFeature(Window.FEATURE_NO_TITLE);
        //CanvasView 
        //mCanvasView = new CanvasView(this);
        //setContentView(mCanvasView ); //R.layout.main);
        setContentView(R.layout.main);
        
        mCanvasView = (CanvasView)findViewById(R.id.canvasview);
        mTextView = (TextView)findViewById(R.id.textview);
        mCanvasView.setTextView(mTextView);
		mTimer.schedule(mTimerTask, Def.INTERVAL, Def.INTERVAL);
		mRandom = new Random();

    }


}

CanvasView 소스
public class CanvasView extends View {
	
	Context mContext;
	private TextView mTextView;
	
	public CanvasView(Context context, AttributeSet attrs) {
		super(context, attrs);
		mContext = context;
		
		
		
		// TODO Auto-generated constructor stub
		mHandler.sendEmptyMessageDelayed(0, Def.INTERVAL);
	}
	int w,h;
	int A=1,B=1;
	Canvas mCanvas;
	
	private int CenterX,CenterY;

	/*
	public CanvasView(Context context) {
		super(context);
		// TODO Auto-generated constructor stub
		
	}
	*/
	Handler mHandler = new Handler() {

		@Override
		public void handleMessage( Message msg ) {
			// TODO Auto-generated method stub
			//super.handleMessage(msg);
			drawCurve(mCanvas);
			invalidate();
			mHandler.sendEmptyMessageDelayed(0, Def.INTERVAL);
		}
		
	};
	void drawCurve(Canvas canvas) {
		w = getWidth();
		h = getHeight();
		
		CenterX = w/2;
		CenterY = h/2;
		
		int x1 = CenterX*2;
		int y1 = CenterY;
		
		Paint paint = new Paint();
		canvas.drawColor(Color.WHITE);
		
		paint.setColor(Color.BLUE);
		paint.setTextSize(24);
		
		//canvas.drawText("A:"+ A + " B: "+B, 10, 30, paint);
		getTextView().setText("A:"+ A + " B: "+B);
		
		for(float r=0; r<= Math.PI*3; r += 0.01) {
			int x2 = (int)(CenterX + Math.cos(r*A) * CenterX );
			int y2 = (int)(CenterY - Math.sin(r*B) * CenterY );
			canvas.drawLine(x1, y1, x2, y2, paint);
			//try { Thread.sleep(100); } catch (InterruptedException e) {;}
			x1 = x2 ;
			y1 = y2 ;
		}
	}
	@Override
	protected void onDraw(Canvas canvas) {
		// TODO Auto-generated method stub
		
		mCanvas = canvas;
		
		drawCurve(canvas);
		
		//super.onDraw(canvas);
	}
	public void setTextView(TextView mTextView) {
		this.mTextView = mTextView;
	}
	public TextView getTextView() {
		return mTextView;
	}

}



'0.일반개발' 카테고리의 다른 글

HTML 제거하기  (0) 2010.09.28
ECMA 262 Brief History  (0) 2010.09.28
JNI ffmpeg  (0) 2010.09.28
삼각함수 trigonometric function 기초  (0) 2010.09.28
아날로그 시계  (0) 2010.09.27

+ Recent posts