欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  移动技术

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

程序员文章站 2023-12-15 22:52:46
最近要实现这样一个效果,然后拿出来与大家分享一下主要的几段代码,希望大家能够用到,与人方便自己方便嘛! 首先: 要实现的是浮动状态栏效果,通过在activity的o...

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

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, 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);
    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, 0, 0);
  paint.setcolor(color);
  canvas.drawroundrect(rectf, roundpx, roundpx, paint);
  paint.setxfermode(new porterduffxfermode(porterduff.mode.src_in));
  canvas.drawbitmap(bitmap, src, dst, paint);
  return output;
}

以上所述是小编给大家介绍的 android实现个人资料页面头像背景模糊显示包括状态栏,希望对大家有所帮助

上一篇:

下一篇: