【发布时间】:2014-07-02 23:15:04
【问题描述】:
我有一个方法 plotPoints(float x, float y),我想用它在可绘制对象上绘制一个点。我尝试使用位图和画布向可绘制对象添加一个点。然后,根据是否调用了 plotPoints(),我要么将单独活动的 ImageView 设置为原始可绘制的位图(不带点),要么将已编辑的带点位图设置为。但是,ImageView 始终显示原始可绘制对象,它从不显示绘制点。如果您能提供帮助,请告诉我。
相关代码如下:
Bitmap labBitmap = BitmapFactory.decodeResource(getResources(), R.drawable.lab_image);
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);
}
这是我决定使用哪个位图并开始新活动的地方:
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);
}
这里是新活动的 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);
}
【问题讨论】:
-
我会开始尝试通过在 openLabView() 中设置断点并运行几个调试来跟踪这一点,这样您就可以在 pointsPlotted 为 true 而为 false 时检查 ByteArray 的内容。跨度>
标签: android canvas bitmap imageview drawable