简体   繁体   中英

convert image to bitmap (Android)

If I have a view object with a canvas that draws lines how do I convert the image to a bitmap and then save it as a PNG? I want to do this in the saveCanvasImage() method. In the code below I'm assuming I have to use getDrawingCache somehow with the tv object. I don't get what I've read about this at all. (Assume the view object exists even though that doesn't happen until a command button is pressed).

public class MainActivity extends Activity {

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

@Override
protected void onPause() {
    // TODO Auto-generated method stub
    super.onPause();
    finish();
}


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

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    // Handle presses on the action bar items
    switch (item.getItemId()) {
        case R.id.action_save:
            saveCanvasImage();
            return true;
        case R.id.action_sign:
            openTEV();
            return true;
        default:
            return super.onOptionsItemSelected(item);
    }
}

public void saveCanvasImage() {

    Log.d("key1","save test");

}

public void openTEV() {

    Log.d("key2","set content view");
    MyTouchEventView tv = new MyTouchEventView(this);
    setContentView(tv);
    addContentView(tv.btnReset, tv.params);

}

}

MyTouchEventView.java:

public class MyTouchEventView extends View {

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

public Button btnReset;
public LayoutParams params;

public MyTouchEventView(Context context) {
    super(context);

    paint.setAntiAlias(true);
    paint.setColor(Color.BLACK);
    paint.setStyle(Paint.Style.STROKE);
    paint.setStrokeJoin(Paint.Join.ROUND);
    paint.setStrokeWidth(10f);

    btnReset = new Button(context);
    btnReset.setText("Clear Screen");

    params = new LayoutParams(LayoutParams.MATCH_PARENT,
            LayoutParams.WRAP_CONTENT);
    btnReset.setLayoutParams(params);

    btnReset.setOnClickListener(new View.OnClickListener() {

        public void onClick(View v) {
            // TODO Auto-generated method stub

            // resets the screen
            path.reset();

            // Calls the onDraw() method
            postInvalidate();

        }
    });

}

@Override
protected void onDraw(Canvas canvas) {
    canvas.drawPath(path, paint);
}

@Override
public boolean onTouchEvent(MotionEvent event) {

    // Gives you x and y coordinates on the Event.
    float pointX = event.getX();
    float pointY = event.getY();

    // Checks for the event that occurs
    switch (event.getAction()) {
    case MotionEvent.ACTION_DOWN:
        path.moveTo(pointX, pointY);
        return true;
    case MotionEvent.ACTION_MOVE:
        path.lineTo(pointX, pointY);
        break;
    case MotionEvent.ACTION_UP:
        //;
        break;
    default:
        return false;
    }

    // Schedules a repaint.
    // Force a view to draw.
    postInvalidate();
    return true;
}

}

Each View has a bitmap in which the view drawing is cached. Have a read about View.getDrawingCache() in the android reference docs.

Note: you will also need to set View.setDrawingCacheEnabled(true)


To answer your comment, at the moment you create a MyTouchEventView called tv locally in the method openTEV() .

To make tv acessable in other methods, you could instead make it a class level object. Declare it at the top of your MainActivity class.

public class MainActivity extends Activity {
    // Class Variables
    private MyTouchEventView tv;
    ...

Then you can change openTEV() to:

public void openTEV() {

    Log.d("key2","set content view");
    tv = new MyTouchEventView(this);
    setContentView(tv);
    addContentView(tv.btnReset, tv.params);
}

Then you can also access it it elsewhere in the MainActivity class. For example:

public void saveCanvasImage() {

    ...
    tv.getDrawingCache();  // We can access tv because it was created at the class level
    ...

    Log.d("key1","save test");

}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM