0

我正在创建一个绘画应用程序,但我无法在图像上绘制/绘画任何东西,请帮助我:

public class DrawingSurface extends SurfaceView implements SurfaceHolder.Callback {
    private Boolean _run;
    protected DrawThread thread;
    private Bitmap mBitmap;
    public boolean isDrawing = true;
    public DrawingPath previewPath;

    private CommandManager commandManager;

    public DrawingSurface(Context context, AttributeSet attrs) {
        super(context, attrs);

        getHolder().addCallback(this);


        commandManager = new CommandManager();
        thread = new DrawThread(getHolder());
    }

    private Handler previewDoneHandler = new Handler(){
        @Override
        public void handleMessage(Message msg) {
            isDrawing = false;
        }
    };

    class DrawThread extends  Thread{
        private SurfaceHolder mSurfaceHolder;


        public DrawThread(SurfaceHolder surfaceHolder){
            mSurfaceHolder = surfaceHolder;

        }

        public void setRunning(boolean run) {
            _run = run;
        }


        @Override
        public void run() {
            Canvas canvas = null;
            while (_run){
                if(isDrawing == true){
                    try{
                        canvas = mSurfaceHolder.lockCanvas(null);
                        if(mBitmap == null){
                          //  mBitmap =  Bitmap.createBitmap (1, 1, Bitmap.Config.ARGB_8888);
                             mBitmap=BitmapFactory.decodeResource(getResources(),R.drawable.splashscreen);

                        }
                //       final Canvas c = new Canvas (mBitmap);
                        mBitmap=BitmapFactory.decodeResource(getResources(),R.drawable.splashscreen);

                   //     c.drawColor(0, PorterDuff.Mode.CLEAR);
                       canvas.drawColor(0, PorterDuff.Mode.CLEAR);


                     //   commandManager.executeAll(c,previewDoneHandler);
                     //   previewPath.draw(c);

                        canvas.drawBitmap (mBitmap, 0,  0,null);
                    } finally {
                        mSurfaceHolder.unlockCanvasAndPost(canvas);
                    }


                }

            }

        }
    }


    public void addDrawingPath (DrawingPath drawingPath){
        commandManager.addCommand(drawingPath);
    }
   // Bitmap mBitmap = Bitmap.createScaledBitmap(Bitmap src, int dstWidth, int dstHeight, boolean filter);




    public boolean hasMoreRedo(){
        return commandManager.hasMoreRedo();
    }

    public void redo(){
        isDrawing = true;
        commandManager.redo();


    }

    public void undo(){
        isDrawing = true;
        commandManager.undo();
    }

    public boolean hasMoreUndo(){
        return commandManager.hasMoreUndo();
    }

    public Bitmap getBitmap(){
        return mBitmap;
    }


    public void surfaceChanged(SurfaceHolder holder, int format, int width,  int height) {
        // TODO Auto-generated method stub
        mBitmap =  Bitmap.createBitmap (width, height, Bitmap.Config.ARGB_8888);;
    }


    public void surfaceCreated(SurfaceHolder holder) {
        // TODO Auto-generated method stub
        thread.setRunning(true);
        thread.start();
    }

    public void surfaceDestroyed(SurfaceHolder holder) {
        // TODO Auto-generated method stub
        boolean retry = true;
        thread.setRunning(false);
        while (retry) {
            try {
                thread.join();
                retry = false;
            } catch (InterruptedException e) {
                // we will try it again and again...
            }
        }
    }

}
4

1 回答 1

0

为什么不让自己更轻松,将您的应用程序拆分为多个表面视图。使用 framelayout 以便您可以控制视图的顺序。我假设您希望能够处理触摸事件(它是一个绘画程序,对吗?),因此在一个视图中处理这些事件并在另一个视图中处理您的绘图,这样代码将更易于管理。创建两个自定义表面视图(在我的例子中,一个是 GLSurfaceView,另一个是 SurfaceView)。一个视图实现图像,第二个视图可以处理带有图形和/或按钮的 GUI。关键是在 XML 中创建一个自定义 GLSurfaceView(或者在您的情况下是两个 Surfaceview),它使用扩展的 SurfaceView 的类名。此外,您必须使 GUI 视图透明。因此,调用绘图类并处理触摸事件的 Surface 视图将具有如下代码:

public class MainSurfaceView extends GLSurfaceView
//Constructor
public MainSurfaceView(Context context, AttributeSet attrs) {       
    super(context, attrs);
    Log.d(TAG, "Constructor called...");
    this.context = context;

    mRenderer = new MainRenderer();
    setEGLConfigChooser(8, 8, 8, 8, 16, 0); 

    setRenderer(mRenderer);
    getHolder().setFormat(PixelFormat.TRANSLUCENT);    

    setZOrderOnTop(true);

    //Render only when there is a change
    setRenderMode(GLSurfaceView.RENDERMODE_WHEN_DIRTY);         
}

//TouchEvent handler
@Override
public boolean onTouchEvent(MotionEvent event) {
    Log.d(TAG, "onTouchEvent called...");
    x = event.getX();
    y = event.getY();

    switch (event.getAction()) {
    case MotionEvent.ACTION_MOVE:

        float dx = x - mPreviousX;
        float dy = y - mPreviousY;

        //Reverse direction of rotation if above midline
        if (y > getHeight() / 2) {
            dx = dx * -1;
        }

        //Reverse direction of rotation if of the midline
        if (y < getWidth() / 2) {
            dy = dy * -1;
        }

        Main.mAngle += (dx + dy) * TOUCH_SCALE_FACTOR;

        TextView txtView = (TextView) ((Activity)context).findViewById(R.id.mangle);
        txtView.setText("Angle: " + String.valueOf(Main.mAngle));

        requestRender();
    }

    mPreviousX = x;
    mPreviousY = y;

    return true;
}

将您要绘制的图像放在单独的表面视图上(这也将使更改图像更容易)。所以你的 Activity 类会有这样的代码(注意我的第二个视图是 GLsurfaceview):

 public class Main extends Activity {
 @Override
public void onCreate(Bundle savedInstanceState) {
    Log.d(TAG, "onCreate called...");
    Log.d(TAG, "state is: " + state.toString());
    super.onCreate(savedInstanceState);

    // When working with the camera, it's useful to stick to one orientation.
    setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);

    //Set full screen
    requestWindowFeature(Window.FEATURE_NO_TITLE);
    getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);

    setContentView(R.layout.main);

    //Substantiate surfaces
    cameraView = (CameraView) findViewById(R.id.camera);
    mGLView = (MainSurfaceView) findViewById(R.id.glSurfaceView);        

    //Initialize TextViews
    txtView = (TextView) findViewById(R.id.mangle);
    txtView.setText("Angle: " + String.valueOf(mAngle)); 


    }
}

希望对您有所帮助...

于 2012-06-28T11:25:00.060 回答