-1

我正在做一个签名项目..我想做的是从用户那里得到一个签名,然后把它放在一个包含他的名字、地址和电话号码的视图中,在底部我必须添加签名......所以我已经使用位图和画布完成了签名部分。现在我想要做的是将它添加到这个视图中并将它一起保存为图像文件......所以我应该创建一个布局并将签名放入其中还是我应该制作另一个位图并将名称、添加、编号和签名整合在一起......?这是我到目前为止所做的......它工作正常并且完美地保存了一个签名......问题是如何和什么我下一步要做什么???

enter code here

/**
 * Constructor used for initializing variable
 * @param c
 *          = Context of the application
 * @param attrs
 *          = AttributeSet of XML tags for signatureView
 */
public Signature(Context c,AttributeSet attrs)
{

    super(c,attrs);
    ctx=c;
    initialize();

  //  tv1.setText((CharSequence) tv);

}

private void initialize()
{
    mBitmap = Bitmap.createBitmap(BITMAP_WIDTH,BITMAP_HEIGHT, Bitmap.Config.ARGB_8888);// i have used 480X800 resolution
    mCanvas = new Canvas(mBitmap);
    mPath = new Path();
    mBitmapPaint = new Paint(Paint.DITHER_FLAG);
    mPaint = new Paint();
    setpaint(Color.BLACK);
    mPaint.setColor(Color.BLACK);
    mPaint.setShadowLayer(10,color.darker_gray, 20, 20);
    mPaint.setTextSize(15);
    mPaint.setFlags(Paint.ANTI_ALIAS_FLAG);             
    mCanvas.drawBitmap(mBitmap, 0, 0, null);
}


/**
 * This method sets the required parameters for paint object to be used.
 * This method also sets drawing cache for SignatureView.
 * This method is private for class and only called by constructor.
 * @param color
 *          = It takes the Color as an argument in integer form which is
 *            used while drawing.
 */
private void setpaint(int color)
{
    mPaint.setAntiAlias(true);
    mPaint.setDither(true);
    mPaint.setColor(color);
    mPaint.setStyle(Paint.Style.STROKE);
    mPaint.setStrokeJoin(Paint.Join.ROUND);
    mPaint.setStrokeCap(Paint.Cap.ROUND);
    mPaint.setStrokeWidth(2);
    mPaint.setAlpha(255);
    mPaint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.SRC));
    this.setDrawingCacheEnabled(true);
    this.setDrawingCacheBackgroundColor(Color.TRANSPARENT);
    this.setDrawingCacheQuality(DRAWING_CACHE_QUALITY_HIGH);
}

/**
 * This method clears canvas by reinitializing bitmap and canvas.
 */
public void clearCanvas()
{
    //mBitmap = Bitmap.createBitmap(BITMAP_WIDTH,BITMAP_HEIGHT, Bitmap.Config.ARGB_8888);
    //mCanvas=new Canvas(mBitmap);
    initialize();
    invalidate();
}

/**
 * This method enables saving drawing made on SignatureView.
 * @param file
 *          = This method takes a file object as parameter which is initialized 
 *            with the path and filename to be saved.
 * @return
 *      = It returns a boolean value whether file is saved successfully or not.
 */
public boolean sign(File file)
{   
    sign=this.getDrawingCache();
            ImageView iv = (ImageView) findViewById(R.id.Ivsignature);      
    try{
    iv.setImageBitmap(sign);
    }catch(Exception e)
    {

    }

    String filename = file.getAbsolutePath ();
    FileOutputStream fos = null;
    try {
        fos = new FileOutputStream (file);
        sign.compress (CompressFormat.JPEG, 100, fos);
//      Toast.makeText(ctx, filename+"  saved", Toast.LENGTH_LONG).show();
        return true;
    } catch (Throwable ex) {
        Toast.makeText(ctx, "error: "+ex.getMessage(), Toast.LENGTH_LONG).show();
        return false;
    }

}

/* (non-Javadoc)
 * @see android.view.View#onSizeChanged(int, int, int, int)
 */
@Override
protected void onSizeChanged(int w, int h, int oldw, int oldh) 
{
    super.onSizeChanged(w, h, oldw, oldh);
}

/* (non-Javadoc)
 * @see android.view.View#onDraw(android.graphics.Canvas)
 */
@Override
protected void onDraw(Canvas canvas) 
{
    canvas.drawColor(Color.TRANSPARENT);
    canvas.drawBitmap(mBitmap, 0, 0, mBitmapPaint);
    canvas.drawPath(mPath, mPaint);
}

/**
 * This method is called in onTouchEvent on ACTION_DOWN event
 * this method resets path to draw on canvas and reinitializes 
 * with new coordinates. 
 * @param x
 *      =  x coordinate of ACTION_DOWN event
 * @param y
 *      = y coordinate of ACTION_DOWN event
 */
private void touch_start(float x, float y) 
{
    mPath.reset();
    mPath.moveTo(x, y);
    mX = x;
    mY = y;
}

/**
 * This method is called in onTouchEvent on ACTION_MOVE event
 * this method adds coordinates to path for drawing on canvas. 
 * @param x
 *      = x coordinate of while ACTION_MOVE event
 * @param y
 *      = y coordinate of while ACTION_MOVE event
 */
private void touch_move(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;
    }
}

/**
 * This method is called in onTouchEvent on ACTION_UP event
 * this method draws path on canvas and resets path. 
 */
private void touch_up() 
{
    mPath.lineTo(mX, mY);
    mCanvas.drawPath(mPath, mPaint);
    mPath.reset();
}


/* (non-Javadoc)
 * @see android.view.View#onTouchEvent(android.view.MotionEvent)
 */
@Override
public boolean onTouchEvent(MotionEvent event) 
{
    float x = event.getX();
    float y = event.getY();
    switch (event.getAction()) {
        case MotionEvent.ACTION_DOWN:
            touch_start(x, y);
            mCanvas.drawPoint(x, y, mPaint);
            invalidate();
            break;
        case MotionEvent.ACTION_MOVE:
            touch_move(x, y);
            invalidate();
            break;
        case MotionEvent.ACTION_UP:
            touch_up();
            invalidate();
            break;
    }

    return true;
}

}

4

1 回答 1

0

使用并在其中显示您保存的ImageView签名文件。在布局中包含后,您可以使用以下代码将布局的内容保存为:ImageViewsetImageBitmapImageViewBitmap

LinearLayout layout = (LinearLayout)findViewById(R.id.myLayout);
layout.setDrawingCacheEnabled(true);
layout.buildDrawingCache();
Bitmap bitmap = layout.getDrawingCache();
于 2012-10-17T13:42:38.330 回答