为什么帆布是不一样的尺寸视图的OnDraw? (安卓) [英] Why canvas is not the same size as view in onDraw? (Android)

查看:141
本文介绍了为什么帆布是不一样的尺寸视图的OnDraw? (安卓)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试着用这种

@Override
protected void onDraw(Canvas canvas)
{
    Log.e("TEST", "canvas width: " + canvas.getWidth() + "");
    Log.e("TEST", "view width: " + this.getWidth() + "");

    Log.e("TEST", "canvas height: " + canvas.getHeight() + "");
    Log.e("TEST", "view height: " + this.getHeight() + "");
    super.onDraw(canvas);
    Log.e("TEST", "canvas width: " + canvas.getWidth() + "");
    Log.e("TEST", "view width: " + this.getWidth() + "");

    Log.e("TEST", "canvas height: " + canvas.getHeight() + "");
    Log.e("TEST", "view height: " + this.getHeight() + "");
}

这是结果:

canvas width: 320
view width: 480
canvas height: 533
view height: 300
canvas width: 320
view width: 480
canvas height: 533
view height: 300

这是我的main.xml中部分

Part from my main.xml

    android:layout_width="480px"
    android:layout_height="300px"

据我的日志打印和我的XML的一部分,视大小是正确的。 我的设备是480×800和宽度和高度由日志结果除以得到1.5(五百三十三分之八百= 1.5和三百二十〇分之四百八十= 1.5),这样看来我得到的画布,从整个屏幕上绘制。这是为什么,为什么会这样小,我该怎么办正常的方式提请?

According to my Log prints and part of my xml, size of view is correct. My device is 480x800 and both width and height divided by Log results give 1.5 (800/533=1.5 and 480/320=1.5), so it seems I get Canvas from entire screen to draw on. Why is this, and why is it so small and what should I do to draw in normal way?

推荐答案

SuitUp,

视图管理画布对象将可能永远是从你的屏幕大小不同。这在很大程度上取决于要从中使用上述逻辑对象。这样做的原因是,Android的创建了一个新的画布或重用旧,视乎你的父母和孩子都在处理它被调用的循环渲染和地点。东西的时候你重写的行为和动态改变的意见(特别是如果你这样做的布局/措施/抽奖周期内得到更奇怪的。

The view-managed canvas object will likely always be different from your screen size. A lot depends on from which object you are using the above logic. The reason for this is that Android creates a new canvas or reuses the old one depending on how your parents and children are rendered and where in the cycle of processing it is being called. Things get even stranger when you override the behavior and change the views dynamically (especially if you do so DURING the layout/measure/draw cycle.

这是为什么,为什么它这么小......

Why is this, and why is it so small...

的画布的大小是基于所述对象,它是在布局的参数。如果重写onMeasure或onLayout,画布将什么是要调整仅重新调整。如果您有问题画布大小,你真的想看看你的布局参数为您绘制在视图中,有时会更大,有时会更小。它只会永远是相同的大小作为一个顶级查看/ ViewGroup中,有屏幕 match_parent (或 FILL_PARENT )同时为 layout_width layout_height 的一个活动,就是全屏(无通知栏),而不是使用 Theme.Dialog 的风格。

The canvas's size is based on the layout parameters of the object that it is in. If you override onMeasure or onLayout, the canvas will readjust only what is necessary to readjust. If you are having problems with the canvas size, you really want to look at your layout parameters for the view that you are drawing in. Sometimes it will be bigger and sometimes it will be smaller. It will only ever be the same size as the screen in a top level View/ViewGroup that has match_parent (or fill_parent) for both the layout_width and layout_height in an Activity that is fullscreen (no notification bar) and not using the Theme.Dialog style.

......我该怎么做正常的方式提请?

... what should I do to draw in normal way?

这取决于你用​​正常的意思是,你的需求是什么。如果你想手动管理整个UI,那么你必须采取的顶层对象的控制权。这通常是通过扩展顶级视图,以便您可以重写的行为做了。发射做到这一点通过覆盖顶层的FrameLayout然后将其子视图。你要知道,这可能是昂贵的,你可以借鉴的东西从屏幕,如果你有一个足够大的画布。

That depends on what you mean by normal and what your needs are. If you are trying to manage the entire UI manually, then you have to take control of the top level object. This is normally done by extending the top-level view so that you can override the behavior. Launchers do this by overriding the top-level FrameLayout and then its child views. Mind you, this can be costly as you CAN draw things off of the screen if you have a big enough canvas.

大多数程序利用孩子的意见和简单的覆盖,他们画的方式,了解该子视图不应该控制的方式,一个父视图绘制。在这种情况下,在画布小而有限的,但它优化了的逻辑和允许对象移动相应地,同时保持稳定的渲染。

MOST programs utilize child Views and simply override the way that they draw, understanding that a child View should not control the way that a parent View draws. In this case, the canvas is small and limited, but it optimizes the logic and allows the object to moved accordingly while keeping the rendering stable.

游戏使用全屏 SurfaceView ,以实现他们的需求。这提供了非常灵活的画布上手动和不断变化的presentation优先。有许多的例子和教程显示如何做到这一点。在谷歌 SurfaceView 简单地进行搜索。

Games make use of a fullscreen SurfaceView in order to accomplish their needs. This provides a very flexible canvas with prioritization on the manual and ever-changing presentation. There are a number of examples and tutorials showing how to do this. Simply search in Google for SurfaceView.

在一般情况下,对于大多数视图,缺省呈现和绘制方法,可以用在其面向对象的性质。适当利用的功能,如onMeasure和onLayout经常达到了预期的效果。不知道更多有关您的特定需求,这是可以得到的最好建议。

In general, for most Views, the default rendering and drawing methods can be utilized in their object oriented nature. Properly utilizing functions like onMeasure and onLayout often achieves the desired results. Without knowing more about your particular needs, this is the best advice that can be given.

详细信息(不确定相关性)

了解Android的如何呈现视图可对于实现理想的结果。这么多已经进入它必须考虑多个屏幕和不同的方式来呈现给许多不同的参数相同的视图引擎。我会研究如何利用布局参数,以减少工作,你所要做的金额。此外,还存在大量的上onMeasure,onLayout和OnDraw中之间的关系的信息。在许多情况下,简单地将合适的参数,以正确的意见将在控制,你必须采取以正确地呈现你的对象数额的巨大差异。

Understanding how Android renders Views can be crucial to achieving desirable results. So much has gone into the engine that it must account for multiple screens, and different ways to render the same View given many different parameters. I would research how to utilize Layout parameters to cut down on the amount of work you have to do. Further, there is a lot of information on the relationships between onMeasure, onLayout and onDraw. In many circumstances simply applying the right parameters to the correct Views will make a huge difference in the amount of control that you have to take in order to render your object correctly.

希望这有助于

FuzzicalLogic

FuzzicalLogic

这篇关于为什么帆布是不一样的尺寸视图的OnDraw? (安卓)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆