Reputation: 674
Im trying to get imageview size but its returning zero. i know its because im calling it in onCreate(). can anyone help me how to do this so that when I will click a button it will show me the size of that ImageView. here what i have tried so far...
@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();
}
UPDATE
This is what I am getting right now...
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)
Upvotes: 0
Views: 210
Reputation: 7026
The size isn't set up until the component is added to the parent window, and then the window is "packed" or laid out to the right size. So in the onCreate
, it will be 0,0
Can't you just get the size when the function is called?
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);
}
Upvotes: 0
Reputation: 10348
Try this it uses view tree observer for getting the width and height. The earlier methods didnot work because the view was not drawn in the screen(layout pass incomplete). Something similar should work:
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());
}
});
Upvotes: 1