gpt4 book ai didi

java - 如何使用 onTouch 事件更新绘制到 Canvas 上的 Android 位图的位置?

转载 作者:行者123 更新时间:2023-12-01 14:53:48 58 4
gpt4 key购买 nike

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

我的 Activity :

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;
}

}

我还需要添加其他内容吗?

最佳答案

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

关于java - 如何使用 onTouch 事件更新绘制到 Canvas 上的 Android 位图的位置?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14512101/

58 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com