Android - image view inside view pager issues

suresh cheemalamudi picture suresh cheemalamudi · Apr 30, 2013 · Viewed 9.4k times · Source

I have an image view inside view pager to display some images coming from url as shown in the screen shot below,

enter image description here However, the image should be filled( should occupy the complete view pager space). But what is happening in my case is the image is displayed in center even though when i set layout params to fill parent.

my fragment where i return layout with image view:

ImageView image = new ImageView(getActivity());

        ImageLoader imgLoader = new ImageLoader(MainActivity.context);
        imgLoader.DisplayImage(imagePath, new Activity(), image);

        LinearLayout layout = new LinearLayout(getActivity());
        layout.setLayoutParams(new LayoutParams());

        layout.setGravity(Gravity.CENTER);
        layout.addView(image);

        return layout;

view pager:

<LinearLayout
            android:layout_width="match_parent"
            android:layout_height="225dp"
            android:background="@color/white"
            android:orientation="vertical" >

            <android.support.v4.view.ViewPager
                android:id="@+id/pager"
                android:layout_width="fill_parent"
                android:layout_height="0dp"
                android:layout_weight="1" />
        </LinearLayout>

when the image is still loading from url i ll show some progress like animation and once loaded i will set the image inside imageview

if (bitmap != null) {
                imageView.setBackgroundResource(0);
                imageView.setImageBitmap(bitmap);

            } else {
                imageView.setBackgroundResource(R.drawable.view_pager_progress);
                final AnimationDrawable startAnimation = (AnimationDrawable) imageView
                        .getBackground();
                imageView.setScaleType(ScaleType.CENTER);// i tried FIT_XY, dint work
                startAnimation.start();
            }

To summarize, what i want is, the image inside view pager should fill the complete space inside view pager both horizontally and vertically. Any idea how to do this?

Answer

Eugene Popovich picture Eugene Popovich · Apr 30, 2013

Try to set ScaleType for you imageView to CENTER_CROP

imageView.setScaleType(CENTER_CROP)

Also, perhaps, you need to add some layout params when adding ImageView to LinearLayout