【问题标题】:OnTouch Listener doesn't change bitmap locationOnTouch 侦听器不会更改位图位置
【发布时间】:2016-06-17 12:39:03
【问题描述】:

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



}

【问题讨论】:

    标签: java android bitmap


    【解决方案1】:

    移除 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);
    
    }
    
    @Override
    protected void onPause() {
        super.onPause();
        mv.pause();
    }
    
    @Override
    protected 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();
    }
    
    
    @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();
    }
    
    @Override
    public boolean onTouchEvent(MotionEvent event) {
        x = event.getX();
        y = event.getY();
      //  run();
        return super.onTouchEvent(event);
    }
    }
    

    以上对我来说很好。

    【讨论】:

    • 不工作 :( 崩溃了。日志:java.lang.RuntimeException:无法启动活动 ComponentInfo{com.myfirstapplication.owner.appversion1/com.myfirstapplication.owner.appversion1.Board}:java。 lang.ClassCastException: com.myfirstapplication。有什么帮助吗?谢谢!
    猜你喜欢
    • 1970-01-01
    • 2020-08-27
    • 1970-01-01
    • 2013-05-02
    • 1970-01-01
    • 2011-08-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多