【问题标题】:Android Custom Bar: draw over status barAndroid自定义栏:绘制状态栏
【发布时间】:2016-04-26 10:10:18
【问题描述】:

Picture link

Video link


问题:
当我在活动中使用我的自定义视图时。然后我更改自定义视图的翻译。然后,自定义视图绘制在状态栏上。


当我删除代码时, canvas.clipRect(0, 0, getWidth(), getHeight(), Region.Op.REPLACE); in CustomView.java,就可以了。
但在实际情况下,我需要clipRect函数来限制。那么,如何计算剪辑范围。

请帮我解决它。

布局文件:

<RelativeLayout
    android:id="@+id/action_bar"
    android:layout_width="match_parent"
    android:layout_height="40dp"
    android:background="#00e0e0">

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerInParent="true"
        android:text="Test Title" />

    <TextView
        android:id="@+id/text"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentRight="true"
        android:layout_centerVertical="true"
        android:text="up"
        android:textSize="32dp" />

</RelativeLayout>

<cn.bangnijiao.testunitdemo.CustomView
    android:id="@+id/counterView"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_below="@id/action_bar"
    android:layout_centerHorizontal="true" />

代码:

public class YActivity extends Activity {

TextView up;
CustomView customView;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_y);

    up = (TextView) findViewById(R.id.text);
    customView = (CustomView) findViewById(R.id.counterView);

    up.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            customView.setTranslationY(customView.getTranslationY() - 20);
        }
    });

    findViewById(R.id.action_bar).bringToFront();
}

}

自定义查看代码:

public class CustomView extends View {

private Paint mPaint;
int size = 120;

public CustomView(Context context, AttributeSet attrs) {
    super(context, attrs);
    mPaint = new Paint(Paint.ANTI_ALIAS_FLAG);
}

@Override
protected void onDraw(Canvas canvas) {
    super.onDraw(canvas);
    drawSeparatorsAndBackground(canvas);
}

private void drawSeparatorsAndBackground(Canvas canvas) {

    mPaint.setColor(Color.YELLOW);
    canvas.drawRect(0, 0, getWidth(), getHeight(), mPaint);

    canvas.clipRect(0, 0, getWidth(), getHeight(), Region.Op.REPLACE);

    mPaint.setColor(Color.BLACK);

    int verticalCount = (getWidth() / size);
    float[] verticalLines = new float[verticalCount * 4];

    for (int i = 0; i < verticalCount; i++) {
        verticalLines[i * 4 + 0] = i * size;
        verticalLines[i * 4 + 1] = 0;
        verticalLines[i * 4 + 2] = i * size;
        verticalLines[i * 4 + 3] = getHeight();
    }
    canvas.drawLines(verticalLines, mPaint);

    int hourHorizontalCount = (getHeight() / size);
    float[] hourHorizontalLines = new float[hourHorizontalCount * 4];

    for (int i = 0; i < hourHorizontalCount; i++) {
        hourHorizontalLines[i * 4 + 0] = 0;
        hourHorizontalLines[i * 4 + 1] = i * size;
        hourHorizontalLines[i * 4 + 2] = getWidth();
        hourHorizontalLines[i * 4 + 3] = i * size;
    }
    canvas.drawLines(hourHorizontalLines, mPaint);
}

}

【问题讨论】:

    标签: android custom-view android-statusbar


    【解决方案1】:

    试试 canvas.save() 和 canvas.restore()

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        int saveCount = canvas.save();
        drawSeparatorsAndBackground(canvas);
        canvas.restoreToCount(saveCount);
    }
    

    【讨论】:

    • 谢谢,但没有效果
    • @Kevinsu917 我运行你的代码没有在状态栏上绘制,可能是你的样式有 true 和 ActionBar 设置 fitSystemWindows=true?
    • 只是发生在某些手机上。例如三星 Galaxy S6-5.1.0-API 22。您可以通过 Genymotion 模拟器尝试。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-05-11
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多