在包含位图支持的画布的 Android 视图上获取点的像素颜色值 [英] Getting the pixel color value of a point on an Android View that includes a Bitmap-backed Canvas

查看:26
本文介绍了在包含位图支持的画布的 Android 视图上获取点的像素颜色值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图找出在 查看.我可以通过三种方式写入视图:

I'm trying to figure out the best way to get the pixel color value at a given point on a View. There are three ways that I write to the View:

  1. 我用 View.setBackgroundDrawable(...).

我用Canvas.drawText(...), Canvas.drawLine(...),等等,到 位图支持的画布.

I write text, draw lines, etc., with Canvas.drawText(...), Canvas.drawLine(...), etc., to a Bitmap-backed Canvas.

我通过让它们写入传递给视图的画布来绘制子对象(精灵)onDraw(Canvas canvas) 方法.

I draw child objects (sprites) by having them write to the Canvas passed to the View's onDraw(Canvas canvas) method.

这是我的类中扩展 View 的 onDraw() 方法:

Here is the onDraw() method from my class that extends View:

   @Override
   public void onDraw(Canvas canvas) {
      // 1. Redraw the background image.
      super.onDraw(canvas);
      // 2. Redraw any text, lines, etc.
      canvas.drawBitmap(bitmap, 0, 0, null);
      // 3. Redraw the sprites.
      for (Sprite sprite : sprites) {
        sprite.onDraw(canvas);
      }
    }

在考虑所有这些来源的情况下,获取像素颜色值的最佳方法是什么?

What would be the best way to get a pixel's color value that would take into account all of those sources?

推荐答案

如何将视图加载到位图(在完成所有绘图/精灵等之后的某个时间),然后从位图中获取像素颜色?

How about load the view to a bitmap (at some point after all your drawing/sprites etc is done), then get the pixel color from the bitmap?

public static Bitmap loadBitmapFromView(View v) {
    Bitmap b = Bitmap.createBitmap( v.getLayoutParams().width, v.getLayoutParams().height, Bitmap.Config.ARGB_8888);                
    Canvas c = new Canvas(b);
    v.layout(0, 0, v.getLayoutParams().width, v.getLayoutParams().height);
    v.draw(c);
    return b;
}

然后在结果上使用 getPixel(x,y)?

then use getPixel(x,y) on the result?

http://developer.android.com/reference/android/graphics/Bitmap.html#getPixel%28int,%20int%29

这篇关于在包含位图支持的画布的 Android 视图上获取点的像素颜色值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆