2012-10-17 45 views
-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; 
} 

}

+0

是必要的,你創建簽名爲位圖?我會保留它的文本和樣式。 – SunnySonic

+0

不,我必須給用戶全屏...爲了得到簽名..天然必須使用位圖 – Audi

回答

0

使用的ImageView和使用setImageBitmap顯示該ImageView您保存的簽名文件。一旦你已經包括在佈局ImageView,可以使用下面的代碼佈局的內容保存爲Bitmap

LinearLayout layout = (LinearLayout)findViewById(R.id.myLayout); 
layout.setDrawingCacheEnabled(true); 
layout.buildDrawingCache(); 
Bitmap bitmap = layout.getDrawingCache(); 
+0

然後保存佈局爲圖片??? – Audi

+0

答覆已更新。 – Rajesh

+0

我沒有得到如何將位圖包含到佈局中... – Audi