微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

Android实现个人资料页面头像背景模糊显示包(状态栏)

最近要实现这样一个效果,然后拿出来与大家分享一下主要的几段代码,希望大家能够用到,与人方便自己方便嘛!

首先:

要实现的是浮动状态栏效果,通过在Activity的onCreate方法调用这个方法,然后就可以让整个布局浮现在整个手机屏幕之下了,这是我觉着最简单的一种方法了。

public static void alphaTask(Activity context) {
  context.getwindow().requestFeature(Window.FEATURE_NO_TITLE);
  if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
    Window window = context.getwindow();
    window.clearFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS
        | WindowManager.LayoutParams.FLAG_TRANSLUCENT_NAVIGATION);
    window.getDecorView().setsystemUIVisibility(View.SYstem_UI_FLAG_LAYOUT_FULLSCREEN
        | View.SYstem_UI_FLAG_LAYOUT_HIDE_NAVIGATION
        | View.SYstem_UI_FLAG_LAYOUT_STABLE);
    window.addFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYstem_BAR_BACKGROUNDS);
    window.setStatusBarColor(Color.TRANSPARENT);
    window.setNavigationBarColor(Color.TRANSPARENT);
  }
}

接下来:

获得状态栏高度,将actionbar(我这里是自己在布局当中写的,然后在代码中设置他距离顶部的高度让他看起来和我们的bar没有什么区别)的位置改变位于状态来的下方

public static int getStatusBarHight(Context context) {
    /**
     * 获取状态栏高度――方法1
     * */
    int statusBarHeight = -1;
    //获取status_bar_height资源的ID
    int resourceId = context.getResources().getIdentifier("status_bar_height","dimen","android");
    if (resourceId > 0) {
      //根据资源ID获取响应的尺寸值
      statusBarHeight = context.getResources().getDimensionPixelSize(resourceId);
    }
//    int i = DensityUtil.px2dip(context,statusBarHeight);

    return statusBarHeight;
  }

最后:

实现背景高斯模糊效果

public class FastBlurUtils {
  public static Bitmap doblur(Bitmap sentBitmap,int radius,boolean canReuseInBitmap) {
    Bitmap bitmap;
    if (canReuseInBitmap) {
      bitmap = sentBitmap;
    } else {
      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,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,h);
    return (bitmap);
  }
}

这里再加一个上圆形头像的工具,将图片转换为圆形,很方便的

/**
 * 转换图片成圆形
 * @param bitmap 传入Bitmap对象
 * @return
 */
public static Bitmap toRoundBitmap(Bitmap bitmap) {
  int width = bitmap.getWidth();
  int height = bitmap.getHeight();
  float roundPx;
  float left,top,right,bottom,dst_left,dst_top,dst_right,dst_bottom;
  if (width <= height) {
    roundPx = width / 2;
    top = 0;
    bottom = width;
    left = 0;
    right = width;
    height = width;
    dst_left = 0;
    dst_top = 0;
    dst_right = width;
    dst_bottom = width;
  } else {
    roundPx = height / 2;
    float clip = (width - height) / 2;
    left = clip;
    right = width - clip;
    top = 0;
    bottom = height;
    width = height;
    dst_left = 0;
    dst_top = 0;
    dst_right = height;
    dst_bottom = height;
  }
  Bitmap output = Bitmap.createBitmap(width,height,Bitmap.Config.ARGB_8888);
  Canvas canvas = new Canvas(output);
  final int color = 0xff424242;
  final Paint paint = new Paint();
  final Rect src = new Rect((int)left,(int)top,(int)right,(int)bottom);
  final Rect dst = new Rect((int)dst_left,(int)dst_top,(int)dst_right,(int)dst_bottom);
  final RectF rectF = new RectF(dst);
  paint.setAntiAlias(true);
  canvas.drawARGB(0,0);
  paint.setColor(color);
  canvas.drawRoundRect(rectF,roundPx,paint);
  paint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.SRC_IN));
  canvas.drawBitmap(bitmap,src,dst,paint);
  return output;
}

以上所述是小编给大家介绍的 Android实现个人资料页面头像背景模糊显示包括状态栏,希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对编程小技巧网站的支持

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐