0

我已经创建了一个 Activity 类和 CustomView 类。Activity 类能够从本地存储(图库)中检索图像并显示为 ImageView。CustomView 类是使用基本绘图功能构建的,例如涂鸦、擦除、保存。

问题是:从图库中获取图像后,我需要将图像从 Activity 类传递给 CustomView 类进行编辑。我该怎么做?

这是我的 CustomView 类(我删除了不必要的代码,只显示方法名称):

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 类:

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 并进行编辑。

4

2 回答 2

0

我认为出于您的目的,您可以使用广播发送器和接收器。

第 1 步:在您的自定义视图类中创建一个广播接收器。

       BroadcastReceiver receiver = new BroadcastReceiver() {

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

第 2 步:在创建广播接收器后注册您的接收器。

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

第 3 步:从您的活动发送广播以使用意图接收图像。

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

我希望这可以帮助你。

也可以有其他方法,但我认为这对你有用。

于 2013-10-25T04:28:36.730 回答
0

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

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

接下来,当您取回位图时,调用 set the bitmap 的DrawView. 一切都应该正常..

@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);
    }
}
于 2013-10-25T07:44:37.197 回答