Pass image from activity to custom view edit

不羁岁月 提交于 2019-12-25 06:23:41

问题


I had create an Activity class and CustomView class. The Activity class able retrieve image from local storage(gallery) and show as ImageView. The CustomView class is build with basic drawing feature such as doodle, erase, save.

The question is: After I get the image from gallery, I need to pass the image from Activity class to CustomView class for edit. How should I do that ?

Here is my CustomView class (I had remove the unnecessary code, only show the method name):

class DrawView extends View {

public DrawView(Context context, AttributeSet attrs){
    super(context, attrs);
    setupDrawing();
}

private void setupDrawing(){

}

@Override
protected void onSizeChanged(int w, int h, int oldw, int oldh) {
    super.onSizeChanged(w, h, oldw, oldh);
    canvasBitmap = Bitmap.createBitmap(w, h, Bitmap.Config.ARGB_8888);
    drawCanvas = new Canvas(canvasBitmap);
}

@Override
public boolean onTouchEvent(MotionEvent event) {
    float touchX = event.getX();
    float touchY = event.getY();

    //respond to down, move and up events   
    //redraw the view   
}

//draw view - after touch event
    @Override
    protected void onDraw(Canvas canvas) {
        canvas.drawBitmap(canvasBitmap, 0, 0, canvasPaint);
        canvas.drawPath(drawPath, drawPaint);   
    }

}

And this is my MainActivity class:

public class MainActivity extends Activity implements OnClickListener {

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

    //call the custom drawing view
    drawView = (DrawView)findViewById(R.id.drawable);
}

@Override
public void onClick(View view){
if(view.getId()==R.id.Go){

        Intent i = new Intent(
                Intent.ACTION_PICK,
                android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);

        startActivityForResult(i, RESULT_LOAD_IMAGE);
    }
}

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    super.onActivityResult(requestCode, resultCode, data);

    if (requestCode == RESULT_LOAD_IMAGE && resultCode == RESULT_OK && null != data) {
        Uri selectedImage = data.getData();
        String[] filePathColumn = { MediaStore.Images.Media.DATA };

        Cursor cursor = getContentResolver().query(selectedImage,
                filePathColumn, null, null, null);
        cursor.moveToFirst();

        int columnIndex = cursor.getColumnIndex(filePathColumn[0]);
        String picturePath = cursor.getString(columnIndex);
        cursor.close();

        Intent i = new Intent(this, DrawView.class);
        i.putExtra(picturePath, true);

    }
}

I had testing the get image method by using ImageView and it is success. The question is how i pass the image to CustomView and edit.


回答1:


First create a method in your DrawView to set the bitmap:

class DrawView extends View {

public DrawView(Context context, AttributeSet attrs){
    super(context, attrs);
    setupDrawing();
}

public void setCanvasBitmap(Bitmap bitmap) {
        canvasBitmap = bitmap;
        drawCanvas = new Canvas(canvasBitmap);
        invalidate();
    }
...
...

Next when you get the bitmap back, call set the bitmap of the DrawView. Everything should work fine..

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    super.onActivityResult(requestCode, resultCode, data);

    if (requestCode == RESULT_LOAD_IMAGE && resultCode == RESULT_OK && null != data) {
        Uri selectedImage = data.getData();
        String[] filePathColumn = { MediaStore.Images.Media.DATA };

        Cursor cursor = getContentResolver().query(selectedImage,
                filePathColumn, null, null, null);
        cursor.moveToFirst();

        int columnIndex = cursor.getColumnIndex(filePathColumn[0]);
        String picturePath = cursor.getString(columnIndex);
        cursor.close();

        Bitmap loadedBitmap = BitmapFactory.decodeFile(picturePath);
        Bitmap drawableBitmap = loadedBitmap.copy(Bitmap.Config.ARGB_8888, true);
        drawView.setCanvasBitmap(drawableBitmap);
    }
}



回答2:


I think for your purpose your can use broadcast sender and receiver.

Step 1: Create a broadcast receiver in your custom view class.

       BroadcastReceiver receiver = new BroadcastReceiver() {

            @Override
            public void onReceive(Context context, Intent intent) {
                if (intent.getAction().equals("image_received")) {
                    // GET YOUR IMAGE HERE.
                }
            }
        };

Step 2: Register your receiver after creating your broadcast receiver.

IntentFilter filter = new IntentFilter("image_received");
registerReceiver(receiver, filter);

Step 3: Send broadcast from your activity to receive image using intent.

Intent intent = new Intent("image_received");
intent.putExtra("image", YOUR_IMAGE_OBJECT);
SendBroadcast(intent);

I hope this can help you.

There can be other ways too but I think this will work for you.



来源:https://stackoverflow.com/questions/19580780/pass-image-from-activity-to-custom-view-edit

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!