从一个活动传递形象自定义视图编辑

问题描述:

我创建活动类和CustomView类。 Activity类能够检索本地存储(画廊),并显示为ImageView的形象。该CustomView类是基本的绘图功能,打造如涂鸦,擦除,保存。

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.

现在的问题是:
之后我从画廊的形象,我需要从Activity类的形象传递给CustomView类编辑。我应该怎么办呢?

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 ?

下面是我的CustomView类(我不得不删除不必要的code,只显示了该方法的名称):

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

}

这是我MainActivity类别:

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);

    }
}

我已经使用的ImageView测试获取图像的方法,它是成功的。现在的问题是怎样的形象传递给CustomView和编辑。

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.

首先创建一个方法你的 DrawView 设置位图:

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();
    }
...
...

接下来,当你的位图回来,拨打设定的 DrawView 的位图。应当一切正常。

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