Итак, я получил холст и макет.
На холсте я рисую краской, а в макете - фоновое изображение.
После рисования на холсте я хотел бы сохранить изображение, нарисованное вместе с фоном макета. Может ли кто-нибудь помочь мне, как я мог это сделать?
Более того, когда я сохраняю холст, он сохраняет только черное изображение.
public class paint extends Activity implements OnTouchListener{
DrawPanel dp;
private ArrayList<Path> pointsToDraw = new ArrayList<Path>();
private Paint mPaint;
Path path;
protected Button cancel;
protected Button save;
protected FrameLayout fl;
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
dp = new DrawPanel(this);
dp.setOnTouchListener(this);
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,WindowManager.LayoutParams.FLAG_FULLSCREEN);
requestWindowFeature(Window.FEATURE_NO_TITLE);
mPaint = new Paint();
mPaint.setDither(true);
mPaint.setColor(Color.WHITE);
mPaint.setStyle(Paint.Style.STROKE);
mPaint.setStrokeJoin(Paint.Join.MITER);
mPaint.setStrokeCap(Paint.Cap.BUTT);
mPaint.setStrokeWidth(5);
fl = new FrameLayout(this);
WindowManager.LayoutParams lp = new WindowManager.LayoutParams(
LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT,
WindowManager.LayoutParams.TYPE_APPLICATION,
WindowManager.LayoutParams.FLAG_LAYOUT_IN_SCREEN,
PixelFormat.TRANSPARENT);
fl.addView(dp,370,370);
this.getWindowManager().addView(fl , lp);
setContentView(R.layout.boy_crown);
Button save = new Button(this);
FrameLayout.LayoutParams params2 = new FrameLayout.LayoutParams( LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT );
params2.topMargin = 375;
params2.gravity =0;
params2.rightMargin = 160;
params2.gravity =0;
save.setLayoutParams(new LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT,ViewGroup.LayoutParams.WRAP_CONTENT));
save.setPadding(0,0, 130, 60);
fl.addView(save, params2);
save.setOnClickListener(new ButtonClickHandler2());
}
public class ButtonClickHandler2 implements View.OnClickListener {
public void onClick(View view) {
dp.setDrawingCacheEnabled(true);
Bitmap bm = dp.getDrawingCache();
try {
String path = Environment.getExternalStorageDirectory().toString();
File imgDirectory = new File("/sdcard/BillysCrown");
imgDirectory.mkdirs();
OutputStream fOut = null;
File file = null;
String fileid = System.currentTimeMillis() + "";
file = new File(path, "/BillysCrown/" + fileid + ".png");
Toast.makeText(paint.this, "saved at: " + file.getAbsolutePath(), Toast.LENGTH_SHORT).show();
fOut = new FileOutputStream(file);
bm.compress(Bitmap.CompressFormat.PNG, 100, fOut);
fOut.flush();
fOut.close();
} catch (Exception e) {
e.printStackTrace();
}
}
}
@Override
protected void onPause() {
// TODO Auto-generated method stub
super.onPause();
dp.pause();
}
@Override
protected void onResume() {
// TODO Auto-generated method stub
super.onResume();
dp.resume();
}
public class DrawPanel extends SurfaceView implements Runnable{
Thread t = null;
SurfaceHolder holder;
boolean isItOk = false ;
public DrawPanel(Context context) {
super(context);
// TODO Auto-generated constructor stub
holder = getHolder();
}
@Override
public void run() {
// TODO Auto-generated method stub
while( isItOk == true){
if(!holder.getSurface().isValid()){
continue;
}
holder.setFormat(PixelFormat.TRANSPARENT);
Canvas c = holder.lockCanvas();
onDraw(c);
holder.unlockCanvasAndPost(c);
}
}
@Override
protected void onDraw(Canvas canvas) {
// TODO Auto-generated method stub
super.onDraw(canvas);
synchronized(pointsToDraw)
{
for (Path path : pointsToDraw)
{
canvas.drawPath(path, mPaint);
}
}
}
public void pause(){
isItOk = false;
while(true){
try{
t.join();
}catch(InterruptedException e){
e.printStackTrace();
}
break;
}
t = null;
}
public void resume(){
isItOk = true;
t = new Thread(this);
t.start();
}
}
@Override
public boolean onTouch(View v, MotionEvent me) {
// TODO Auto-generated method stub
synchronized(pointsToDraw)
{
if(me.getAction() == MotionEvent.ACTION_DOWN){
path = new Path();
path.moveTo(me.getX(), me.getY());
//path.lineTo(me.getX(), me.getY());
pointsToDraw.add(path);
}else if(me.getAction() == MotionEvent.ACTION_MOVE){
path.lineTo(me.getX(), me.getY());
}else if(me.getAction() == MotionEvent.ACTION_UP){
//path.lineTo(me.getX(), me.getY());
}
}
return true;
}
}