2013-08-24 14 views
12

在android中設置視圖的方式是,它可以將一些顏色過濾器應用於低於可見範圍內的所有內容?就像這個例子:在Android中爲給定視圖下的視圖設置PorterDuff顏色效果

filtering view

只是一個簡單的矩形是反轉低於它的一切顏色的看法。當然,當用戶滾動列表時,它也反映在倒排框中。是否有一些簡單的方法來使用濾色器,PorterDuff模式等?

回答

12

你試圖使用這樣的視圖層次來解決這個問題:

  • 家長
    • 的ListView
    • InverterView

問題是,在這個位置上,InverterView沒有控制權如何繪製ListView。但是你知道誰可以控制如何繪製ListViewListView的父級佈局。換句話說,你真正需要的是這樣的一個層次:

  • 家長
    • InverterLayout
      • 的ListView

現在InverterLayout負責繪製ListView,並且可以應用效果吧。

class InverterLayout extends FrameLayout 
{ 
    // structure to hold our color filter 
    private Paint paint = new Paint(); 
    // the color filter itself 
    private ColorFilter cf; 
    // the rectangle we want to invert 
    private Rect inversion_rect = new Rect(100, 100, 300, 300); 

    public InverterLayout(Context context) 
    { 
     super(context); 
     // construct the inversion color matrix 
     float[] mat = new float[] 
     { 
      -1, 0, 0, 0, 255, 
      0, -1, 0, 0, 255, 
      0, 0, -1, 0, 255, 
      0, 0, 0, 1, 0 
     }; 
     cf = new ColorMatrixColorFilter(new ColorMatrix(mat)); 
    } 

    @Override protected void dispatchDraw(Canvas c) 
    { 
     // create a temporary bitmap to draw the child views 
     Bitmap b = Bitmap.createBitmap(getWidth(), getHeight(), Config.ARGB_8888); 
     Canvas cc = new Canvas(b); 
     // draw them to that temporary bitmap 
     super.dispatchDraw(cc); 
     // copy the temporary bitmap to screen without the inversion filter 
     paint.setColorFilter(null); 
     c.drawBitmap(b, 0, 0, paint); 
     // copy the inverted rectangle 
     paint.setColorFilter(cf); 
     c.drawBitmap(b, inversion_rect, inversion_rect, paint); 
    } 
} 

當使用這個,確保你的孩子視圖都有自己的背景。如果視圖是透明的並且窗口背景顯示爲透明,則該窗口背景將不會反轉,因爲InverterLayout無法控制如何繪製窗口。

1

如果我可以簡單地評論我會,因爲我不知道我在這裏是否正確。所以買家要小心,在這裏它:

我想你可能能夠通過利用View.OnDragEve [] ntListener來實現這種效果。這是documentation

我想你應該調整在android中使用的拖放觸發器模型(即應用程序和操作系統之間的通信手段)。你如何調整它將完全取決於你的應用程序中的機制,強制一個視圖進入另一個視圖的界限。一旦你弄清楚了,你就可以通過以下方法做出高級色彩魔術: ColorMatrixPorterDuffColorFilter和其他幾個。

這聽起來比替代Xfermode更容易。祝你好運!

1

這裏是我會做什麼:

景觀層次:

  • 的RelativeLayout(或FrameLayout裏)
    • 的ListView
      • CustomInverterView

InverterView變得定位通過在onTouchListener父視圖(RelativeLayout的) 的OnTouch:

if (event.getAction() == MotionEvent.ACTION_MOVE) 
      // Set Coordinates for the custom View. 
      // Copy the bitmapCache of the ListView (jsut the portion you need 
      // and send it to the customView. 
      // Apply the filters you want. 
      // Invalidate! 

我使用該方法實現了一個放大鏡。它像一個魅力。也許我可以稍後再發布一些代碼,但我現在有點忙。

相關問題