我正在尝试将我的绘图保存到Canvas和Framelayout上,背景就可以了,我能够保存Canvas并且它的绘图但问题是FrameLayout。似乎我的代码只保存画布而不是Framelayout(背景图像)。任何人都可以帮助我。
public class Draw extends Activity implements {
public static String filex;
private static final int SELECT_PICTURE = 1;
public static String selectedImagePath;
public static Bitmap cBitmap;
public static Integer resize;
public static Integer imageBackgroundEraser;
// Instance variables
public static Paint mPaint;
private MaskFilter mEmboss;
private MaskFilter mBlur;
private MyView mView;
private int mImageCount = 0;
private int mLastBg = 0;
public static Integer WW;
public static Integer WH;
private float BrushWidth;
private int thickness;
private int BrushColor;
public static int backgroundColor;
float Mx1,My1;
float x,y;
@SuppressWarnings("deprecation")
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_draw);
Display display = getWindowManager().getDefaultDisplay(); //***
final MyView myView = new MyView(this);
FrameLayout frm_layout=(FrameLayout) findViewById(R.id.main_frame);
frm_layout.addView(myView);
Drawable x = Drawable.createFromPath(selectedImagePath)
frm_layout.setBackgroundDrawable(x);
frm_layout.addView(myView);
setInitialPaint();
}
private void setInitialPaint() {
//bgColor = 0xFFFFFFFF; // default bg color white
BrushWidth = 3;
BrushColor = 0xFF000000; //**
mPaint = new Paint();
mPaint.setAntiAlias(true);
mPaint.setDither(true);
mPaint.setColor(BrushColor);
mPaint.setStyle(Paint.Style.STROKE);
mPaint.setStrokeJoin(Paint.Join.ROUND);
mPaint.setStrokeCap(Paint.Cap.ROUND);
mPaint.setStrokeWidth(BrushWidth);
mPaint.setPathEffect(new CornerPathEffect(30) );
//mEmboss = new EmbossMaskFilter(new float[] { 1, 1, 1 },0.4f, 6, 3.5f);
//mBlur = new BlurMaskFilter(8, BlurMaskFilter.Blur.NORMAL);
}
public void onClick(View view){
switch (view.getId()){
case R.id.saveBtn:
File myDir=new File("/sdcard/Pictures/Draw");
myDir.mkdirs();
String timeStamp = new SimpleDateFormat("yyyyMMdd_HHmmss").format(new Date(System.currentTimeMillis())); //***
String fname = timeStamp + ".png";
File file = new File (myDir, fname);
System.out.println(file+"----> file");
if (file.exists ()) file.delete ();
saveAsPng(file);
break;
}
}
public void saveAsPng (File f)
{
String fname = f.getAbsolutePath ();
FileOutputStream fos = null;
try {
fos = new FileOutputStream (f);
mView.mBitmap.compress (CompressFormat.PNG, 100, fos);
sendBroadcast(new Intent(Intent.ACTION_MEDIA_MOUNTED, Uri.parse("file://"+ Environment.getExternalStorageDirectory())));
Toast.makeText (getApplicationContext(), "Saved " + fname, Toast.LENGTH_LONG).show ();
} catch (Throwable ex) {
Toast.makeText (getApplicationContext(), "Error: " + ex.getMessage (), Toast.LENGTH_LONG).show ();
ex.printStackTrace ();
}
} // end saveAsPng
} // end Main
public class MyView extends View {
// Constants and variables
private static final float MINP = 0.25f;
private static final float MAXP = 0.75f;
public static Bitmap mBitmap;
public static Canvas mCanvas;
private Path mPath;
private Paint mBitmapPaint;
private Bitmap cBitmap;
private Canvas xCanvas;
private Bitmap xBitmap;
public MyView(Context c)
{
super(c);
// Figure out how this works. Seems odd that we set up a canvas and a bitmap.
// I don't see how it connects to what shows on screen.
mBitmap = Bitmap.createBitmap(Draw4FunMain.WW, Draw4FunMain.WH, Bitmap.Config.ARGB_8888);
mCanvas = new Canvas(mBitmap);
mPath = new Path();
mBitmapPaint = new Paint(Paint.DITHER_FLAG);
// mCanvas.setBackgroundResource(0xFFFFFFFF);
if (Draw4FunMain.imageBackgroundEraser == null) {
mCanvas.drawColor (Color.WHITE);
} else {
mCanvas.drawColor (Color.TRANSPARENT);
}
}
public MyView (Context c, int color)
{
super(c);
mBitmap = Bitmap.createBitmap(Draw4FunMain.WW, Draw4FunMain.WH, Bitmap.Config.ARGB_8888);
mCanvas = new Canvas(mBitmap);
mPath = new Path();
mBitmapPaint = new Paint(Paint.DITHER_FLAG);
mCanvas.drawColor (color);
//Draw4FunMain.backgroundColor = color;
}
/**
*/
// Methods
/**
*/
@Override protected void onSizeChanged(int w, int h, int oldw, int oldh)
{
super.onSizeChanged(w, h, oldw, oldh);
}
/**
*/
@Override protected void onDraw(Canvas canvas)
{
//canvas.drawColor(Color.TRANSPARENT);
canvas.drawBitmap(mBitmap, 0, 0, null);
canvas.drawPath(mPath, Draw4FunMain.mPaint);
}
/**
*/
private float mX, mY;
private static final float TOUCH_TOLERANCE = 4;
/**
*/
private void touch_start(float x, float y) {
mPath.reset();
mPath.moveTo(x, y);
mX = x;
mY = y;
}
/**
*/
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;
}
}
/**
*/
private void touch_up() {
mPath.lineTo(mX, mY);
// commit the path to our offscreen
mCanvas.drawPath(mPath, Draw4FunMain.mPaint);
// kill this so we don't double draw
mPath.reset();
}
/**
*/
@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);
invalidate();
break;
case MotionEvent.ACTION_MOVE:
touch_move(x, y);
invalidate();
break;
case MotionEvent.ACTION_UP:
touch_up();
invalidate();
break;
}
return true;
}
} // end MyView
答案 0 :(得分:0)
您为FrameLayout设置的背景图像未保存是因为您没有在要保存的位图上绘制它。
有一种简单的方法可以做你想要的,只需截取FrameLayout的截图,这样就可以捕获FrameLayout中的任何内容,并将该截图保存为文件的原始字节。
frameLayout.buildDrawingCache();
Bitmap bitmap = frameLayout.getDrawingCache();
bitmap.compress (CompressFormat.PNG, 100, fos);