Android Activity contentView大小

时间:2013-09-07 12:11:11

标签: java android android-activity

Sample

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

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

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

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

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 Here

1 个答案:

答案 0 :(得分:0)

接受的答案应该给出与此相同数量的Widh和身高:

@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,                  
        }

我正在查看Activity source

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

我认为mWindow将拥有所需的数据。问题是何时如何才能获得它?

不幸的是,这是abstract method

public abstract void setContentView(View view);

但是在新的SVGImageView(这个)上覆盖onSizeChanged我会在那里正确地使用数字。

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

Activity Lifecycle

我的通话记录:

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

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