0

我正在尝试使用 onTouch 事件更新位图的位置。图像正确显示在初始位置。日志显示 onTouch 事件正在更新坐标,还显示正在调用 onDraw。

我的活动:

package com.miker.haminvaders;

import android.app.Activity;
import android.os.Bundle;

public class HamInvadersActivity extends Activity
{
    private DrawView drawView;
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState)
    {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);

    drawView = (DrawView) findViewById(R.id.drawView);      

    }
    @Override
    public void onPause()
    {
    super.onPause();
    drawView.stopGame();

    }
    @Override
    protected void onDestroy()
    {
    super.onDestroy();
    drawView.releaseResources();
    }
}

我的线程类:

package com.miker.haminvaders;

import android.graphics.Canvas;
import android.util.Log;
import android.view.SurfaceHolder;


public class MainThread extends Thread
{
    private boolean running;
    SurfaceHolder surfaceHolder;
    DrawView drawView;
    public void setRunning(boolean r)
    {
        this.running = r;
    }
    @Override
    public void run()
    {
        Canvas canvas = null;
        while(running)
        {
            //Log.v("HAM", "tick!");
            try
            {
                canvas = surfaceHolder.lockCanvas();
                synchronized(surfaceHolder)
                {
                    drawView.onDraw(canvas);
                    drawView.updatePositions();

                }
            }
            finally
            {
                if(canvas != null)
                    surfaceHolder.unlockCanvasAndPost(canvas);
            } 
        }
    }
    public MainThread(SurfaceHolder surface, DrawView draw)
    {
        super();
        surfaceHolder = surface;
        drawView = draw;
    }

}

我的 SurfaceView 类:

package com.miker.haminvaders;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.view.SurfaceHolder;
import android.view.SurfaceView;
import android.graphics.BitmapFactory;
import android.util.Log;


public class DrawView extends SurfaceView implements SurfaceHolder.Callback
{
    private MainThread myThread;

    private int screenWidth;
    private int screenHeight;

    private Paint textPaint;
    private Paint backPaint;

    private float x, y;
    private Bitmap bmpHam;


    public DrawView(Context context, AttributeSet attrs)
    {
        super(context, attrs);

        SurfaceHolder holder = getHolder();
        holder.addCallback(this);

        textPaint = new Paint();
        backPaint = new Paint();

        myThread = new MainThread(holder, this);
        bmpHam = BitmapFactory.decodeResource(getResources(), R.drawable.lilhamstrich);


    }
    @Override
    protected void onSizeChanged( int w, int h, int oldw, int oldh)
    {
        super.onSizeChanged(w, h, oldw, oldh);
        screenWidth = w;
        screenHeight = h;

        textPaint.setTextSize(w/20);
        backPaint.setColor(Color.WHITE);

        newGame();
    }
    public void newGame()
    {
        x = screenWidth/2;
        y = screenHeight/2;
    }
    public void updatePositions()
    {
    }
    public void onDraw(Canvas canvas)
    {
            canvas.drawColor(Color.WHITE);
            canvas.drawBitmap(bmpHam, x, y, null);
            Log.v("HAM", "Draw! " + x + " " + y);
    }
    public void stopGame()
    {
        if(myThread != null)
            myThread.setRunning(false);
    }
    public void releaseResources()
    {
        //soundPool stuff
    }
    @Override
    public void surfaceChanged(SurfaceHolder holder, int format, int width, int height)
    {

    }
    @Override
    public void surfaceCreated(SurfaceHolder holder)
    {
        myThread.setRunning(true);
        myThread.start();
    }
    @Override
    public void surfaceDestroyed(SurfaceHolder holder)
    {
        boolean retry = true;
        myThread.setRunning(false);
        while(retry)
        {
            try
            {
                myThread.join();
                retry = false;
            }
            catch(InterruptedException e)
            {

            }
        }
    }
    @Override
    public boolean onTouchEvent (MotionEvent event)
    {
    int action = event.getAction();
    if( action == MotionEvent.ACTION_DOWN)
    {

        x = event.getX();
        y = event.getY();
        Log.v("HAM","Down! " + x + " " + y);
    }
    return true;
    }

}

还有什么我需要包括的吗?

4

1 回答 1

1

调用invalidate()onTouchEvent()它应该会导致重绘。

于 2013-01-25T21:35:53.157 回答