-1

I have a launch screen imageview in my application. I am converting png file bundled with my application to bitmap after scaling it as png is greater resolution. I am doing decoing and scaling down in callback function of that imageview when it is about to draw. I need the bitmap to fill entire screen. But Sometimes it is not getting drawn as It is getting width and height of the view as 0. It occurs sometime only. I think this is not the right way of doing it. Please suggest me best solution.

<com.example.myImageView
    android:id="@+id/ovation_background"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:contentDescription="@string/background_screen"
    android:background="@color/black"/>

In Activity onCreate function:

mImageView.getMeasuredWidth() and mImageView.getMeasuredHeight() is coming as 0 sometimes.

ViewTreeObserver vtoBackgroundView = mImageView.getViewTreeObserver();
vtoBackgroundView.addOnPreDrawListener(new ViewTreeObserver.OnPreDrawListener() {

        @Override
        public boolean onPreDraw() {
            Bitmap bitmap = decodeSampledBitmapFromResource
                            (getResources(), R.drawable.launch_screen, 
                             **mImageView.getMeasuredWidth()**, 
                             **mImageView.getMeasuredHeight()**);
            if ( bitmap != null )
            {
                mImageView.setBackgroundBitmap(bitmap);

            }
            ViewTreeObserver obs2 = mImageView.getViewTreeObserver();
            obs2.removeOnPreDrawListener(this);
            return true;
        }
    });
4

1 回答 1

1

If You getting size of Your view before it is actually draw on screen, You don't get proper size of image by getMeasuredWidth() and getMeasuredHeight(), because android don't know how big is your View based on real screen configuration.

You may consider getting real screen dimensions by getting it in onSizeChanged (int w, int h, int oldw, int oldh).

http://developer.android.com/reference/android/view/View.html#onSizeChanged(int, int, int, int)

The first call of that method give you info like this (real width of view, real height of view, 0, 0)

So the first two parameters is what I think you want get, and second two parameters in this case You may ignore.

于 2013-04-25T07:38:43.060 回答