2012-04-09 143 views

回答

0

這裏有一個例子的代碼; d(曲線的F(X)= X^3功能)

繪出的函數的樣品的線程。

public class MainThread extends Thread { 
private static final String TAG = MainThread.class.getSimpleName(); 
private SurfaceHolder surfaceHolder; 
private MainGamePanel gamePanel; 
private Paint paintRed; 
private Paint paintBlue; 
private boolean running; 

public void setRunning(boolean running) { 
    this.running = running; 
} 

public MainThread(SurfaceHolder surfaceHolder, MainGamePanel gamePanel) { 
    super(); 

    this.paintRed = new Paint(); 
    this.paintRed.setColor(Color.RED); 
    this.paintRed.setStrokeWidth(1); 

    this.paintBlue = new Paint(); 
    this.paintBlue.setColor(Color.BLUE); 
    this.paintBlue.setStrokeWidth(1); 

    this.surfaceHolder = surfaceHolder; 
    this.gamePanel = gamePanel; 
} 

class MeasuredAxis { 
    public static final int X_GAP = 1; 
    public static final int Y_GAP = 1; 

    int _realHeight; 
    int _realWidth; 

    public MeasuredAxis(int width, int height) { 
     _realWidth = width; 
     _realHeight = height; 
    } 

    public int getXAxisNegLimit() { 
     return (_realWidth/2)*(-1); 
    } 

    public int getXAxisLimit() { 
     return (_realWidth/2); 
    } 

    public float getXMeasured(int x) { 
     return ((_realWidth/2) + x); 
    } 

    public float getYMeasured(float y) { 
     return ((_realHeight/2) - y); 
    } 
} 


private float function(float x) { 
    return new Double(Math.pow(x, 3)).floatValue(); 
} 

@Override 
public void run() { 
    long fps = 0L; 
    Log.d(TAG, "Starting game loop"); 
    Canvas canvas = surfaceHolder.lockCanvas(); 

    MeasuredAxis measured = new MeasuredAxis(canvas.getWidth(), canvas.getHeight()); 

    synchronized (canvas) { 
     int x = measured.getXAxisNegLimit(); 
     //from -x to +x evaluate and plot the function 
     while (x++ < measured.getXAxisLimit()) { 
      canvas.drawLine(
        measured.getXMeasured(x), 
        measured.getYMeasured(function(x)), 
        measured.getXMeasured(x+MeasuredAxis.X_GAP), 
        measured.getYMeasured(function(x+MeasuredAxis.Y_GAP)), 
        paintRed); 
     } 
     canvas.save(); 
     canvas.translate(0, 0); 
     canvas.scale(canvas.getWidth(), canvas.getHeight()); 
     canvas.restore(); 

    } 
    surfaceHolder.unlockCanvasAndPost(canvas); 

} 
    } 

繪製功能的示例表面視圖。

public class MainGamePanel extends SurfaceView implements 
    SurfaceHolder.Callback { 
private MainThread thread; 

public MainGamePanel(Context context) { 
    super(context); 
    getHolder().addCallback(this); 
    // create the game loop thread 
    thread = new MainThread(getHolder(), this); 
    setFocusable(true); 
} 

@Override 
public void surfaceChanged(SurfaceHolder holder, int format, int width, 
     int height) { 
} 

@Override 
public void surfaceCreated(SurfaceHolder holder) { 
    thread.setRunning(true); 
    thread.start(); 
} 

@Override 
public void surfaceDestroyed(SurfaceHolder holder) { 
    boolean retry = true; 
    while (retry) { 
     try { 
      thread.join(); 
      retry = false; 
     } catch (InterruptedException e) { 
      // try again shutting down the thread 
     } 
    } 
} 

@Override 
public boolean onTouchEvent(MotionEvent event) { 
    return super.onTouchEvent(event); 
} 

@Override 
protected void onDraw(Canvas canvas) { 


} 
    } 

和示例活動

public class MainActivity extends Activity { 

@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    requestWindowFeature(Window.FEATURE_NO_TITLE); 
    getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, 
      WindowManager.LayoutParams.FLAG_FULLSCREEN); 
    setContentView(new MainGamePanel(this)); 
} 
} 

希望這可以幫助你!