2014-02-05 100 views
2

是否可以在Android佈局中創建洞。我有兩個背景,一個普通,一個模糊,以及我想要創建的單元格可以顯示模糊的背景。在Android佈局中挖洞

我的XML將被修建這樣的:

Background_blurred> Background_normal>細胞。

- 我已經知道如何模糊背景。

-Cells無處不在,就像一個GridView,而不只是在邊界上。 enter image description here

+1

你可以做一個圖片?只是爲了弄清楚你的意思。 –

+0

我不認爲這是可能的,但我想看看是否有人想出了一些東西。給予好評。那麼,使用自定義視圖處理所有單元的繪製可能是可能的,但我沒有看到在佈局或GridView中使用它的簡單方法。 – Tenfour04

+1

現在我只能想象2層位圖:1(模糊)作爲背景。另一個(非模糊)孔,像Emmenthal奶酪切片,背景。 –

回答

1

試試這個:(中的onCreate)

class FL extends FrameLayout { 
    private List<View> mViews = new ArrayList<View>(); 
    private Bitmap mBack; 
    private Bitmap mBackBlur; 
    private int[] mLocation = new int[2]; 
    private Matrix mMatrix = new Matrix(); 

    public FL(Context context) { 
     super(context); 
     Resources res = getResources(); 
     mBack = BitmapFactory.decodeResource(res, R.drawable.back); 
     mBackBlur = BitmapFactory.decodeResource(res, R.drawable.back_blur); 
    } 

    @Override 
    protected void onSizeChanged(int w, int h, int oldw, int oldh) { 
     RectF src = new RectF(0, 0, mBack.getWidth(), mBack.getHeight()); 
     RectF dst = new RectF(0, 0, w, h); 
     mMatrix.setRectToRect(src, dst, ScaleToFit.FILL); 
    } 

    public void add(View v) { 
     mViews.add(v); 
    } 

    @Override 
    protected void dispatchDraw(Canvas canvas) { 
     canvas.drawBitmap(mBack, mMatrix, null); 

     canvas.save(Canvas.CLIP_SAVE_FLAG); 
     getLocationOnScreen(mLocation); 
     int x = mLocation[0]; 
     int y = mLocation[1]; 
     Op op = Op.REPLACE; 
     for (View v : mViews) { 
      v.getLocationOnScreen(mLocation); 
      mLocation[0] -= x; 
      mLocation[1] -= y; 

      int left = mLocation[0]; 
      int top = mLocation[1]; 
      int right = left + v.getWidth(); 
      int bottom = top + v.getHeight(); 
      canvas.clipRect(left, top, right, bottom, op); 
      op = Op.UNION; 
     } 
     canvas.drawBitmap(mBackBlur, mMatrix, null); 
     canvas.restore(); 
     super.dispatchDraw(canvas); 
    } 
} 

測試代碼:

FL fl = new FL(this); 
    ScrollView sv = new ScrollView(this); 
    LinearLayout ll = new LinearLayout(this); 
    ll.setOrientation(LinearLayout.VERTICAL); 
    float[] hsv = { 
      0, 1, 0.75f 
    }; 
    float[] h = { 
      0, 39, 60, 120, 300 
    }; 
    for (int i = 0; i < 5; i++) { 
     TextView tv = new TextView(this); 
     hsv[0] = h[i]; 
     tv.setTextColor(0xffeeeeee); 
     tv.setBackgroundColor(Color.HSVToColor(128, hsv)); 
     tv.setPadding(10, 10, 10, 10); 
     tv.setTextSize(64); 
     tv.setText("#" + i); 
     LayoutParams params = new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT); 
     params.topMargin = 10; 
     params.bottomMargin = 10; 
     params.leftMargin = 20; 
     params.rightMargin = 20; 
     ll.addView(tv, params); 
     fl.add(tv); 
    } 
    sv.addView(ll); 
    fl.addView(sv); 
    setContentView(fl); 

其中R.drawable.back是:

enter image description here

和R.drawable .back_blur是:

enter image description here

結果是:

enter image description here

+0

哇,這真的很好,但它有一些問題(失真):模糊的圖像較小,當我滾動時縮小。我會進入這個解決方案。 – Tsunaze

+0

是的,有一些小fehler,現在看到 – pskink

+0

哈,模糊的背景仍然偏移(和更小),也許我的圖像的大小不一樣,這就是爲什麼當我滾動到遠處的效果是偏遠的。我會盡快給您回覆。但這是一個很大的改進! – Tsunaze