简体   繁体   中英

How to drawing on surface view canvas in android

I have aa problem in my surface view . Actually when i draw any object on surface view then during drawing(onMoving) it's not show, but when i release my finger from surface view then drawing object seen. So any one solve it plz.

My code is...............

  public boolean onTouch(View view, MotionEvent motionEvent) {
    if(motionEvent.getAction() == MotionEvent.ACTION_DOWN){

        currentDrawingPath = new DrawingPath();
        currentDrawingPath.paint = currentPaint;
        currentDrawingPath.path = new Path();
        currentBrush.mouseDown(currentDrawingPath.path, motionEvent.getX(), motionEvent.getY());    

    }else if(motionEvent.getAction() == MotionEvent.ACTION_MOVE){

        currentBrush.mouseMove( currentDrawingPath.path, motionEvent.getX(), motionEvent.getY() );      

    }else if(motionEvent.getAction() == MotionEvent.ACTION_UP){
        currentBrush.mouseUp( currentDrawingPath.path, motionEvent.getX(), motionEvent.getY() );            
        drawingSurface.addDrawingPath(currentDrawingPath);


    }

    return true;
}

Try this code

Ourview v;
Bitmap ball;
float x,y;

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    v = new Ourview(this);
    v.setOnTouchListener(this);
    ball = BitmapFactory.decodeResource(getResources(), R.drawable.circle_green);
    x = y = 0;
    setContentView(v);
}


@Override
protected void onPause() {
    // TODO Auto-generated method stub
    super.onPause();
    v.pause();
}

@Override
protected void onResume() {
    // TODO Auto-generated method stub
    super.onResume();
    v.resume();
}

public class Ourview extends SurfaceView implements Runnable{

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

    public Ourview(Context context) {
        super(context);
        // TODO Auto-generated constructor stub
        holder = getHolder();
    }

    public void run() {
        // TODO Auto-generated method stub
        while (isItOk == true){
            //perfom convas drawing
            if (!holder.getSurface().isValid()){
                continue;
            }
            Canvas c = holder.lockCanvas();
            c.drawARGB(250, 10, 50, 100);
            c.drawBitmap(ball, x - (ball.getWidth()/2), y - (ball.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();

    }

}

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

    try {
        Thread.sleep(50);
    } catch (InterruptedException e) {
        // TODO Auto-generated catch block
        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;
}

}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM