【问题标题】:Getting the background color of an ImageView获取 ImageView 的背景颜色
【发布时间】:2012-02-18 07:43:36
【问题描述】:

我有一个普通的ImageView 对象colorSquare

直接调用即可设置对象的颜色。

colorSquare.setBackgroundColor(color);

但是我该如何做相反的事情,即检索 ImageView 背景的颜色?

【问题讨论】:

    标签: android colors imageview


    【解决方案1】:

    你能做的就是

    从 ImageView 中获取 ColorDrawable。

    ColorDrawable drawable = (ColorDrawable) colorSquare.getBackground();
    

    现在

    drawable.getColor()

    会给你颜色。

    这只有在你设置了颜色时才有效,否则你会得到 ClassCastException

    【讨论】:

      【解决方案2】:
      private int colorfindcolor(View v, int x, int y) {
      
      
          int offset = 1; // 3x3 Matrix
          int pixelsNumber = 0;
      
          int xImage = 0;
          int yImage = 0;
      
      
          Bitmap imageBitmap = BitmapFactory.decodeResource(context.getResources(),
                  R.drawable.palette_color);
          xImage = (int) (x * ((double) imageBitmap.getWidth() / (double) paletteImg
                  .getWidth()));
          yImage = (int) (y * ((double) imageBitmap.getHeight() / (double) paletteImg
                  .getHeight()));
      
          for (int i = xImage - offset; i <= xImage + offset; i++) {
              for (int j = yImage - offset; j <= yImage + offset; j++) {
                  try {
      
                      color = imageBitmap.getPixel(i, j);
      
                      pixelsNumber += 1;
                  } catch (Exception e) {
                      // Log.w(TAG, "Error picking color!");
                  }
              }
          }
      
          return color;
      
      }
      

      其中x,y是imageview触摸事件的event.getX(),event.getX()

      【讨论】:

      • 这个回复与问题有关吗?
      猜你喜欢
      • 1970-01-01
      • 2016-10-25
      • 1970-01-01
      • 2012-02-15
      • 2021-11-20
      • 2013-01-24
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多