【问题标题】:Android editing Drawable and setting it to ImageView in another activityAndroid 编辑 Drawable 并在另一个活动中将其设置为 ImageView
【发布时间】: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


【解决方案1】:

在您的plotPoints() 方法中,您在该方法的范围内定义了一个Bitmap tempBitmap。因此,在该方法(openLabView())之外,它没有引用来自plotPoints() 的tempBitmap,但在类范围内可能还有另一个同名的Bitmap。这可能是openLabView() 中生成的Bitmap 似乎没有变化的原因。

我建议在plotPoints() 中稍微重命名Bitmap tempBitmap 以便于调试,看看plotPoints() 中的Bitmap 是否可以传递给openLabView(),因为Bitmap 应该已经被Canvas。 (基本上传递给Canvas 构造函数的Bitmap 就是你想要的。)

【讨论】:

  • 感谢您的快速回复!我尝试在类的开头声明 tempBitmap (并重命名它),以免像你提到的那样限制范围。不幸的是,调用 openLabView() 后位图没有改变。
  • 好吧,再试一次,如果 Paint 对象没有设置画笔大小怎么办?尝试使用setStrokeWidth() 甚至绘制矩形,看看是否会有所不同。甚至可以尝试直接在plotPoints()方法中直接调用带有位图的intent代码进行测试
  • 知道了!我弄乱了一堆画图设置,最终找到了一个有效的组合。非常感谢您的帮助!
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2015-10-31
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多