【问题标题】:how to paint on an image and save the image to phone gallery in android如何在图像上绘画并将图像保存到android中的手机图库
【发布时间】:2012-01-31 07:44:10
【问题描述】:

** 我的意图是将绘制的图像与我的背景视图一起保存到画廊** //

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

【问题讨论】:

  • 您想从资源/SD 卡中获取图像并将您的文本/颜色/绘图添加到该图像并将其保存到新文件吗?

标签: java android


【解决方案1】:

请尝试下面的工作代码 我觉得应该对你有帮助

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

【讨论】:

  • 当我使用上述代码时,我的 imagepaint 类抛出异常。你能帮我解决吗
  • 你不需要任何东西,只需制作上面类的对象并将其添加到任何布局中。你能把你的代码放在这里吗?所以我可以从中得到更好的想法来解决错误
  • drawView = (DrawableImageView) findViewById(R.id.drawView);
  • 在上面的代码中我得到了错误类转换异常
  • 请在xml中采用一种线性布局,然后在运行时在该线性布局中添加drawableimageview。
猜你喜欢
  • 2011-12-24
  • 2011-08-27
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-09-08
  • 1970-01-01
  • 2019-10-19
相关资源
最近更新 更多