我创建了一个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测试了get image方法,但它很成功。问题是我如何将图像传递给CustomView并进行编辑。
答案 0 :(得分: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);
我希望这可以帮到你。
还有其他方法,但我认为这对你有用。
答案 1 :(得分: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();
}
...
...
接下来,当您获得位图时,调用设置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);
}
}