0

我一直在开发绘图应用程序,它有以下代码:

public class PainterView extends View implements DrawingListener {

private Context context;
private Painter painter;
private Bitmap background;
private Bitmap bitmap;
private Paint bitmapPaint;
private Path path;
private Paint paint;

private float x;
private float y;

public PainterView(Context context, Painter painter) {

    super(context);
    this.context = context;
    this.painter = painter;
}

@Override
protected void onSizeChanged(int w, int h, int oldw, int oldh) {
    super.onSizeChanged(w, h, oldw, oldh);
}

@Override
protected void onDraw(Canvas canvas) {
    if (bitmap != null) {
        canvas.drawBitmap(background, 0, 0, bitmapPaint);
        canvas.drawBitmap(bitmap, 0, 0, bitmapPaint);
        canvas.drawPath(path, paint);
    }
}

@Override
public boolean onTouchEvent(MotionEvent event) {
    x = event.getX();
    y = event.getY();
    switch (event.getAction()) {
    case MotionEvent.ACTION_DOWN:
        painter.touchStart(x, y);
        break;
    case MotionEvent.ACTION_MOVE:
        painter.touchMove(x, y);
        break;
    case MotionEvent.ACTION_UP:
        painter.touchUp();
        break;
    }
    return true;
}

@Override
public void onPictureUpdate(Bitmap background, Bitmap bitmap, Paint bitmapPaint, Path path, Paint paint) {
    this.background=background;
    this.bitmap = bitmap;
    this.bitmapPaint = bitmapPaint;
    this.path = path;
    this.paint = paint;
    invalidate();
}

public void setPainter(Painter painter) {
    this.painter = painter;
}
}

此代码捕获所有触摸事件,将此事件发送到模型,保存坐标并将位图发送到视图以进行绘图。

我正在使用这段代码,它运行良好,但我的客户告诉我,在某些地方绘图不流畅或缺失。所以我需要使用多线程。所以,请告诉我,我该怎么做?或者我可以使用其他方式提高生产力吗?

4

1 回答 1

0

这是我如何实现您想要的基本框架:

public class Drawing extends Activity implements OnTouchListener{

    DrawPanel dp;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        super.onCreate(savedInstanceState);
        dp = new DrawPanel(this);
        dp.setOnTouchListener(this);
            // do other stuff you want
            setContentView(dp);
    }

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



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

    public class DrawPanel extends SurfaceView implements Runnable{

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

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

        @Override
        public void run() {
            // TODO Auto-generated method stub
            while( isItOk == true){

                if(!holder.getSurface().isValid()){
                    continue;
                }

                Canvas c = holder.lockCanvas();
                c.drawARGB(255, 0, 0, 0);
                onDraw(c);
                holder.unlockCanvasAndPost(c);
            }
        }

        @Override
        protected void onDraw(Canvas canvas) {
            // TODO Auto-generated method stub
            super.onDraw(canvas);
                    // do your drawing stuff here
            }
        }

        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 onTouch(View v, MotionEvent me) {
    // capture your coordinates here         
    return true;

    }


}
于 2012-06-23T17:35:08.063 回答