在API 16之前获取Canvas的大小

时间:2015-08-19 21:45:03

标签: android

文档说明 Canvas.getHeight()

  

返回当前绘图层的高度

这与API的情况一样吗? 16?

示例:

import android.app.ActionBar;
import android.app.Activity;
import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.ViewGroup;
import android.widget.FrameLayout;

class MyView extends View {
    Rect r = new Rect();
    public MyView(Context context) {
        super(context);
        FrameLayout.LayoutParams params = new FrameLayout.LayoutParams(20, 40);
        params.leftMargin = 50;
        params.topMargin = 50;
        setLayoutParams(params);
    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        canvas.drawColor(Color.rgb(255, 0, 0));
        Log.d("me", "canvas.getHeight = " + canvas.getHeight());
        Log.d("me", "canvas.getWidth = " + canvas.getWidth());
        canvas.getClipBounds(r);
        Log.d("me", ".getClipBounds.height() = " + r.height());
        Log.d("me", ".getClipBounds.widht() = " + r.width());
    }
}

public class MainActivity extends Activity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        FrameLayout container = new FrameLayout(this);
        container.setLayoutParams(new ViewGroup.LayoutParams(
                ViewGroup.LayoutParams.MATCH_PARENT,
                ViewGroup.LayoutParams.MATCH_PARENT));
        container.addView(new MyView(this));
        setContentView(container);
    }
}

API> = 16:

如果我使用的是像Galaxy Nexus这样的模拟器,API 16或更高版本 canvas.getHeight()会按预期返回40:

D/me﹕ canvas.getHeight = 40
D/me﹕ canvas.getWidth = 20
D/me﹕ .getClipBounds.height() = 40
D/me﹕ .getClipBounds.widht() = 20

API< 16:

但如果我使用的是像Galaxy Nexus这样的模拟器,使用的是API 15或更低版本(也尝试使用真实设备和API 7):

D/me﹕ canvas.getHeight = 1280
D/me﹕ canvas.getWidth = 720
D/me﹕ .getClipBounds.height() = 40
D/me﹕ .getClipBounds.widht() = 20

问题:

为什么结果不同?我在文档中找不到任何提示。

0 个答案:

没有答案