1. 程式人生 > >Android Launcher3獲取桌布作為左屏背景 並實現毛玻璃效果

Android Launcher3獲取桌布作為左屏背景 並實現毛玻璃效果

在Launcher.java中(實現左屏的方法詳見https://mp.csdn.net/postedit/80192206)

在oncreate()中

 /*wanchengguo 負一屏載入佈局 20180424 begin*/
        WallpaperManager mWallpaperManager = WallpaperManager.getInstance(getApplicationContext());
        /*wanchengguo 負一屏載入佈局 20180424 end*/

新建方法    

private void setBackgroundScreen() {

    //setBackNegative(int x, int y, int width, int height) 
    DisplayMetrics dm = this.getResources().getDisplayMetrics();
int width = dm.widthPixels;

int height = dm.heightPixels;//獲取螢幕的高寬

    //ll_main 是左屏的主控制元件     

    ll_main.setBackgroundDrawable(setBackNegative(0,0,width,height));// //起始的X Y 要截的圖的寬度  要截的圖的高度

    }

    public Drawable  setBackNegative(int x,int y,int width,int height) {
    WallpaperInfo wallpaperInfo = mWallpaperManager.getWallpaperInfo(); 
   
    Bitmap mBackground = null;
    if(wallpaperInfo != null) {//判斷是靜態還是動態桌布
   
    }else {
    mBackground = mWallpaperManager.getBitmap();//獲取當前桌面處理後的桌布  ~靜態桌布
   
    }
   
    Bitmap result = Bitmap.createBitmap(mBackground, x,y,width,height, null, false);//根據螢幕大小,以及view長寬,裁剪當前桌面桌布   起始的X Y 要截的圖的寬度  要截的圖的高度
    Drawable drawable = FastBlur.doBlur(result);//高斯模糊  FastBlur 自己新建的類 在文章後面貼出
    return drawable ;

    }

桌布變化更新背景

 private BroadcastReceiver WallpaperIntentReceiver = new BroadcastReceiver() {
//監聽桌布更換的廣播
 @Override
 public void onReceive(Context context, Intent intent) {
 String action = intent.getAction();
 //Intent.ACTION_WALLPAPER_CHANGED
 android.util.Log.i("wcg","更換了桌布");
 
 setBackgroundScreen();
           
 }
 };

在oncreate()中

 //註冊桌布切換的廣播 wanchengguo 20180427 begin

        
        IntentFilter intentFilter_wallpaper = new IntentFilter(Intent.ACTION_WALLPAPER_CHANGED);
        registerReceiver(WallpaperIntentReceiver, intentFilter_wallpaper);

        //註冊桌布切換的廣播 wanchengguo 20180427 end

/*毛玻璃模糊效果類:*/
public class FastBlur {
private static final float BLUR_RADIUS = 10.0f;

public static Drawable doBlur(Bitmap sentBitmap) {
int radius = (int)BLUR_RADIUS ;
Bitmap bitmap = sentBitmap;



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 dv[] = new int[256 * divsum];
for (i = 0; i < 256 * divsum; 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++) {
// Preserve alpha channel: ( 0xff000000 & pix[yi] )
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);
//Bitmap cc = roundBitmapByShader(bitmap,bitmap.getWidth(),bitmap.getWidth(),25);
Drawable drawable = new BitmapDrawable(bitmap);
return (drawable);
}
/**
     * 利用BitmapShader繪製圓角圖片
     *
     * @param bitmap
     *              待處理圖片
     * @param outWidth
     *              結果圖片寬度,一般為控制元件的寬度
     * @param outHeight
     *              結果圖片高度,一般為控制元件的高度
     * @param radius
     *              圓角半徑大小
     * @return
     *              結果圖片
     */
    private static Bitmap roundBitmapByShader(Bitmap bitmap, int outWidth, int outHeight, int radius) {
        if(bitmap == null) {
            throw new NullPointerException("Bitmap can't be null");
        }
        android.util.Log.i("wanchengguo","587bitmap.getWidth()="+bitmap.getWidth());
        // 初始化縮放比
        float widthScale = outWidth * 1.0f / bitmap.getWidth();
        float heightScale = outHeight * 1.0f / bitmap.getHeight();
        Matrix matrix = new Matrix();
        matrix.setScale(widthScale, heightScale);
        
        // 初始化繪製紋理圖
        BitmapShader bitmapShader = new BitmapShader(bitmap, Shader.TileMode.CLAMP, Shader.TileMode.CLAMP);
        
        // 根據控制元件大小對紋理圖進行拉伸縮放處理
        bitmapShader.setLocalMatrix(matrix);


        // 初始化目標bitmap
        Bitmap targetBitmap = Bitmap.createBitmap(outWidth, outHeight, Bitmap.Config.ARGB_8888);


        // 初始化目標畫布
        Canvas targetCanvas = new Canvas(targetBitmap);


        // 初始化畫筆
        Paint paint = new Paint();
        paint.setAntiAlias(true);
        paint.setShader(bitmapShader);


        // 利用畫筆將紋理圖繪製到畫布上面
        targetCanvas.drawRoundRect(new RectF(0, 0, outWidth, outWidth), radius, radius, paint);
       
        
        return targetBitmap;
    }




}