自定義view簽名板

寫文章先上圖 字體隨便畫

qianming.jpg
  /**
     * 自定義簽名控件
     */
    class SignatureView extends View {

        //畫筆
        private Paint paint;

        //畫布
        private Canvas cacheCanvas;

        //位圖
        private Bitmap cachebBitmap;

        //圖片保存路徑
        private Path path;

        //位圖緩存
        public Bitmap getCachebBitmap() {
            return cachebBitmap;
        }

        public SignatureView(Context context) {
            super(context);
            init();
        }

        /**
         * 初始化
         */
        private void init() {
            //設置畫筆
            paint = new Paint();
            paint.setAntiAlias(true);
            paint.setStrokeWidth(20);
            paint.setStyle(Paint.Style.STROKE);
            paint.setColor(Color.BLACK);
            path = new Path();

            //創(chuàng)建位圖
            cachebBitmap = Bitmap.createBitmap(10, 10, Bitmap.Config.ARGB_8888);

            //用自定義位圖構建畫布
            cacheCanvas = new Canvas(cachebBitmap);
            //設置畫布為白色
            cacheCanvas.drawColor(Color.WHITE);
        }

        /**
         * 清除畫板,重置畫筆
         */
        public void clear() {
            if (cacheCanvas != null) {
                paint.setColor(Color.WHITE);
                cacheCanvas.drawPaint(paint);
                paint.setColor(Color.BLACK);
                cacheCanvas.drawColor(Color.WHITE);
                invalidate();
            }
        }

        @Override
        protected void onDraw(Canvas canvas) {
            canvas.drawBitmap(cachebBitmap, 0, 0, null);
            canvas.drawPath(path, paint);
        }

        @Override
        protected void onSizeChanged(int w, int h, int oldw, int oldh) {

            int curW = cachebBitmap != null ? cachebBitmap.getWidth() : 0;
            int curH = cachebBitmap != null ? cachebBitmap.getHeight() : 0;
            if (curW >= w && curH >= h) {
                return;
            }

            if (curW < w) curW = w;
            if (curH < h) curH = h;
            Bitmap newBitmap = Bitmap.createBitmap(curW, curH, Bitmap.Config.ARGB_8888);
            Canvas newCanvas = new Canvas();
            newCanvas.setBitmap(newBitmap);
            if (cachebBitmap != null) {
                newCanvas.drawBitmap(cachebBitmap, 0, 0, null);
            }
            cachebBitmap = newBitmap;
            cacheCanvas = newCanvas;
        }

        private float cur_x, cur_y;

        @Override
        public boolean onTouchEvent(MotionEvent event) {
            float x = event.getX();
            float y = event.getY();
            switch (event.getAction()) {
                case MotionEvent.ACTION_DOWN: {
                    cur_x = x;
                    cur_y = y;
                    path.moveTo(cur_x, cur_y);
                    break;
                }
                case MotionEvent.ACTION_MOVE: {
                    path.quadTo(cur_x, cur_y, x, y);
                    cur_x = x;
                    cur_y = y;
                    break;
                }
                case MotionEvent.ACTION_UP: {
                    cacheCanvas.drawPath(path, paint);
                    path.reset();
                    break;
                }
            }
            invalidate();
            return true;
        }

    }


在xml里創(chuàng)建

        <FrameLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:id="@+id/fl_view"
        android:background="@color/white"
        ></FrameLayout>

        SignatureView mView = new SignatureView(this);
        frameLayout.addView(mView);
        mView.requestFocus();

保存bitmap  直接調用
         Bitmap bitmap = mView.getCachebBitmap();

結束!?。。。?!

?著作權歸作者所有,轉載或內容合作請聯(lián)系作者
【社區(qū)內容提示】社區(qū)部分內容疑似由AI輔助生成,瀏覽時請結合常識與多方信息審慎甄別。
平臺聲明:文章內容(如有圖片或視頻亦包括在內)由作者上傳并發(fā)布,文章內容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務。

友情鏈接更多精彩內容