【问题标题】:Translucent gradient status bar on Android Lollipop presented in Material DesignMaterial Design 中呈现的 Android Lollipop 上的半透明渐变状态栏
【发布时间】:2016-01-11 01:33:50
【问题描述】:

我想做这样的东西

适用于 Android 5.0 及更高版本?

我该如何实现呢?我在 StackOverFlow 或 android 开发者网站上找不到任何解决方案。

我建议我可以使状态栏透明并在状态栏下绘制渐变绘制。但问题很少。

第一个问题是图形绘制的通常渐变不支持材料设计规范http://www.google.com/design/spec/style/imagery.html

第二个问题是我无法通过android:fitsSystemWindows="true" 将地图片段放入窗口。

【问题讨论】:

  • 如果您否定我的问题,请描述在 StackOverFlow 中发布的任何解决方案,或在 cmets 中描述我的问题中的问题。
  • 使用 9-patch 绘制渐变不是问题
  • 你能告诉我们你已经尝试过什么(代码),你得到了什么以及你期望得到什么?
  • 有任何解决方案吗?卡在同样的问题上,并没有找到任何方法来做到这一点。
  • 您只是希望渐变位于工具栏/操作栏的顶部还是应该向下延伸更远?

标签: android material-design android-appbarlayout


【解决方案1】:

与 Material Design 网站上显示的图大致相同的公式是:

y = 3/(4*(x+0.5)) - 0.5

我尝试了几种通过 Canvas 绘制双曲面渐变的方法,并找到了最快的解决方案。

public class HyperbolaGradientDrawable extends Drawable {

private static final int ALPHA_DEFAULT = (int) (0.6f * 255);

private int mAlpha = ALPHA_DEFAULT;
private int mColor;
private Rect  mBmpRect = new Rect();
private int[] mColors  = new int[0];
private Bitmap mBmp;

@Override
public void draw(Canvas canvas) {
    Rect bounds = getBounds();
    if (mColors.length != bounds.height()) {
        int alpha;
        float y, alphaRelative;
        mColors = new int[bounds.height()];
        for (int i = 0; i < bounds.height(); i++) {
            y = ((float) i) / bounds.height();
            // this function gives approximately 0.5 of the bearing alpha at 3/10ths closed to the darker end
            alphaRelative = 3 / (4 * (y + 0.5f)) - 0.5f;
            alpha = (int) (alphaRelative * mAlpha);
            mColors[i] = alpha << 24 | mColor;
        }
        mBmp = Bitmap.createBitmap(mColors, 1, bounds.height(), Bitmap.Config.ARGB_8888);
        mBmpRect.set(0, 0, 1, bounds.height());
    }
    canvas.drawBitmap(mBmp, mBmpRect, bounds, null);
}

public void setColor(int color) {
    // remove alpha chanel
    mColor = color & 0x00FFFFFF;
}

@Override
public void setAlpha(int alpha) {
    mAlpha = alpha;
}

@Override
public void setColorFilter(ColorFilter colorFilter) {

}

@Override
public int getOpacity() {
    return PixelFormat.TRANSLUCENT;
}
}

我知道 Google 建议不要在 draw 方法中创建新对象,但它比通过 Canvas 逐行绘制更快。

你可以在demo project看几种方式的比较

【讨论】:

    猜你喜欢
    • 2015-08-06
    • 1970-01-01
    • 2017-07-07
    • 2015-04-07
    • 1970-01-01
    • 1970-01-01
    • 2015-11-03
    • 2020-05-20
    • 2021-10-24
    相关资源
    最近更新 更多