android 电子签名 手写签名 功能实现,Android面试总结

invalidate();

}

public Bitmap getTransparentSignatureBitmap() {

ensureSignatureBitmap();

return mSignatureBitmap;

}

public Bitmap getTransparentSignatureBitmap(boolean trimBlankSpace) {

if (!trimBlankSpace) {

return getTransparentSignatureBitmap();

}

ensureSignatureBitmap();

int imgHeight = mSignatureBitmap.getHeight();

int imgWidth = mSignatureBitmap.getWidth();

int backgroundColor = Color.TRANSPARENT;

int xMin = Integer.MAX_VALUE, xMax = Integer.MIN_VALUE, yMin = Integer.MAX_VALUE, yMax = Integer.MIN_VALUE;

boolean foundPixel = false;

// Find xMin

for (int x = 0; x < imgWidth; x++) {

boolean stop = false;

for (int y = 0; y < imgHeight; y++) {

if (mSignatureBitmap.getPixel(x, y) != backgroundColor) {

xMin = x;

stop = true;

foundPixel = true;

break;

}

}

if (stop)

break;

}

// Image is empty…

if (!foundPixel)

return null;

// Find yMin

for (int y = 0; y < imgHeight; y++) {

boolean stop = false;

for (int x = xMin; x < imgWidth; x++) {

if (mSignatureBitmap.getPixel(x, y) != backgroundColor) {

yMin = y;

stop = true;

break;

}

}

if (stop)

break;

}

// Find xMax

for (int x = imgWidth - 1; x >= xMin; x–) {

boolean stop = false;

for (int y = yMin; y < imgHeight; y++) {

if (mSignatureBitmap.getPixel(x, y) != backgroundColor) {

xMax = x;

stop = true;

break;

}

}

if (stop)

break;

}

// Find yMax

for (int y = imgHeight - 1; y >= yMin; y–) {

boolean stop = false;

for (int x = xMin; x <= xMax; x++) {

if (mSignatureBitmap.getPixel(x, y) != backgroundColor) {

yMax = y;

stop = true;

break;

}

}

if (stop)

break;

}

return Bitmap.createBitmap(mSignatureBitmap, xMin, yMin, xMax - xMin, yMa

  • 5
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Android Studio中实现手写签名的一种方法是使用Canvas和Path类来捕捉手指的移动轨迹,并将其绘制在画布上。下面是一个简单的步骤示例: 1. 创建一个新的Android项目,并在布局文件中添加一个自定义的View来显示手写签名的画布。 2. 在自定义View的类中,重写onTouchEvent方法,并在其中处理手指的触摸事件。 3. 在onTouchEvent方法中,通过MotionEvent对象获取手指的位置,并根据手指的移动轨迹更新Path对象。 4. 在自定义View的类中,重写onDraw方法,并在其中使用Canvas对象将Path对象绘制在画布上。 5. 在MainActivity中,将自定义View添加到布局文件中,并设置相应的布局参数。 以下是一个简单的示例代码: ```java public class SignatureView extends View { private Path path; private Paint paint; public SignatureView(Context context) { super(context); init(); } public SignatureView(Context context, @Nullable AttributeSet attrs) { super(context, attrs); init(); } private void init() { path = new Path(); paint = new Paint(); paint.setColor(Color.BLACK); paint.setStyle(Paint.Style.STROKE); paint.setStrokeWidth(5); } @Override protected void onDraw(Canvas canvas) { super.onDraw(canvas); canvas.drawPath(path, paint); } @Override public boolean onTouchEvent(MotionEvent event) { float x = event.getX(); float y = event.getY(); switch (event.getAction()) { case MotionEvent.ACTION_DOWN: path.moveTo(x, y); return true; case MotionEvent.ACTION_MOVE: path.lineTo(x, y); break; case MotionEvent.ACTION_UP: // Do something when the finger is lifted break; } invalidate(); return true; } } ``` 在布局文件中添加如下代码: ```xml <com.example.myapplication.SignatureView android:id="@+id/signatureView" android:layout_width="match_parent" android:layout_height="match_parent"/> ``` 在MainActivity中找到SignatureView并设置布局参数: ```java SignatureView signatureView = findViewById(R.id.signatureView); LinearLayout.LayoutParams params = new LinearLayout.LayoutParams( LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.MATCH_PARENT); signatureView.setLayoutParams(params); ```

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值