如何在android中绘制表面视图画布

时间:2012-12-05 12:41:51

标签: android

我的表面视图中存在问题。实际上,当我在表面视图上绘制任何对象时,然后在绘图(onMoving)期间它不显示,但是当我从表面视图释放我的手指然后绘制对象时。所以任何人都可以解决它。

我的代码是...............

  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;
}

1 个答案:

答案 0 :(得分:-1)

试试此代码

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;
}

}