TQC-309 觸控位置並畫圖

TQC-309 畫圖

這是TQC的309題,

程式碼為自行撰寫,

僅供參考。

 

主要練習用SurfaceView畫圖,

還有,必須使用觸控取得畫面位置,

再加上Runnable更新,

程式要先理解一下,

再動手寫。

 



    
        
            
                
                
            
        

    
    

 

 




 

 

package COM.TQC.GDD03;

import java.util.Random;

import android.app.Activity;
import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.RectF;
import android.os.Bundle;
import android.view.GestureDetector;
import android.view.MotionEvent;
import android.view.SurfaceHolder;
import android.view.SurfaceView;

public class GDD03 extends Activity 
{	
	MySurfaceView mySurfaceView;
	
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
	    mySurfaceView = new MySurfaceView(this);
	    setContentView(mySurfaceView);
	   }
	   
	 @Override
	 protected void onResume() {
	  // TODO Auto-generated method stub
	  super.onResume();
	  mySurfaceView.onResumeMySurfaceView();
	 }
	 
	 @Override
	 protected void onPause() {
	  // TODO Auto-generated method stub
	  super.onPause();
	  mySurfaceView.onPauseMySurfaceView();
	 }
	 
	 class MySurfaceView extends SurfaceView implements Runnable
	 {
		
	    Thread thread = null;
	    SurfaceHolder surfaceHolder;
	    volatile boolean running = false;
	     
	    private Paint paint = new Paint(Paint.ANTI_ALIAS_FLAG);
	    Random random;
	     
	    volatile boolean touched = false;
	    volatile float touched_x, touched_y;
	 
	  public MySurfaceView(Context context) {
	   super(context);
	   // TODO Auto-generated constructor stub
	   surfaceHolder = getHolder();	
	   random = new Random();
	  }
	   
	  public void onResumeMySurfaceView(){
	   running = true;	  
	   thread = new Thread(this);
	   thread.start();
	  }
	   
	  public void onPauseMySurfaceView(){
	   boolean retry = true;
	   running = false;
	   while(retry){
	    try {
	     thread.join();
	     retry = false;
	    } catch (InterruptedException e) {
	     // TODO Auto-generated catch block
	     e.printStackTrace();
	    }
	   }
	  }
	 
	  @Override
	  public void run() {
	   // TODO Auto-generated method stub
	   while(running){
	    if(surfaceHolder.getSurface().isValid()){
	     //此canvas自動更新
	     Canvas canvas = surfaceHolder.lockCanvas();
	     //... actual drawing on canvas
	 
	     paint.setStyle(Paint.Style.STROKE);
	        paint.setStrokeWidth(3);
	      
	     int w = canvas.getWidth();
	     int h = canvas.getHeight();
	     int x = random.nextInt(w-1);
	     int y = random.nextInt(h-1);
	     int r = random.nextInt(255);
	     int g = random.nextInt(255);
	     int b = random.nextInt(255);
	     
	     //此paint畫面設定為閃礫
	     //paint.setColor(0xff000000 + (r << 16) + (g << 8) + b);	     
	     //此paint畫面設定綠色
	     //paint.setColor(Color.GREEN);
	       
         //此canvas繪圖記錄之前的圖形
	     //canvas.drawPoint(x, y, paint);	
	     //此canvas繪圖不記錄之前的圖形 
	     //canvas.drawColor(Color.GREEN);
	   
	     
	     if(touched)
	     {
	      canvas.drawColor(Color.GREEN);
	      paint.setStrokeWidth(10);
	      paint.setColor(Color.BLACK);
	      canvas.drawPoint(touched_x, touched_y, paint);
	     
	     }
	      
	     surfaceHolder.unlockCanvasAndPost(canvas);
	    }
	   }
	  }
	 
	  @Override
	  public boolean onTouchEvent(MotionEvent event) {
	   // TODO Auto-generated method stub
	    
	   touched_x = event.getX();
	   touched_y = event.getY();
	    
	   int action = event.getAction();
	   switch(action){
	   case MotionEvent.ACTION_DOWN:
	    touched = true;
	    break;
	   case MotionEvent.ACTION_MOVE:
	    touched = true;
	    break;
	   case MotionEvent.ACTION_UP:
	    touched = false;
	    break;
	   case MotionEvent.ACTION_CANCEL:
	    touched = false;
	    break;
	   case MotionEvent.ACTION_OUTSIDE:
	    touched = false;
	    break;
	   default:
	   }
	   return true; //processed
	  }
	     
	 }
	  
	
}

 

 

自我LV~