我有一个" 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();
}
}