有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!


共 (1) 个答案

  1. # 1 楼答案

    这里有一个示例代码;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));
    }
    }
    

    希望这对你有帮助