1

样本

如果你搜索有很多问题。答案是告诉屏幕宽度/高度。不需要!

我需要图像视图的可用空间:白色背景的汽车。如何得到它?

我不需要顶部装饰栏,底部/右侧菜单栏也不需要整个屏幕尺寸,包括时间、电池。

这是我到目前为止所尝试的:

public class MainActivity extends Activity {

    private SVGImageView svgImageView;

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

        svgImageView = new SVGImageView(this);
        svgImageView.setImageAsset("my.svg");

        // Set the ImageView as the content view for the Activity
        setContentView(svgImageView);
    }



    @Override
    protected void onPostResume() {
        super.onPostResume();
        int w = getWindow().getDecorView().getWidth();
        int h = getWindow().getDecorView().getHeight();
        Log.d("DisplayMetrics","w2:"+w+", h2:"+h);// 0 and 0
    }



    @Override
    protected void onStart() {
        super.onStart();

        DisplayMetrics metrics = getResources().getDisplayMetrics();
        int width = metrics.widthPixels;
        int height = metrics.heightPixels;
        //int bottom = svgImageView.getBottom();

        int w = svgImageView.getWidth();//0
        int h = svgImageView.getHeight();//0

        Log.d("DisplayMetrics", "width: "+width+ ", height:"+height+", w:"+w+", h:"+h);
        // landscape: 1280 x 752.  Real 1280x700
        // portrait: 800 x 1232. Real 800x1177


    }

SVGImageView 是 ImageView 的子项,如果有人有兴趣,可以在这里了解更多关于 SVG 的信息。

4

1 回答 1

0

接受的答案应该给出与此相同的宽度和高度:

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

    svgImageView = new SVGImageView(this){

        @Override
        protected void onDraw(Canvas canvas) {
            super.onDraw(canvas);
            int canvasH = canvas.getHeight();
            int canvasW = canvas.getWidth();
            Log.d("DisplayMetrics", "canvasW:"+canvasW + ", canvasH: "+canvasH);// Portrait 800 x 1176,  Landscape 1280 x 696,                  
        }

我正在检查活动源

public void setContentView(View view) {
    getWindow().setContentView(view);
}

我认为 mWindow 将具有所需的数据。问题是何时以及如何获得它?

不幸的是,这是一个抽象方法

public abstract void setContentView(View view);

但是覆盖onSizeChangedat new SVGImageView(this) 我将在那里正确地获得数字。

PhoneWindow是我的实现,现在:

// This is the top-level view of the window, containing the window decor.
private DecorView mDecor;

// This is the view in which the window contents are placed. It is either
// mDecor itself, or a child of mDecor where the contents go.
private ViewGroup mContentParent;


private final class DecorView extends FrameLayout implements RootViewSurfaceTaker

活动生命周期

我的通话记录:

myActivity.onPostResume()
myContentView.onSizeChanged()
myContentView.onDraw();

=> 需要订阅布局监听器或覆盖顶部组件onSizeChanged

于 2013-09-07T12:28:55.173 回答