如何在Android中获取ImageView大小?

时间:2014-06-17 16:02:44

标签: java android

我试图获得imageview大小,但它返回零。我知道它因为我在onCreate()中调用它。任何人都可以帮助我如何做到这一点,以便当我点击一个按钮时,它将显示该ImageView的大小。这是我到目前为止所尝试的......

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

            btn = (Button)findViewById(R.id.btn);
            imgView = (ImageView)findViewById(R.id.imgView);        

            btn.setOnClickListener(new OnClickListener() {

                @Override
                public void onClick(View v) {
                    getSize();              
                }
            });

        }

void getSize)
    {
        BitmapDrawable bd = (BitmapDrawable)imgView.getDrawable();
        bmp = bd.getBitmap();           
        Bitmap op = Bitmap.createBitmap(bmp, imgWidth/2, imgHeight/2, imgWidth, imgHeight);

        imgView.setImageBitmap(op);     
    }

    @Override
    public void onWindowFocusChanged(boolean hasFocus){
        imgWidth=imgView.getWidth();
        imgHeight=imgView.getHeight();
    }

更新

这就是我现在所得到的......

06-18 00:13:20.206: E/AndroidRuntime(919): FATAL EXCEPTION: main
06-18 00:13:20.206: E/AndroidRuntime(919): java.lang.IllegalArgumentException: x must be >= 0
06-18 00:13:20.206: E/AndroidRuntime(919):  at android.graphics.Bitmap.checkXYSign(Bitmap.java:225)
06-18 00:13:20.206: E/AndroidRuntime(919):  at android.graphics.Bitmap.createBitmap(Bitmap.java:495)
06-18 00:13:20.206: E/AndroidRuntime(919):  at android.graphics.Bitmap.createBitmap(Bitmap.java:471)
06-18 00:13:20.206: E/AndroidRuntime(919):  at com.example.bitmapfun.Main.Gray(Main.java:44)
06-18 00:13:20.206: E/AndroidRuntime(919):  at com.example.bitmapfun.Main$1.onClick(Main.java:34)
06-18 00:13:20.206: E/AndroidRuntime(919):  at android.view.View.performClick(View.java:3527)
06-18 00:13:20.206: E/AndroidRuntime(919):  at android.view.View$PerformClick.run(View.java:14234)
06-18 00:13:20.206: E/AndroidRuntime(919):  at android.os.Handler.handleCallback(Handler.java:605)
06-18 00:13:20.206: E/AndroidRuntime(919):  at android.os.Handler.dispatchMessage(Handler.java:92)
06-18 00:13:20.206: E/AndroidRuntime(919):  at android.os.Looper.loop(Looper.java:137)
06-18 00:13:20.206: E/AndroidRuntime(919):  at android.app.ActivityThread.main(ActivityThread.java:4441)
06-18 00:13:20.206: E/AndroidRuntime(919):  at java.lang.reflect.Method.invokeNative(Native Method)
06-18 00:13:20.206: E/AndroidRuntime(919):  at java.lang.reflect.Method.invoke(Method.java:511)
06-18 00:13:20.206: E/AndroidRuntime(919):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784)
06-18 00:13:20.206: E/AndroidRuntime(919):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551)
06-18 00:13:20.206: E/AndroidRuntime(919):  at dalvik.system.NativeStart.main(Native Method)

2 个答案:

答案 0 :(得分:1)

试试这个,它使用视图树观察器来获取宽度和高度。之前的方法不起作用,因为视图未在屏幕中绘制(布局传递不完整)。类似的东西应该有效:

final ImageView iv = (ImageView)findViewById(R.id.image);
ViewTreeObserver vto = iv.getViewTreeObserver();
vto.addOnGlobalLayoutListener(new OnGlobalLayoutListener() {
    @Override
    public void onGlobalLayout() {
       Log.d("width",""+iv.getWidth());
    }
});

答案 1 :(得分:0)

在将组件添加到父窗口之前,不会设置大小,然后窗口被打包"或布局到合适的尺寸。因此,在onCreate中,它将是0,0

在调用该函数时,您是否只能获得大小?

void getSize()
    {
      imgWidth=imgView.getWidth();
      imgHeight=imgView.getHeight();
      BitmapDrawable bd = (BitmapDrawable)imgView.getDrawable();
      bmp = bd.getBitmap();           
      Bitmap op = Bitmap.createBitmap(bmp, imgWidth/2, imgHeight/2, imgWidth, imgHeight);

      imgView.setImageBitmap(op);     
    }