温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

Android应用怎么实现一个浮动状态栏效果

发布时间:2020-11-25 16:35:11 来源:亿速云 阅读:164 作者:Leah 栏目:移动开发

这期内容当中小编将会给大家带来有关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应用怎么实现一个浮动状态栏效果了,如果刚好有类似的疑惑,不妨参照上述分析进行理解。如果想知道更多相关知识,欢迎关注亿速云行业资讯频道。

向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

AI