我试图让SignatureView在填写表单后捕获我的用户签名,但我的代码并没有让它绘制。我在youtube上找到了这个绘图代码并尝试了它们但它没有绘制任何东西,虽然滚动锁正在工作。
代码如下签名视图的xml
<SignatureView
android:background="#FFFFFF"
android:layout_width="match_parent"
android:layout_height="400dp"
android:id="@+id/signatureView"/>
用于启用绘图的签名视图的代码
public class SignatureView extends View {
public static int BRUSH_SIZE = 5;
public static final int DEFAULT_COLOR = Color.BLACK;
public static final int DEFAUL_BG_COLOR = Color.WHITE;
private static final float TOUCH_TOLERANCE = 4;
private float mX, mY;
private Path mPath;
private Paint mPaint;
private ArrayList<FingerPath> paths = new ArrayList<>();
private int currentColor;
private int backgroundColor = DEFAUL_BG_COLOR;
private int strokewidth;
private Bitmap mBitmap;
private Canvas mCanvas;
private Paint mBitmapPaint = new Paint(Paint.DITHER_FLAG);
public SignatureView(Context context){
this(context, null);
}
public SignatureView(Context context, AttributeSet attrs){
super(context, attrs);
mPaint = new Paint();
mPaint.setAntiAlias(true);
mPaint.setDither(true);
mPaint.setColor(DEFAULT_COLOR);
mPaint.setStyle(Paint.Style.STROKE);
mPaint.setStrokeJoin(Paint.Join.ROUND);
mPaint.setStrokeCap(Paint.Cap.ROUND);
mPaint.setXfermode(null);
mPaint.setAlpha(0xff);
}
public void init(DisplayMetrics metrics){
int height = metrics.heightPixels;
int width = metrics.widthPixels;
mBitmap = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888);
mCanvas = new Canvas(mBitmap);
currentColor = DEFAULT_COLOR;
strokewidth = BRUSH_SIZE;
}
protected void onDraw(Canvas canvas){
canvas.save();
mCanvas.drawColor(backgroundColor);
for (FingerPath fp : paths){
mPaint.setColor(fp.color);
mPaint.setStrokeWidth(fp.strokewidth);
}
canvas.drawBitmap(mBitmap, 0, 0, mBitmapPaint);
canvas.restore();
}
private void touchStart(float x, float y){
mPath = new Path();
FingerPath fp = new FingerPath(currentColor, strokewidth, mPath);
paths.add(fp);
mPath.reset();
mPath.moveTo(x, y);
mX = x;
mY = y;
}
private void touchMove(float x, float y){
float dx = Math.abs(x - mX);
float dy = Math.abs(y - mY);
if (dx >= TOUCH_TOLERANCE || dy >= TOUCH_TOLERANCE){
mPath.quadTo(mX, mY, (x + mX) / 2, (y + mY) /2);
mX = x;
mY = y;
}
}
private void touchUp(){
mPath.lineTo(mX, mY);
}
@Override
public boolean onTouchEvent(MotionEvent event) {
float x = event.getX();
float y = event.getY();
getParent().requestDisallowInterceptTouchEvent(true);
switch(event.getAction()){
case MotionEvent.ACTION_DOWN :
touchStart(x, y);
invalidate();
break;
case MotionEvent.ACTION_MOVE :
touchMove(x, y);
invalidate();
break;
case MotionEvent.ACTION_UP :
touchUp();
invalidate();
break;
}
return true;
}
指纹类
public class FingerPath {
public int color;
public int strokewidth;
public Path path;
public FingerPath(int color, int strokewidth, Path path){
this.color = color;
this.strokewidth = strokewidth;
this.path = path;
}
}
页面的java类
public class Application_applicant extends AppCompatActivity {
private SignatureView sv;
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_app_applicant);
sv = (SignatureView)findViewById(R.id.signatureView);
DisplayMetrics metrics = new DisplayMetrics();
getWindowManager().getDefaultDisplay().getMetrics(metrics);
sv.init(metrics);
}
}
答案 0 :(得分:0)
请在此链接的Android演示文稿中查看MyView
课程:https://android.googlesource.com/platform/development/+/master/samples/ApiDemos/src/com/example/android/apis/graphics/FingerPaint.java
您可以在此项目中找到一些演示。这非常有用
答案 1 :(得分:0)