2012-01-31 48 views
0

**我的意圖是挽救畫圖像與我的背景以畫廊沿** //怎麼畫的圖像和圖像保存到手機圖庫中的Android

public boolean onTouch(View view, MotionEvent event) { 

     // draw the new Points to our internal canvas/bitmap 

     System.out.println("onTouch(View vc) { IS CALLING11111111111"); 

     if (event.getAction() == MotionEvent.ACTION_DOWN) { 

      paint.setColor(Color.RED); 

      path = new Path(); 

      path.moveTo(event.getX(), event.getY()); 

     } else 
         if (event.getAction() == MotionEvent.ACTION_MOVE) { 

      int historySize = event.getHistorySize(); 

      for (int i = 0; i < historySize; i++) { 

       path.lineTo(event.getHistoricalX(i),    
               event.getHistoricalY(i)); 

      } 

      path.lineTo(event.getX(), event.getY()); 

      canvas.drawPath(path, paint); 

     } 
       else { 
      return super.onTouchEvent(event); 
     } 

     invalidate(); 

     return true; 
    } 
+0

你想從資源/ SD卡拍攝圖像和文本/彩色/圖紙添加到圖像,並把它保存到新的文件? – 2012-01-31 07:46:53

回答

0

請嘗試以下工作代碼 我覺得應該對你有所幫助

import android.content.Context; 
    import android.graphics.Bitmap; 
    import android.graphics.Canvas; 
    import android.graphics.Matrix; 
    import android.graphics.Paint; 
    import android.view.MotionEvent; 
    import android.view.View; 

public class DrawableImageView extends View { 
private Bitmap mBitmap; 
private Bitmap pic; 
private Canvas mCanvas; 
private final Paint mPaint; 
private int a = 255; 
private int r = 255; 
private int g = 255; 
private int b = 255; 
private float width = 4; 

public DrawableImageView(Context c, Bitmap img) { 
    super(c); 
    pic = img; 
    mPaint = new Paint(); 
    mPaint.setAntiAlias(true); 
    mPaint.setARGB(a,r,g,b); 


    Bitmap newBitmap = Bitmap.createBitmap(img.getWidth(), img.getHeight(), Bitmap.Config.RGB_565); 
    Canvas newCanvas = new Canvas(); 
    newCanvas.setBitmap(newBitmap); 
if (img != null) { 
    newCanvas.drawBitmap(img, 0, 0, null); 
    } 
mBitmap = newBitmap; 
mCanvas = newCanvas; 

    mCanvas.setBitmap(mBitmap); 
} 

public DrawableImageView(Context c, Bitmap img, int alpha, int red, int green, int blue) { 
this(c, img); 
setColor(alpha, red, green, blue); 
}  
public DrawableImageView(Context c, Bitmap img, int alpha, int red, int green, int blue, float w) { 
this(c, img, alpha, red, green, blue); 
width = w; 
} 

public Bitmap getBitmap() {return mBitmap;} 
public void setWidth(float w) {width = w;} 
public void setColor(int alpha, int red, int green, int blue) { 
a = alpha; 
r = red; 
g = green; 
b = blue; 
    mPaint.setARGB(a,r,g,b); 
} 
    public void Undo() { 
mCanvas.drawBitmap(pic, 0, 0, null); 
invalidate(); 
} 

float scaleX; 
float scaleY; 
float scale; 
@Override protected void onSizeChanged(int w, int h, int oldw, int oldh) {   
scaleX = (float) w/mBitmap.getWidth(); 
scaleY = (float) h/mBitmap.getHeight(); 
scale = scaleX > scaleY ? scaleY : scaleX; 
} 

@Override protected void onDraw(Canvas canvas) { 
    if (mBitmap != null) { 
     Matrix matrix = new Matrix(); 
    matrix.postScale(scale, scale); 
    canvas.drawBitmap(mBitmap, matrix, null); 
     //canvas.drawBitmap(mBitmap, 0,0, null); 
    } 
} 

float lastX; 
float lastY; 
@Override public boolean onTouchEvent(MotionEvent event) { 
    mPaint.setStrokeWidth(width/scale); 

    float curX = event.getX()/scale; 
    float curY = event.getY()/scale; 
    switch (event.getAction()){ 
    case MotionEvent.ACTION_DOWN:{ 
      mCanvas.drawCircle(curX, curY,width/2/scale, mPaint); 
     break; 
    } 
    case MotionEvent.ACTION_MOVE:{ 
    mCanvas.drawLine(lastX, lastY, curX, curY, mPaint); 
     mCanvas.drawCircle(curX, curY,width/2/scale, mPaint); //fix for weird jaggies occur between line start and line stop 
     break; 
    } 
    case MotionEvent.ACTION_CANCEL: 
case MotionEvent.ACTION_UP:{ 
    mCanvas.drawLine(lastX, lastY, curX, curY, mPaint); 
     mCanvas.drawCircle(curX, curY,width/2/scale, mPaint); 
     break; 
} 

} 
lastX = curX; 
lastY = curY; 
    invalidate(); //invalidate only modified rect... 

return true; 
    } 
    } 
+0

當我使用上面的代碼時,我的imagepaint類拋出異常。請你幫助我解決 – vinodkumar 2012-02-03 05:37:15

+0

你不需要任何東西,只要將上面的對象加入到任何佈局中就可以了。請把你的代碼放在這裏?所以我可以從它得到更好的主意,以解決錯誤 – 2012-02-03 05:41:07

+0

drawView =(DrawableImageView)findViewById(R.id.drawView); – vinodkumar 2012-02-03 06:11:04