Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/193.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android 用于移动位图的曲面视图和画布_Android_Canvas_Surfaceview - Fatal编程技术网

Android 用于移动位图的曲面视图和画布

Android 用于移动位图的曲面视图和画布,android,canvas,surfaceview,Android,Canvas,Surfaceview,我有一个SurfaceView,我希望画布中的位图徽标可以移动 我做错了什么 static float x, y; Bitmap logo; SurfaceView ss = (SurfaceView) findViewById(R.id.svSS); logo = BitmapFactory.decodeResource(getResources(), R.drawable.logo); x = 40; y = 415; ss.setOnTouchLi

我有一个
SurfaceView
,我希望
画布中的
位图徽标可以移动

我做错了什么

static float x, y;
Bitmap logo;

SurfaceView ss = (SurfaceView) findViewById(R.id.svSS);   
    logo = BitmapFactory.decodeResource(getResources(), R.drawable.logo);

    x = 40;
    y = 415;
    ss.setOnTouchListener(new View.OnTouchListener() {

        @Override
        public boolean onTouch(View v, MotionEvent me) {
            try {
                Thread.sleep(50);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        switch(me.getAction()) {
        case MotionEvent.ACTION_DOWN:
            x = me.getX();
            y = me.getY();
            break;
        case MotionEvent.ACTION_UP:
            x = me.getX();
            y = me.getY();
            break;
        case MotionEvent.ACTION_MOVE:
            x = me.getX();
            y = me.getY();
            break;
            }
        return true;
        }
    });

public class OurView extends SurfaceView implements Runnable{

    Thread t = null;
    SurfaceHolder holder;
    boolean isItOK = false;


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

    }
    public void run (){
        while (isItOK == true){
            //canvas DRAWING
            if (!holder.getSurface().isValid()){
                continue;
            }
            Canvas c = holder.lockCanvas();
            c.drawARGB(255, 200, 100, 100);
            c.drawBitmap(logo, x,y,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();

    }
}

现在,曲面视图仅为黑色。。如果未着色,则不会发生任何事情。您可能会忘记在
OurView
类中实现
onDraw(Canvas c)
方法,并将
onTouchEvent
移动到类中

类结构应如下所示:

public class OurView extends View implements Runnable {

//...your runnable stuff here
//...Runnable stuff means your run(), pause() etc.

  public OurView(Context context) {
    super(context);
    //your constructor stuff here
    // your constructor, do you find the similar stuff to this you wrote? That's your constructor, so you can just add:
    holder = getHolder();
}

protected void onDraw (Canvas c) {

    c.drawBitmap(bitmap, x, y);
//set colour, draw bitmap here, onDraw() will be called automatically, so just call invalidate(); when you need to "refresh" the view
}

public boolean onTouchEvent(MotionEvent e) {
    float x = e.getX();
    float y = e.getY();

    swith(e.getAction()) {
    case MotionEvent.ACTION_DOWN:
     ...
    }
}
}
有关更明确的示例和参考,请访问:


希望它能对您有所帮助。

您需要在
OurView
类中实现
onDraw(Canvas c)
方法。谢谢,我是个新手。。你说的构造函数和可运行的东西是什么意思?抱歉@johnaplesim请检查我的改进答案我尝试了,但没有成功,(最后的代码)。。顺便说一句,我想要的是小部件表面视图(来自xml),而不是整个屏幕是表面视图