的getWidth和getHeight正在返回一个零 [英] getWidth and getHeight are returning a zero

查看:112
本文介绍了的getWidth和getHeight正在返回一个零的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想使为Android一个简单的画图程序。

我有一个自定义的查看类来处理图形。当我打电话了的getWidth 的getHeight metheds,我得到一个零。

不过,绘图工作得很好,我很难code在宽度和高度,以便它的工作原理。为什么要这样做呢?


我的视图类

 公共类cDrawing扩展视图{

    焦炭的BitMap [];
    static final的短WIDTH = 160;
    static final的短HEIGHT = 440;
    静态最后的char EMPTY ='';
    INT mWidthSize;
    INT mHeightSize;

    静态最后的char RED ='R';
    诠释Ÿ;

    公共cDrawing(上下文的背景下){
        超(上下文);

        Y = 3;
        //设置我们的位图
        的BitMap =新的char [宽*高]。
        的for(int i = 0; I<宽*高;我++)
                的BitMap [我] =空;


        //返回零,为什么???????
        INT H =的getHeight();
        H = 400;
        INT W =的getWidth();
        W = 320;
        mWidthSize =瓦特/宽度;
        mHeightSize = H /高度;


        // TODO自动生成构造函数存根
    }
 


Activity类

 公共类cCanves扩展活动实现OnClickListener {
    / **第一次创建活动时调用。 * /
    @覆盖
    公共无效的onCreate(包savedInstanceState){
        super.onCreate(savedInstanceState);
        的setContentView(R.layout.canves);
        cDrawing板=新cDrawing(本);


        的LinearLayout布局=(的LinearLayout)findViewById(R.id.parent2);
        layout.addView(板);


        //设置按钮
        查看mEraser = findViewById(R.id.buteraser);
        mEraser.setOnClickListener(本);

        查看mBlack = findViewById(R.id.butblack);
        mBlack.setOnClickListener(本);

        查看mWhite = findViewById(R.id.butwhite);
        mWhite.setOnClickListener(本);

        查看MRED = findViewById(R.id.butred);
        mRed.setOnClickListener(本);

    } //函数结束

    公共无效的onClick(视图v){
        我的意图;
        开关(v.getId())
        {
        案例R.id.buteraser:
            打破;
        案例R.id.butblack:
            打破;
        案例R.id.butwhite:
            打破;
        案例R.id.butred:
            打破;
        } //结束开关

    }   // 功能
}
 


XML文件

 < XML版本=1.0编码=UTF-8&GT?;
<的LinearLayout机器人:ID =@ + ID /母
  的xmlns:机器人=htt​​p://schemas.android.com/apk/res/android
  机器人:layout_width =FILL_PARENT
  机器人:方向=垂直
  机器人:layout_height =FILL_PARENT>

    <的LinearLayout
      的xmlns:机器人=htt​​p://schemas.android.com/apk/res/android
      机器人:layout_width =WRAP_CONTENT
      机器人:方向=横向
      机器人:layout_height =WRAP_CONTENT>

    < ImageButton的机器人:ID =@ + ID / buteraser
      机器人:SRC =@可绘制/图标
      机器人:layout_width =WRAP_CONTENT
      机器人:layout_height =WRAP_CONTENT/>

    < ImageButton的机器人:ID =@ + ID / butblack
      机器人:SRC =@可绘制/图标
      机器人:layout_width =WRAP_CONTENT
      机器人:layout_height =WRAP_CONTENT/>

    < ImageButton的机器人:ID =@ + ID / butwhite
      机器人:SRC =@可绘制/图标
      机器人:layout_width =WRAP_CONTENT
      机器人:layout_height =WRAP_CONTENT/>

    < ImageButton的机器人:ID =@ + ID / butred
      机器人:SRC =@可绘制/图标
      机器人:layout_width =WRAP_CONTENT
      机器人:layout_height =WRAP_CONTENT/>
    < / LinearLayout中>

    <的LinearLayout机器人:ID =@ + ID / parent2
      机器人:layout_width =FILL_PARENT
      机器人:方向=垂直
      机器人:layout_height =FILL_PARENT>
    < / LinearLayout中>


< / LinearLayout中>
 

解决方案

的宽度和高度都没有定义,直到观点实际上是呈现在屏幕上。

使用<一个href="https://developer.android.com/reference/android/view/ViewGroup.html#onLayout%28boolean,%20int,%20int,%20int,%20int%29"相对=nofollow称号=保护抽象无效onLayout(布尔改变,诠释L,INT T,INT R,int b)在>保护抽象无效onLayout(布尔改变,诠释L,INT T,INT R,int b)在(您在您的活动覆盖),知道什么时候该尺寸已经准备就绪。

I am trying to make a simple drawing program for the android.

I have a custom View class to handle the drawing. When I call its getWidth and getHeight metheds, I get a zero.

But, the drawing works fine, I hard code in the width and height so it works. Why is it doing this?


My View class

public class cDrawing extends View{

    char BitMap[];
    static final short WIDTH=160;
    static final short HEIGHT=440;
    static final char EMPTY=' ';
    int mWidthSize;
    int mHeightSize;

    static final char RED ='R';
    int y;

    public cDrawing(Context context) {
        super(context);

        y=3;
        // set up our bitmap
        BitMap=new char[WIDTH*HEIGHT];
        for(int i=0; i<WIDTH*HEIGHT; i++)
                BitMap[i]=EMPTY;


        // returns zero why???????
        int h=getHeight();
        h=400;
        int w=getWidth();
        w=320;
        mWidthSize=w/WIDTH;
        mHeightSize=h/HEIGHT;


        // TODO Auto-generated constructor stub
    }


The Activity class

public class cCanves extends Activity  implements OnClickListener {
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.canves);
        cDrawing board=new cDrawing(this);


        LinearLayout layout = (LinearLayout)findViewById(R.id.parent2);
        layout.addView(board);


        // set up buttons
        View mEraser = findViewById(R.id.buteraser);
        mEraser.setOnClickListener(this);

        View mBlack = findViewById(R.id.butblack);
        mBlack.setOnClickListener(this);

        View mWhite = findViewById(R.id.butwhite);
        mWhite.setOnClickListener(this);

        View mRed = findViewById(R.id.butred);
        mRed.setOnClickListener(this);

    } // end function

    public void onClick(View v) {
        Intent i;
        switch(v.getId())
        {
        case R.id.buteraser:
            break;
        case R.id.butblack:
            break;
        case R.id.butwhite:
            break;
        case R.id.butred:
            break;
        } // end switch

    }   // function
}


the xml file

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout android:id="@+id/parent"
  xmlns:android="http://schemas.android.com/apk/res/android"
  android:layout_width="fill_parent"
  android:orientation="vertical"
  android:layout_height="fill_parent">

    <LinearLayout
      xmlns:android="http://schemas.android.com/apk/res/android"
      android:layout_width="wrap_content"
      android:orientation="horizontal"
      android:layout_height="wrap_content">

    <ImageButton android:id="@+id/buteraser"
      android:src="@drawable/icon"
      android:layout_width="wrap_content"
      android:layout_height="wrap_content"/>

    <ImageButton android:id="@+id/butblack"
      android:src="@drawable/icon"
      android:layout_width="wrap_content"
      android:layout_height="wrap_content"/>

    <ImageButton android:id="@+id/butwhite"
      android:src="@drawable/icon"
      android:layout_width="wrap_content"
      android:layout_height="wrap_content"/>

    <ImageButton android:id="@+id/butred"
      android:src="@drawable/icon"
      android:layout_width="wrap_content"
      android:layout_height="wrap_content"/>
    </LinearLayout>

    <LinearLayout android:id="@+id/parent2"
      android:layout_width="fill_parent"
      android:orientation="vertical"
      android:layout_height="fill_parent">
    </LinearLayout>


</LinearLayout>

解决方案

The width and height are not defined until the view is actually rendered to the screen.

Use protected abstract void onLayout (boolean changed, int l, int t, int r, int b) (which you override in your activity) to know when the sizes are ready.

这篇关于的getWidth和getHeight正在返回一个零的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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