2012-02-18 13 views
6

Mam zwykły ImageView obiekt colorSquare.Uzyskanie koloru tła o ImageView

Łatwo jest ustawić kolor obiektu, dzwoniąc.

colorSquare.setBackgroundColor(color); 

Ale jak wykonać odwrotność, tj. Pobrać kolor tła ImageView?

Odpowiedz

14

Co można zrobić, to:

pobierz ColorDrawable z ImageView.

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

teraz

drawable.getColor()

będzie u koloru.

To będzie działać tylko jeśli u mieć ustawić kolor albo u dostanie ClassCastException

+0

Tak, to dziękuje. – CjS

+0

ta będzie działać tylko na poziomie API 11 lub powyżej – Maragues

+0

@Maragues Tak to będzie działać tylko na poziomie API> = 11 –

0
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; 

} 

gdzie x, y są event.getX(), event.getX() z ImageView dotykowym imprezy

+1

WTH jest to odpowiedź trzeba zrobić w/w pytanie? – swooby