简体   繁体   English

翻译画布和偏移操作栏

[英]translate canvas and offset actionbar

Please bear with me if this isn't clear, this is my first posting here. 如果不清楚,请多包涵,这是我在这里的第一篇文章。

I'm drawing a couple of lines on a canvas and trying to translate the canvas to centre the lines on the screen. 我在画布上画了几条线,并试图平移画布以使这些线在屏幕上居中。 The trouble is, I have an actionbar (using actionbarsherlock) which I want to exclude from the translation ie I want the top of the view to be under the action bar. 麻烦的是,我有一个操作栏(使用actionbarsherlock),我想从翻译中排除它,即我希望视图的顶部在操作栏的下面。

As it is, the data is centred vertically on the whole screen height, but I want it to be centred vertically only on the visible part of the canvas under the action bar. 照原样,数据在整个屏幕高度上垂直居中,但是我希望它仅在动作栏下方画布的可见部分上垂直居中。

Any ideas of the best way to achieve this? 对实现此目标的最佳方法有何想法?

@Override
protected void onDraw(Canvas canvas) {
    Paint paint = mPaint;

    paint.setAntiAlias(true);
    paint.setColor(Color.WHITE);
    paint.setStyle(Paint.Style.FILL);


    int centrew = canvas.getWidth()/2;
    int centreh = canvas.getHeight()/2;

    canvas.translate(centrew, centreh);
    canvas.drawLine(0, -5, 0, 5, mPaint);
    canvas.drawLine(5, 0, -5, 0, mPaint);

}

if you know the exact size of the bar, you could apply it it to the overall canvas height to increase the shift: 如果您知道钢筋的确切尺寸,可以将其应用于画布的整体高度以增加平移:

int maxV = canvas.getHeight() + barHeight;
int centreh = maxV/2;

I am assuming, the bar is on top, covering apart of the canvas, so the shift (from the top left corner) has to be greater. 我假设酒吧在顶部,覆盖了画布的一部分,因此(从左上角开始)移位必须更大。 If the bar is on the bottom, the shift should be reduced: 如果栏位于底部,则应减少偏移:

int maxV = canvas.getHeight() - barHeight;
int centreh = maxV/2;

EDIT: If the bar has a different size in pixels on different devices, you have to hack a bit to make it work. 编辑:如果栏在不同设备上的像素大小不同,则必须进行修改以使其工作。 You can safely make assuptions about the bar and revise them in case they don't hold. 您可以放心地对酒吧进行假设,并在不举行的情况下对其进行修改。 You could assume a certain ratio reserved for the bar and adapt the absolute pixel count accordingly (the oneliner is rather for illustration): 您可以假定为条形保留一定的比率,并相应地调整绝对像素数(oneliner只是用于说明):

int maxV = Math.round(canvas.getHeight()*(1f + barRatio)); 

You can further adapt the ratio depending on the aspect ratio of the screen to make it work even better. 您可以根据屏幕的高宽比进一步调整比例,以使其更好地工作。

Unfortunately there is no really clean solution as you cannot read the current layout and extract sizes. 不幸的是,没有真正干净的解决方案,因为您无法阅读当前的布局并提取尺寸。

The solution I used was as described here : 我使用的解决方案如下所述:

http://developer.android.com/resources/samples/HoneycombGallery/src/com/example/android/hcgallery/TitlesFragment.html http://developer.android.com/resources/samples/HoneycombGallery/src/com/example/android/hcgallery/TitlesFragment.html

Basically my activity has the following layout listener defined: 基本上,我的活动定义了以下布局侦听器:

ViewTreeObserver.OnGlobalLayoutListener layoutListener = new ViewTreeObserver.OnGlobalLayoutListener() {
    @Override
    public void onGlobalLayout() {
        mmv.setAbShift(MunroBag.this.actionBar.getHeight());
    }
};  

Then the view class has the following method defined: 然后,视图类定义了以下方法:

    public void setAbShift(int shift)
    {
        abShift = shift;
        this.invalidate();
    }

So onDraw gets called and the shift is applied to the view depending on the actionBar height. 因此onDraw被调用,并且根据actionBar的高度将平移应用于视图。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM