Android手写签名 附带加载pdf合同 及可拖动签名

首先加载pdf是借鉴的这篇文章https://blog.csdn.net/qq_16252123,在此声明

然后我就说一下不同的地方吧 在该基础之上做了些修改。gif做残了,不太清楚,凑合看吧。。。


1.我去掉了原有的签名功能和一些与我这边业务不相关的按钮,只保留了签名,开始签名,保存,和撤销按钮


2.加了一个监听事件,使得监听翻页动作到最后一页时,才会显示签名按钮,其他页均不显示

ReaderView.java

 public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX,
                           float velocityY) {
        if (mScrollDisabled)
            return true;


        View v = mChildViews.get(mCurrent);
        if (v != null) {
            Rect bounds = getScrollBounds(v);
            sRect = bounds;
            invalidate();
            switch (directionOfTravel(velocityX, velocityY)) {
                case MOVING_LEFT:
                    if (bounds.left >= 0) {
                        // Fling off to the left bring next view onto screen
                        View vl = mChildViews.get(mCurrent + 1);
                        if ((mCurrent+1)+1 ==pageNum) {
                            listener.scroll(true);
                        }
                        if (vl != null) {
                            slideViewOntoScreen(vl);
                            return true;
                        }
                    }
                    break;
                case MOVING_RIGHT:
                    if (bounds.right <= 0) {
                        // Fling off to the right bring previous view onto screen
                        View vr = mChildViews.get(mCurrent - 1);
                        if ((mCurrent-1)+1 !=pageNum) {
                            listener.scroll(false);
                        }
                        if (vr != null) {
                            slideViewOntoScreen(vr);
                            return true;
                        }
                    }
                    break;
            }
            mScrollerLastX = mScrollerLastY = 0;

            Rect expandedBounds = new Rect(bounds);
            expandedBounds.inset(-FLING_MARGIN, -FLING_MARGIN);

            if (withinBoundsInDirectionOfTravel(bounds, velocityX, velocityY)
                    && expandedBounds.contains(0, 0)) {
                mScroller.fling(0, 0, (int) velocityX, (int) velocityY, bounds.left, bounds.right, bounds.top, bounds.bottom);
                post(this);
            }
        }

        return true;
    }
PDFActivity.java
  @Override
    public void scroll(boolean lastPage) {
        if (lastPage){
            rlSign.setVisibility(View.VISIBLE);
            Toast.makeText(PDFActivity.this, "最后一页", Toast.LENGTH_SHORT).show();

        }else {
            rlSign.setVisibility(View.GONE);
        }

    }

3.点击开始签名,跳转到签名页,很简单,我就不贴代码了,最下面有项目地址。签名完成后,回调到上一页面

此签名可以拖动,只有保存签名后,pdf才可以进行翻页操作



下面是拖动功能的布局代码,布局写好就可以拖动了。

activity_pdf.xml

 <com.example.jammy.pdf_demo.view.VDHDeepLayout
        android:id="@+id/signView1"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:visibility="gone"
        android:layout_below="@+id/rl_top">
        <ImageView
            android:id="@+id/image"
            android:layout_width="70dp"
            android:layout_height="50dp"
            android:layout_centerInParent="true"
            android:clickable="true" />

        <ImageView
            android:id="@+id/image2"
            android:layout_width="70dp"
            android:layout_height="50dp"
            android:visibility="gone" />

        <ImageView
            android:id="@+id/image3"
            android:layout_width="70dp"
            android:layout_height="50dp"
            android:visibility="gone" />
    </com.example.jammy.pdf_demo.view.VDHDeepLayout>

4.下面是将签名保存在pdf上面

signView1就是VDHDeepLayout这个view
 rlSave.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    float scale = readerView.getmScale();///得到放大因子
                    SavePdf savePdf = new SavePdf(in_path, out_path);
                    savePdf.setScale(scale);
                    savePdf.setPageNum(readerView.getDisplayedViewIndex() + 1);

                    savePdf.setWidthScale(1.0f * readerView.scrollX / readerView.getDisplayedView().getWidth());//计算宽偏移的百分比
                    savePdf.setHeightScale(1.0f * readerView.scrollY / readerView.getDisplayedView().getHeight());//计算长偏移的百分比


                    savePdf.setDensity(density);
                    Bitmap bitmap = Bitmap.createBitmap(signView1.getWidth(), signView1.getHeight(),
                            Bitmap.Config.ARGB_8888);
                    Canvas canvas = new Canvas(bitmap);
                    signView1.draw(canvas);
                    savePdf.setBitmap(bitmap);
                    save_pdf = new Save_Pdf(savePdf);
                    save_pdf.execute();
                    popupWindow.dismiss();
                    iBack = true;
                    rlSave.setVisibility(View.GONE);
                    rlClear.setVisibility(View.GONE);
                    rlUpdate.setVisibility(View.GONE);
                    rlBack.setVisibility(View.GONE);
                    signView1.setVisibility(View.GONE);
                    ///显示隐藏probar
                    progressDialog = ProgressDialog.show(PDFActivity.this, null, "正在存储...");
                    signatureView.clear();
                    readerView.setAdapter(new MuPDFPageAdapter(PDFActivity.this, muPDFCore));
                }
            });

剩下保存用到的一些类就是借鉴的文章开始的那篇文章了,  说的不详细,因为时间不够写太多,还望见谅。有问题可以留言讨论


项目下载链接


猜你喜欢

转载自blog.csdn.net/qq_16252123/article/details/80492445