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

 更新时间:2024年04月08日 14:23:10   作者:wangzhongITger  
这篇文章主要介绍了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实现个人资料页面头像背景模糊显示包括状态栏,希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对脚本之家网站的支持!

相关文章

最新评论