Reputation: 6240
I have an image view inside view pager to display some images coming from url as shown in the screen shot below,
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?
Upvotes: 3
Views: 9814
Reputation: 4561
OK. The solution which actually works is:
ImageView imageView = new ImageView(context);
imageView.setLayoutParams(new ViewGroup.LayoutParams(mScreenWidth, ViewGroup.LayoutParams.WRAP_CONTENT));
imageView.setAdjustViewBounds(true);
imageView.setScaleType(ImageView.ScaleType.FIT_CENTER);
Drawable drawable = Drawable.createFromPath(Environment.getExternalStorageDirectory() + "/Android/data/" + getPackageName() +
"/"+....+".jpg");
if(drawable != null)
imageView.setImageDrawable(drawable);
((ViewPager) container).addView(imageView, 0);
<android.support.v4.view.ViewPager xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/view_pager"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@android:color/black"
/>
Upvotes: 0
Reputation: 445
@suresh cheemalamudi in your above code you have given a height to your linear layout where your pager is located.Instead make it match parent.And the xml code should look like this below
<android.support.v4.view.ViewPager
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/view_pager"
android:layout_width="match_parent"
android:layout_height="match_parent" />
and i your java code specify this way
imageView.setScaleType(ImageView.ScaleType.CENTER_INSIDE);
Upvotes: 1
Reputation: 3473
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
Upvotes: 6