获取在Android查看一个点,其中包括一个位图背帆布像素颜色值 [英] Getting the pixel color value of a point on an Android View that includes a Bitmap-backed Canvas

查看:157
本文介绍了获取在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. 我设置的<一个背景图像href="http://developer.android.com/reference/android/view/View.html#setBackgroundDrawable%28android.graphics.drawable.Drawable%29">View.setBackgroundDrawable(...).

我写的文字,画线等,具有 Canvas.drawText(...),的 Canvas.drawLine(...)等,在<一个href="http://developer.android.com/reference/android/graphics/Canvas.html#Canvas%28android.graphics.Bitmap%29">Bitmap-backed帆布。

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

我画子对象(精灵)通过让他们写画布传递给视图的的OnDraw(帆布油画)方法

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

下面是我班上的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?

<一个href="http://developer.android.com/reference/android/graphics/Bitmap.html#getPixel%28int,%20int%29">http://developer.android.com/reference/android/graphics/Bitmap.html#getPixel%28int,%20int%29

这篇关于获取在Android查看一个点,其中包括一个位图背帆布像素颜色值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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