Android CustomView(从ImageView扩展)填充为白色

Android CustomView (extends from ImageView) fill with White

此 Android 应用程序具有签名板。

这是通过从 ImageView 扩展的自定义视图并覆盖 onTouchEvent()onDraw() 方法来实现的。

现在,如果用户犯了错误,我们希望允许将签名板擦干净,让用户重新开始。我们正在尝试使用 fillWhite() 方法来实现这一点。

如何用白色填充ImageView?

public class SignatureView extends ImageView {
// setup initial color
private final int paintColor = Color.BLACK;
// defines paint and canvas
private Paint drawPaint = null;
// stores next circle
private Path path = new Path();

// set up default measurements of the signature pad
private final int PX_SIGNATURE_PAD_WIDTH = 521;
private final int PX_SIGNATURE_PAD_HEIGHT = 134;

private Canvas mCanvas = null;
public static Context stContext;
public static  AttributeSet stAttrs;

public int iScale = 0; // the scale factor from signature pad to Signature View


public SignatureView(Context context, AttributeSet attrs) {
    super(context, attrs);
    stContext = context;
    stAttrs = attrs;
    setFocusable(true);
    setFocusableInTouchMode(true);
    setBackgroundColor(Color.WHITE);
}


@Override
protected void onDraw(Canvas canvas) {

    if (null == drawPaint)
    {
        // Setup paint with color and stroke styles
        // Do this only the first time, when null == drawPaint
        drawPaint = new Paint();
        drawPaint.setColor(paintColor);
        drawPaint.setAntiAlias(true);
        Log.w("SigView","Set strokewidth to: "+3*iScale+" pixels");
        drawPaint.setStrokeWidth(3 * iScale);
        drawPaint.setStyle(Paint.Style.STROKE);
        drawPaint.setStrokeJoin(Paint.Join.ROUND);
        drawPaint.setStrokeCap(Paint.Cap.ROUND);

    }
    canvas.drawPath(path, drawPaint);
    if (null == mCanvas) {
        mCanvas = canvas;
    }
}

@Override
public boolean onTouchEvent(MotionEvent event) {
    float pointX = event.getX();
    float pointY = event.getY();
    // Checks for the event that occurs
    switch (event.getAction()) {
        case MotionEvent.ACTION_DOWN:
            path.moveTo(pointX, pointY);
            return true;
        case MotionEvent.ACTION_MOVE:
            path.lineTo(pointX, pointY);
            break;
        default:
            return false;
    }
    // Force a view to draw again
    postInvalidate();
    return true;
}
/*
Note that the below implementation does not work
*/

public void fillWhite() {
    setImageBitmap(Bitmap.createBitmap(getWidth(), getHeight(), Bitmap.Config.ARGB_8888));
    // Force a view to draw again
    postInvalidate();
}

@Override
protected void onMeasure (int widthMeasureSpec, int heightMeasureSpec) {

    // test if we are allowed to change the size of the SignatureView:
    final int widthSpecMode = MeasureSpec.getMode(widthMeasureSpec);
    final int heightSpecMode = MeasureSpec.getMode(heightMeasureSpec);
    boolean bResizeWidth = widthSpecMode != MeasureSpec.EXACTLY;
    boolean bResizeHeight = heightSpecMode != MeasureSpec.EXACTLY;
    if (bResizeWidth && bResizeHeight)
    {
        // Yes, we ARE allowed to change the size of the SignatureView:
        Dimensions d = fitSignaturePadIntoView(widthMeasureSpec, heightMeasureSpec);
        setMeasuredDimension( d.width, d.height );
    }
    else
    {
        // We are NOT allowed to change the size of the SignatureView:
        setMeasuredDimension( widthMeasureSpec, heightMeasureSpec );
    }


}

}

由于您已将 View 的背景颜色设置为白色,因此您只需清空 Path 并重新绘制即可。

path.reset();
invalidate();

我还建议将 Paint 对象初始化移至构造函数。尽管 if 会在第一次绘制后跳过它,但您会在每次触摸事件时重新绘制,因此最好使 onDraw() 方法尽可能紧凑。