[英]Android editing Drawable and setting it to ImageView in another activity
I have a method plotPoints(float x, float y) that I would like to use to plot a point on a drawable. 我有一个方法plotPoints(float x,float y),我想用它在可绘制对象上绘制一个点。 I attempt to add a point to the drawable using a bitmap and canvas. 我尝试使用位图和画布向可绘制对象添加一个点。 Then, depending on whether plotPoints() has been called, I either set the the ImageView of a separate activity to the original drawable's bitmap (without the point), or the edited bitmap with the point. 然后,根据是否已调用plotPoints(),我将单独活动的ImageView设置为原始可绘制对象的位图(不带点),或者将已编辑的位图设置为带点。 However, the ImageView always displays the original drawable, it never shows the drawn point. 但是,ImageView始终显示原始可绘制对象,而从不显示绘制点。 Please let me know if you can help. 如果您可以提供帮助,请告诉我。
Here is the related code: 以下是相关代码:
Bitmap labBitmap = BitmapFactory.decodeResource(getResources(), R.drawable.lab_image);
plotPoints() method: plotPoints()方法:
public void plotPoints(float x, float y) {
pointsPlotted = true;
//Paint for LocationActivity
Paint labPaint = new Paint();
labPaint.setColor(color.holo_purple);
//Initialize tempBitmap and attach a new canvas to it
Bitmap tempBitmap = Bitmap.createBitmap(labBitmap.getWidth(), labBitmap.getHeight(), Bitmap.Config.ARGB_8888);
Canvas tempCanvas = new Canvas(tempBitmap);
//Draw the image Bitmap onto the Canvas
tempCanvas.drawBitmap(labBitmap, 0, 0, null);
//Set paint Color and draw X and Y coordinates on Canvas
tempCanvas.drawPoint(x, y, labPaint);
}
Here is where I decide which bitmap to use and start the new activity: 在这里,我决定要使用哪个位图并开始新的活动:
public void openLabView() {
//Compress bitmap for smoother pass to LocationActivity
ByteArrayOutputStream baos = new ByteArrayOutputStream();
byte[] b;
Intent intent = new Intent(this, LocationActivity.class);
if(pointsPlotted) {
tempBitmap.compress(Bitmap.CompressFormat.PNG, 100, baos);
b = baos.toByteArray();
intent.putExtra("imageBitmap", b);
}
else {
labBitmap.compress(Bitmap.CompressFormat.PNG, 100, baos);
b = baos.toByteArray();
intent.putExtra("imageBitmap", b);
}
startActivity(intent);
}
And here is the onCreate() of the new activity: 这是新活动的onCreate():
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_location);
//Receive Bitmap for ImageView
Bundle extras = getIntent().getExtras();
byte[] b = extras.getByteArray("imageBitmap");
Bitmap imageBitmap = BitmapFactory.decodeByteArray(b, 0, b.length);
ImageView image = (ImageView) findViewById(R.id.lab_image);
image.setImageBitmap(imageBitmap);
}
In your plotPoints()
method, you define a Bitmap tempBitmap
in that method's scope. 在plotPoints()
方法中,您可以在该方法的作用域中定义一个Bitmap tempBitmap
。 So outside of that method ( openLabView()
) it has no reference to the tempBitmap
from plotPoints()
but maybe another same named Bitmap
in the class scope. 因此,在该方法之外( openLabView()
),它没有对plotPoints()
的tempBitmap
引用,但在类范围中可能还有另一个相同的名为Bitmap
引用。 This might be a reason why the Bitmap
generated in openLabView()
doesn't seem to change. 这可能是为什么openLabView()
中生成的Bitmap
似乎没有变化的原因。
May I suggest slightly renaming Bitmap tempBitmap
in plotPoints()
just for easier debugging, and see if that Bitmap
inside plotPoints()
can be passed to openLabView()
, as that Bitmap
should have been modified by the Canvas
. 我可以建议稍重命名Bitmap tempBitmap
在plotPoints()
只是为了更容易调试,并看看是否有Bitmap
内plotPoints()
可以传递给openLabView()
作为Bitmap
应该已经被修改Canvas
。 (Basically the Bitmap
passed to the Canvas
constructor is what you want.) (基本上,您想要的是传递给Canvas
构造函数的Bitmap
。)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.