gpt4 book ai didi

Android - 在触摸事件时在屏幕上绘制位图

转载 作者:行者123 更新时间:2023-11-29 21:40:30 26 4
gpt4 key购买 nike

我是 android 开发的新手。每当用户触摸屏幕时,我都会尝试绘制位图图像。我设法检索了屏幕上发生的触摸事件的 x 和 y 位置。但是我不明白为什么下面提到的程序不会在 Canvas 上绘制图像。

这是我正在处理的代码:

public class SurfaceViewTest extends Activity implements OnTouchListener {      
FastRenderView renderView;
Bitmap bob565;
Bitmap bob4444;
float x,y;
Canvas canvas = new Canvas();

public void onCreate(Bundle savedInstanceState) {


super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
WindowManager.LayoutParams.FLAG_FULLSCREEN);
renderView = new FastRenderView(this);
setContentView(renderView);
}


protected void onResume() {
super.onResume();
renderView.resume();
}

protected void onPause() {
super.onPause();
renderView.pause();
}


public boolean onTouch(View v, MotionEvent event) {
// TODO Auto-generated method stub
;
switch (event.getAction()) {

case MotionEvent.ACTION_DOWN:
x = event.getX();
y = event.getY()
canvas.drawBitmap(bob4444, x, y, null);
break;

}
return true;
}




class FastRenderView extends SurfaceView implements Runnable {
Thread renderThread = null;
SurfaceHolder holder;
volatile boolean running = false;

public FastRenderView(Context context) {
super(context);
holder = getHolder();

try {
AssetManager assetManager = context.getAssets();
InputStream inputStream = assetManager.open("bobrgb888.png");
bob565 = BitmapFactory.decodeStream(inputStream);
inputStream.close();
Log.d("BitmapText",
"bobrgb888.png format: " + bob565.getConfig());

inputStream = assetManager.open("bobargb8888.png");
BitmapFactory.Options options = new BitmapFactory.Options();
options.inPreferredConfig = Bitmap.Config.ARGB_4444;
bob4444 = BitmapFactory.decodeStream(inputStream, null, options);

inputStream.close();
Log.d("BitmapText",
"bobargb8888.png format: " + bob4444.getConfig());

} catch (IOException e) {

} finally {
// we should really close our input streams here.
}
}


public void resume() {
running = true;
renderThread = new Thread(this);
renderThread.start();
}

public void run() {
while(running) {
if(!holder.getSurface().isValid())
continue;

Canvas canvas = holder.lockCanvas();
canvas.drawRGB(0, 0, 255);
canvas.drawBitmap(bob4444, x, y, null);
holder.unlockCanvasAndPost(canvas);



}
}

public void pause() {
running = false;
while(true) {
try {
renderThread.join();
return;
} catch (InterruptedException e) {
// retry
}
}


}
}
}

最佳答案

没关系。我自己想出来了。我错过了为 View 设置一个触摸监听器。这是进行更改后的最终代码。

public class SurfaceViewTest extends Activity implements OnTouchListener{      
FastRenderView renderView;
float x, y;
Bitmap bob565, bob4444;

public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
WindowManager.LayoutParams.FLAG_FULLSCREEN);
renderView = new FastRenderView(this);
renderView.setOnTouchListener(this);
setContentView(renderView);
}



protected void onResume() {
super.onResume();
renderView.resume();
}

protected void onPause() {
super.onPause();
renderView.pause();
}

class FastRenderView extends SurfaceView implements Runnable {
Thread renderThread = null;
SurfaceHolder holder;
volatile boolean running = false;

public FastRenderView(Context context) {
super(context);
holder = getHolder();


try{
AssetManager assetManager = context.getAssets();
InputStream inputStream = assetManager.open("bobrgb888.png");
bob565 = BitmapFactory.decodeStream(inputStream);
inputStream.close();
Log.d("BitmapText",
"bobrgb888.png format: " + bob565.getConfig());

inputStream = assetManager.open("bobargb8888.png");
BitmapFactory.Options options = new BitmapFactory.Options();
options.inPreferredConfig = Bitmap.Config.ARGB_4444;
bob4444 = BitmapFactory
.decodeStream(inputStream, null, options);
inputStream.close();
Log.d("BitmapText",
"bobargb8888.png format: " + bob4444.getConfig());

}
catch (IOException e) {
// silently ignored, bad coder monkey, baaad!
} finally {
// we should really close our input streams here.
}
}

public void resume() {
running = true;
renderThread = new Thread(this);
renderThread.start();
}

public void run() {
while(running) {
if(!holder.getSurface().isValid())
continue;

Canvas canvas = holder.lockCanvas();
canvas.drawRGB(0, 0, 255);

canvas.drawBitmap(bob4444, x-bob4444.getWidth()/2, y-bob4444.getHeight()/2, null);
holder.unlockCanvasAndPost(canvas);
}
}

public void pause() {
running = false;
while(true) {
try {
renderThread.join();
return;
} catch (InterruptedException e) {
// retry
}
}
}
}

public boolean onTouch(View v, MotionEvent event) {
// TODO Auto-generated method stub

switch (event.getAction()) {


case MotionEvent.ACTION_UP:
x = event.getX();
y = event.getY();

break;
}


return true;
}

关于Android - 在触摸事件时在屏幕上绘制位图,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17257801/

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