Android CustomView(从ImageView扩展)填充White

时间:2016-01-18 13:51:07

标签: android imageview android-imageview

此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 );
    }


}

}

1 个答案:

答案 0 :(得分:2)

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

path.reset();
invalidate();

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