Code Monkey home page Code Monkey logo

Comments (4)

Edward259 avatar Edward259 commented on August 29, 2024

绘制部分看着没问题,可以确认一下可手写区域是否正确以及onRawDrawingTouchPointListReceived回调数据是否正常。多区域手写可以参考ScribbleMultipleScribbleViewActivity

from onyxandroiddemo.

hanxiao2018 avatar hanxiao2018 commented on August 29, 2024

你好,我直接在ScribbleMultipleScribbleViewActivity中写了个简单方法,来保存bitmap,但是保存的bitmap依然是空背景。onRawDrawingTouchPointListReceived的是有值的。具体代码如下(在源文件中增加了drawScribbleToBitmap方法):
public class ScribbleMultipleScribbleViewActivity extends AppCompatActivity {

private static final String TAG = ScribbleMultipleScribbleViewActivity.class.getSimpleName();

@Bind(R.id.button_pen)
Button buttonPen;
@Bind(R.id.button_eraser)
Button buttonEraser;
@Bind(R.id.content)
View content;
@Bind(R.id.surfaceview1)
SurfaceView surfaceView1;
@Bind(R.id.surfaceview2)
SurfaceView surfaceView2;

private TouchHelper touchHelper;

private RawInputCallback rawInputCallback;
private List<Rect> limitRectList = new ArrayList<>();

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_scribble_multiple_scrubble_view_demo);

    ButterKnife.bind(this);
    touchHelper = TouchHelper.create(getWindow().getDecorView().getRootView(), getRawInputCallback());
    initSurfaceView(surfaceView1);
    initSurfaceView(surfaceView2);
}

@Override
protected void onResume() {
    touchHelper.setRawDrawingEnabled(true);
    super.onResume();
}

@Override
protected void onPause() {
    touchHelper.setRawDrawingEnabled(false);
    super.onPause();
}

@Override
protected void onDestroy() {
    touchHelper.closeRawDrawing();
    super.onDestroy();
}

private void initSurfaceView(final SurfaceView surfaceView) {
    final SurfaceHolder.Callback surfaceCallback = new SurfaceHolder.Callback() {
        @Override
        public void surfaceCreated(SurfaceHolder holder) {
            cleanSurfaceView(surfaceView);
            Rect limit = new Rect();
            surfaceView.getGlobalVisibleRect(limit);
            limitRectList.add(limit);
            onSurfaceCreated(limitRectList);
        }

        @Override
        public void surfaceChanged(SurfaceHolder holder, int format, int width, int height) {
        }

        @Override
        public void surfaceDestroyed(SurfaceHolder holder) {
            holder.removeCallback(this);
        }
    };
    surfaceView.getHolder().addCallback(surfaceCallback);
}

private void onSurfaceCreated(List<Rect> limitRectList) {
    if (limitRectList.size() < 2) {
        return;
    }
    touchHelper.setLimitRect(limitRectList, new ArrayList<Rect>())
            .openRawDrawing();
}

public RawInputCallback getRawInputCallback() {
    if (rawInputCallback == null) {
        rawInputCallback = new RawInputCallback() {
            @Override
            public void onBeginRawDrawing(boolean b, TouchPoint touchPoint) {

            }

            @Override
            public void onEndRawDrawing(boolean b, TouchPoint touchPoint) {

            }

            @Override
            public void onRawDrawingTouchPointMoveReceived(TouchPoint touchPoint) {

            }

            @Override
            public void onRawDrawingTouchPointListReceived(TouchPointList touchPointList) {
                drawScribbleToBitmap(touchPointList.getPoints());
            }

            @Override
            public void onBeginRawErasing(boolean b, TouchPoint touchPoint) {
            }

            @Override
            public void onEndRawErasing(boolean b, TouchPoint touchPoint) {
            }

            @Override
            public void onRawErasingTouchPointMoveReceived(TouchPoint touchPoint) {
            }

            @Override
            public void onRawErasingTouchPointListReceived(TouchPointList touchPointList) {
            }
        };
    }
    return rawInputCallback;
}


@OnClick(R.id.button_pen)
public void onPenClick(){
    touchHelper.setRawDrawingEnabled(true);
}

@OnClick(R.id.button_eraser)
public void onEraserClick(){
    touchHelper.setRawDrawingEnabled(false);
    cleanAllSurfaceView();
    touchHelper.setRawDrawingEnabled(true);
}

@OnClick(R.id.button_single_region_mode)
public void onSingleRegionModeClick() {
    touchHelper.setRawDrawingEnabled(false);
    cleanAllSurfaceView();
    touchHelper.setSingleRegionMode();
    touchHelper.setRawDrawingEnabled(true);
}

@OnClick(R.id.button_multi_region_mode)
public void onMultiRegionModeClick() {
    touchHelper.setRawDrawingEnabled(false);
    cleanAllSurfaceView();
    touchHelper.setMultiRegionMode();
    touchHelper.setRawDrawingEnabled(true);
}

private void cleanAllSurfaceView() {
    cleanSurfaceView(surfaceView1);
    cleanSurfaceView(surfaceView2);
}

private void cleanSurfaceView(SurfaceView surfaceView) {
    if (surfaceView.getHolder() == null) {
        return;
    }
    Canvas canvas = surfaceView.getHolder().lockCanvas();
    if (canvas == null) {
        return;
    }
    canvas.drawColor(Color.WHITE);
    surfaceView.getHolder().unlockCanvasAndPost(canvas);
}

Bitmap resultBitmap;
Canvas canvas;
private void drawScribbleToBitmap(List<TouchPoint> list) {
    SurfaceView surfaceView = surfaceView1;
    if (resultBitmap == null) {
        resultBitmap = Bitmap.createBitmap(surfaceView.getWidth(), surfaceView.getHeight(), Bitmap.Config.ARGB_8888);
        canvas = new Canvas(resultBitmap);
    } else {
        resultBitmap = Bitmap.createBitmap(resultBitmap);
        canvas.setBitmap(resultBitmap);
    }

    Path path = new Path();
    PointF prePoint = new PointF(list.get(0).x, list.get(0).y);
    path.moveTo(prePoint.x, prePoint.y);
    for (TouchPoint point : list) {
        System.out.println(point.x + "===================" + point.y);
        path.quadTo(prePoint.x, prePoint.y, point.x, point.y);
        prePoint.x = point.x;
        prePoint.y = point.y;
    }
    Paint paint = new Paint();
    paint.setAntiAlias(true);
    paint.setStyle(Paint.Style.STROKE);
    paint.setColor(Color.BLACK);
    paint.setStrokeWidth(3.0f);

    canvas.drawPath(path, paint);
    BitmapUtils.saveBitmap(resultBitmap, "/sdcard/Android/data/com.onyx.android.demo/test.png");
}

}

from onyxandroiddemo.

onyx-chenhj avatar onyx-chenhj commented on August 29, 2024

昨天已在群里回复了,是点坐标不在bitmap(widthxheight)之内。
点坐标是相对应绑定的view的,这个demo里绑定的是getWindow().getDecorView().getRootView(),点坐标在bitmap区域之外。

from onyxandroiddemo.

hanxiao2018 avatar hanxiao2018 commented on August 29, 2024

好的

from onyxandroiddemo.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.