如何创建一个活动,我可以在屏幕上绘制,保存图像,并将其传输到另一个可绘制的屏幕?

时间:2013-09-28 21:01:12

标签: android bitmap android-canvas

现在,我正在使用CustomView在画布上绘制,我需要将画布(可能作为位图)保存到FileOutputStream中,然后移动到具有相同功能的另一个CustomView。

我不确定我是否应该使用不同的方法来实现它,但是当我调用startActivity(i)时,无论我做什么都会崩溃。

我的理解是我的CustomView被onTouch()绘制,它绘制到Path对象上,然后调用onDraw(canvas),它绘制了Canvas上的Path。 (如果我错了,请纠正我。)

此画布是否包含位图对象?或者每次调用onDraw都需要写入在onCreate()期间创建的单独位图?或者我是否需要为每次调用onDraw()创建一个新的临时位图?我已经看到了很多不同的Q& As,但还没有找到理解。

这是我的MainActivity:

公共类MainActivity扩展了Activity {

Bitmap pic;
CustomView mCustomView;
OnTouchListener touchListener;
Button eraseButton;
String [] files = { "File1.png", "File2.png", "File3.png" };

int color = Color.BLACK;

RelativeLayout layout;

private Paint paint = new Paint();
private Path path = new Path();

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    layout = (RelativeLayout) findViewById(R.id.layout);
    mCustomView = new CustomView(this);
    //layout.buildDrawingCache(); should I use this?

    layout.addView(mCustomView);

    touchListener = new OnTouchListener() {
        public boolean onTouch(View v, MotionEvent event){
            float eventX = event.getX();
            float eventY = event.getY();

            switch (event.getAction()) {
            case MotionEvent.ACTION_DOWN:
                path.moveTo(eventX, eventY);
                break;
            case MotionEvent.ACTION_MOVE:
            case MotionEvent.ACTION_UP:
                path.lineTo(eventX, eventY);
                break;
            }
            mCustomView.invalidate();
            return true;    
        }
    };
    mCustomView.setOnTouchListener(touchListener);
}


@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.main, menu);
    return true;
}

public boolean onOptionsItemSelected(MenuItem item){
    switch (item.getItemId()) {
      case R.id.black:
          Toast.makeText(this, "You have chosen " + getResources().getString(R.string.black) + ".",
                        Toast.LENGTH_SHORT).show();
          paint.setColor(Color.BLACK);
          color = Color.BLACK;         
          break;
      case R.id.red:
          Toast.makeText(this, "You have chosen " + getResources().getString(R.string.red) + ".",
                        Toast.LENGTH_SHORT).show();
          paint.setColor(Color.RED); 
          color = Color.RED;
          break;
      case R.id.blue:
          Toast.makeText(this, "You have chosen " + getResources().getString(R.string.blue) + ".",
                        Toast.LENGTH_SHORT).show();
          paint.setColor(Color.BLUE); 
          color = Color.BLUE;
          break;
      case R.id.yellow:
          Toast.makeText(this, "You have chosen " + getResources().getString(R.string.yellow) + ".",
                        Toast.LENGTH_SHORT).show();
          paint.setColor(Color.YELLOW); 
          color = Color.YELLOW;
          break;
      case R.id.erase:
          Toast.makeText(this, "You have chosen to clear the screen. The current pen is now Black.", Toast.LENGTH_SHORT).show();
          paint.setColor(Color.BLACK);
          color = Color.BLACK;
          path.reset();
          mCustomView.invalidate();
          item.setChecked(false);
          break;
      case R.id.next:
          nextScreen(); //saves the bitmap, and call startActivity(), crashing now
          return true;
      default:
          return super.onOptionsItemSelected(item);
      }
      item.setChecked(true);
      return true;
}

public void nextScreen(){
    try {
        FileOutputStream out = openFileOutput(files[0], Context.MODE_PRIVATE);
        pic.compress(Bitmap.CompressFormat.PNG, 100, out);
        out.close();
    } catch (Exception e){
        Toast.makeText(this, e.getMessage(), Toast.LENGTH_SHORT).show();
        return;
    }
    Intent i = new Intent(this.getApplicationContext(), SecondActivity.class);
    i.putExtra("filename.png", files);
    startActivity(i);
}

public class CustomView extends View {

    public CustomView(Context context) {
        super(context);
        paint.setAntiAlias(true);
        paint.setColor(color);
        paint.setStyle(Paint.Style.STROKE);
    }

    @Override
    protected void onDraw(Canvas canvas) {
        if (pic == null){
            pic = Bitmap.createBitmap(getMeasuredWidth(), getMeasuredHeight(), Bitmap.Config.ARGB_8888); //is this correct?
        }
        canvas.drawPath(path, paint);
        canvas.drawBitmap(pic, 0, 0, paint); //is this correct?
    }
}

}

1 个答案:

答案 0 :(得分:0)

如果您想保存视图,请使用public Bitmap getDrawingCache (),请参阅reference。尝试修改您的代码:

@Override
    protected void onDraw(Canvas canvas) {
        if (pic == null){
            pic = Bitmap.createBitmap(getMeasuredWidth(), getMeasuredHeight(), Bitmap.Config.ARGB_8888); //is this correct?
        }
        canvas.drawPath(path, paint);
        //canvas.drawBitmap(pic, 0, 0, paint); //is this correct?
    }

public void nextScreen(){
    try {
        Bitmap bitmap = mCustomView.getDrawingCache ();
        FileOutputStream out = openFileOutput(files[0], Context.MODE_PRIVATE);
        bitmap.compress(Bitmap.CompressFormat.PNG, 100, out);
        out.close();
    } catch (Exception e){
        Toast.makeText(this, e.getMessage(), Toast.LENGTH_SHORT).show();
        return;
    }
    Intent i = new Intent(this.getApplicationContext(), SecondActivity.class);
    i.putExtra("filename.png", files);
    startActivity(i);
}