我有一个“Board.java”类,它是一个活动,还有一个“MySurfaceVIEw.java”,它是一个SurfaceVIEw.我正在SurfaceVIEw上绘制一些位图,我想在触摸屏幕时移动它(到触摸的位置).它绘制位图但触摸后没有任何反应.
提前致谢!
日志:
Cancelling event due to no window focus: MotionEvent { action=ACTION_CANCEL, actionbutton=0, ID[0]=0, x[0]=539.47266, y[0]=978.45703, toolType[0]=TOol_TYPE_FINGER, buttonState=0, MetaState=0, flags=0x0, edgeFlags=0x0, pointerCount=1, historySize=0, eventTime=90656, downTime=88761, deviceid=0, source=0x1002 }
Board.java:
package com.myfirstapplication.owner.appversion1;import androID.graphics.Bitmap;import androID.graphics.BitmapFactory;import androID.graphics.Canvas;import androID.graphics.color;import androID.os.Bundle;import androID.support.v7.app.AppCompatActivity;import androID.vIEw.MotionEvent;import androID.vIEw.VIEw;import androID.Widget.ImageVIEw;import androID.Widget.relativeLayout;import androID.Widget.TextVIEw;/** * Created by Owner on 15/06/2016. */public class Board extends AppCompatActivity implements VIEw.OntouchListener { TextVIEw tv; MySurfaceVIEw mv; Bitmap bp; float x, y; protected voID onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); mv = new MySurfaceVIEw(this); mv.setontouchListener(this); bp = BitmapFactory.decodeResource(getResources(), R.drawable.missile_cartoon); x = 0; y = 0; setContentVIEw(mv); } @OverrIDe protected voID onPause() { super.onPause(); mv.pause(); } @OverrIDe protected voID onResume() { super.onResume(); mv.resume(); } @OverrIDe public boolean ontouch(VIEw v, MotionEvent event) { try { Thread.sleep(50); } catch (InterruptedException e) { e.printstacktrace(); } x = event.getX(); y = event.getY(); return true; } }
MySurfaceVIEw.java:
package com.myfirstapplication.owner.appversion1;import androID.content.Context;import androID.graphics.Bitmap;import androID.graphics.BitmapFactory;import androID.graphics.Canvas;import androID.graphics.color;import androID.graphics.Paint;import androID.graphics.Path;import androID.graphics.Rect;import androID.util.AttributeSet;import androID.vIEw.MotionEvent;import androID.vIEw.SurfaceHolder;import androID.vIEw.SurfaceVIEw;import androID.vIEw.VIEw;import androID.Widget.ImageVIEw;/** * Created by Owner on 16/06/2016. */public class MySurfaceVIEw extends SurfaceVIEw implements Runnable { float x, y; Bitmap bp; Thread t = null; SurfaceHolder holder; boolean isItOK = false; public MySurfaceVIEw(Context context) { super(context); Bitmap bpold = BitmapFactory.decodeResource(getResources(), R.drawable.missile_cartoon); bp = Bitmap.createScaledBitmap(bpold, 250, 250, true); x = 0; y = 0; holder = getHolder(); } @OverrIDe public voID run() { while(isItOK){ if(!holder.getSurface().isValID()) continue; Canvas c = holder.lockCanvas(); c.drawARGB(255,150,150,10); c.drawBitmap(bp,x-(bp.getWIDth()/2),y-(bp.getHeight()/2),null); holder.unlockCanvasAndPost(c); } } public voID pause() { isItOK = false; while(true){ try{ t.join(); } catch (InterruptedException e){ e.printstacktrace(); } break; } t = null; } public voID resume() { isItOK = true; t = new Thread(this); t.start(); }}
解决方法:
在Board中删除touchListener
板:
public class Board extends AppCompatActivity{MySurfaceVIEw mv;Bitmap bp;protected voID onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); mv = new MySurfaceVIEw(this); bp = BitmapFactory.decodeResource(getResources(), R.mipmap.ic_launcher); setContentVIEw(mv);}@OverrIDeprotected voID onPause() { super.onPause(); mv.pause();}@OverrIDeprotected voID onResume() { super.onResume(); mv.resume();}}
并在MySurfaceVIEw中
public class MySurfaceVIEw extends SurfaceVIEw implements Runnable {float x, y;Bitmap bp;Thread t = null;SurfaceHolder holder;boolean isItOK = false;public MySurfaceVIEw(Context context) { super(context); Bitmap bpold = BitmapFactory.decodeResource(getResources(), R.mipmap.ic_launcher); bp = Bitmap.createScaledBitmap(bpold, 250, 250, true); x = 0; y = 0; holder = getHolder();}@OverrIDepublic voID run() { while(isItOK){ if(!holder.getSurface().isValID()) continue; Canvas c = holder.lockCanvas(); c.drawARGB(255,150,150,10); c.drawBitmap(bp,x-(bp.getWIDth()/2),y-(bp.getHeight()/2),null); holder.unlockCanvasAndPost(c); }}public voID pause() { isItOK = false; while(true){ try{ t.join(); } catch (InterruptedException e){ e.printstacktrace(); } break; } t = null;}public voID resume() { isItOK = true; t = new Thread(this); t.start();}@OverrIDepublic boolean ontouchEvent(MotionEvent event) { x = event.getX(); y = event.getY(); // run(); return super.ontouchEvent(event);}}
以上工作对我来说很好.
总结以上是内存溢出为你收集整理的java – OnTouch Listener不会更改位图位置全部内容,希望文章能够帮你解决java – OnTouch Listener不会更改位图位置所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)