Android教程網
  1. 首頁
  2. Android 技術
  3. Android 手機
  4. Android 系統教程
  5. Android 游戲
 Android教程網 >> Android技術 >> 關於Android編程 >> Android模糊處理簡單實現毛玻璃效果

Android模糊處理簡單實現毛玻璃效果

編輯:關於Android編程

自從iOS系統引入了Blur效果,也就是所謂的毛玻璃、模糊化效果、磨砂效果,各大系統就開始競相模仿,這是怎樣的一個效果呢,我們先來看一下,如下面的圖片:

實現效果大家都知道了,如何在Android中實現呢,說白了就是對圖片進行模糊化處理,小編先給大家講一下Android高級模糊技術的原理,如下:

  • 首先我創建了一個空的bitmap,把背景的一部分復制進去,之後我會對這個bitmap進行模糊處理並設置為TextView的背景。
  • 通過這個bitmap保存Canvas的狀態;
  • 在父布局文件中把Canvas移動到TextView的位置;
  • 把ImageView的內容繪到bitmap中;
  • 此時,我們就有了一個和TextView一樣大小的bitmap,它包含了ImageView的一部分內容,也就是TextView背後一層布局的內容;
  • 創建一個Renderscript的實例;
  • 把bitmap復制一份到Renderscript需要的數據片中;
  • 創建Renderscript模糊處理的實例;
  • 設置輸入,半徑范圍然後進行模糊處理;
  • 把處理後的結果復制回之前的bitmap中;
  • 好了,我們已經把bitmap驚醒模糊處理了,可以將它設置為TextView背景了;

我最近在做一款App,其中有一個功能需要對圖片處理實現毛玻璃的特效,經過一番研究,找到了3中實現方案,其中各有優缺點,如果系統的api在16以上,可以使用系統提供的方法直接處理圖片,但是小編認為下邊的解決方案是實現效果最好的。
代碼如下:

public Bitmap fastblur(Context context, Bitmap sentBitmap, int radius) { 
 
    Bitmap bitmap = sentBitmap.copy(sentBitmap.getConfig(), true); 
 
    if (radius < 1) { 
      return (null); 
    } 
 
    int w = bitmap.getWidth(); 
    int h = bitmap.getHeight(); 
 
    int[] pix = new int[w * h]; 
    bitmap.getPixels(pix, 0, w, 0, 0, w, h); 
 
    int wm = w - 1; 
    int hm = h - 1; 
    int wh = w * h; 
    int div = radius + radius + 1; 
 
    int r[] = new int[wh]; 
    int g[] = new int[wh]; 
    int b[] = new int[wh]; 
    int rsum, gsum, bsum, x, y, i, p, yp, yi, yw; 
    int vmin[] = new int[Math.max(w, h)]; 
 
    int divsum = (div + 1) >> 1; 
    divsum *= divsum; 
    int temp = 256 * divsum; 
    int dv[] = new int[temp]; 
    for (i = 0; i < temp; i++) { 
      dv[i] = (i / divsum); 
    } 
 
    yw = yi = 0; 
 
    int[][] stack = new int[div][3]; 
    int stackpointer; 
    int stackstart; 
    int[] sir; 
    int rbs; 
    int r1 = radius + 1; 
    int routsum, goutsum, boutsum; 
    int rinsum, ginsum, binsum; 
 
    for (y = 0; y < h; y++) { 
      rinsum = ginsum = binsum = routsum = goutsum = boutsum = rsum = gsum = bsum = 0; 
      for (i = -radius; i <= radius; i++) { 
        p = pix[yi + Math.min(wm, Math.max(i, 0))]; 
        sir = stack[i + radius]; 
        sir[0] = (p & 0xff0000) >> 16; 
        sir[1] = (p & 0x00ff00) >> 8; 
        sir[2] = (p & 0x0000ff); 
        rbs = r1 - Math.abs(i); 
        rsum += sir[0] * rbs; 
        gsum += sir[1] * rbs; 
        bsum += sir[2] * rbs; 
        if (i > 0) { 
          rinsum += sir[0]; 
          ginsum += sir[1]; 
          binsum += sir[2]; 
        } else { 
          routsum += sir[0]; 
          goutsum += sir[1]; 
          boutsum += sir[2]; 
        } 
      } 
      stackpointer = radius; 
 
      for (x = 0; x < w; x++) { 
 
        r[yi] = dv[rsum]; 
        g[yi] = dv[gsum]; 
        b[yi] = dv[bsum]; 
 
        rsum -= routsum; 
        gsum -= goutsum; 
        bsum -= boutsum; 
 
        stackstart = stackpointer - radius + div; 
        sir = stack[stackstart % div]; 
 
        routsum -= sir[0]; 
        goutsum -= sir[1]; 
        boutsum -= sir[2]; 
 
        if (y == 0) { 
          vmin[x] = Math.min(x + radius + 1, wm); 
        } 
        p = pix[yw + vmin[x]]; 
 
        sir[0] = (p & 0xff0000) >> 16; 
        sir[1] = (p & 0x00ff00) >> 8; 
        sir[2] = (p & 0x0000ff); 
 
        rinsum += sir[0]; 
        ginsum += sir[1]; 
        binsum += sir[2]; 
 
        rsum += rinsum; 
        gsum += ginsum; 
        bsum += binsum; 
 
        stackpointer = (stackpointer + 1) % div; 
        sir = stack[(stackpointer) % div]; 
 
        routsum += sir[0]; 
        goutsum += sir[1]; 
        boutsum += sir[2]; 
 
        rinsum -= sir[0]; 
        ginsum -= sir[1]; 
        binsum -= sir[2]; 
 
        yi++; 
      } 
      yw += w; 
    } 
    for (x = 0; x < w; x++) { 
      rinsum = ginsum = binsum = routsum = goutsum = boutsum = rsum = gsum = bsum = 0; 
      yp = -radius * w; 
      for (i = -radius; i <= radius; i++) { 
        yi = Math.max(0, yp) + x; 
 
        sir = stack[i + radius]; 
 
        sir[0] = r[yi]; 
        sir[1] = g[yi]; 
        sir[2] = b[yi]; 
 
        rbs = r1 - Math.abs(i); 
 
        rsum += r[yi] * rbs; 
        gsum += g[yi] * rbs; 
        bsum += b[yi] * rbs; 
 
        if (i > 0) { 
          rinsum += sir[0]; 
          ginsum += sir[1]; 
          binsum += sir[2]; 
        } else { 
          routsum += sir[0]; 
          goutsum += sir[1]; 
          boutsum += sir[2]; 
        } 
 
        if (i < hm) { 
          yp += w; 
        } 
      } 
      yi = x; 
      stackpointer = radius; 
      for (y = 0; y < h; y++) { 
        pix[yi] = (0xff000000 & pix[yi]) | (dv[rsum] << 16) 
            | (dv[gsum] << 8) | dv[bsum]; 
 
        rsum -= routsum; 
        gsum -= goutsum; 
        bsum -= boutsum; 
 
        stackstart = stackpointer - radius + div; 
        sir = stack[stackstart % div]; 
 
        routsum -= sir[0]; 
        goutsum -= sir[1]; 
        boutsum -= sir[2]; 
 
        if (x == 0) { 
          vmin[y] = Math.min(y + r1, hm) * w; 
        } 
        p = x + vmin[y]; 
 
        sir[0] = r[p]; 
        sir[1] = g[p]; 
        sir[2] = b[p]; 
 
        rinsum += sir[0]; 
        ginsum += sir[1]; 
        binsum += sir[2]; 
 
        rsum += rinsum; 
        gsum += ginsum; 
        bsum += binsum; 
 
        stackpointer = (stackpointer + 1) % div; 
        sir = stack[stackpointer]; 
 
        routsum += sir[0]; 
        goutsum += sir[1]; 
        boutsum += sir[2]; 
 
        rinsum -= sir[0]; 
        ginsum -= sir[1]; 
        binsum -= sir[2]; 
 
        yi += w; 
      } 
    } 
 
    bitmap.setPixels(pix, 0, w, 0, 0, w, h); 
    return (bitmap); 
  } 

以上就是本文的全部內容,幫助大家輕松實現毛玻璃效果,希望大家喜歡。

  1. 上一頁:
  2. 下一頁:
熱門文章
閱讀排行版
Copyright © Android教程網 All Rights Reserved