我已经创建了一个 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 并进行编辑。