如何在Android中的IMageView中设置全宽图像?

时间:2014-08-27 20:35:30

标签: java android android-layout android-fragments

我想在此ImagePagerView中设置FullWidth Image。每个图像都将是全宽和高度,以便在每个页面中查看。任何帮助请。 Java代码如下。任何帮助请。

package com.exampe.imagepager;

public class FragmentImageView extends Fragment {
package com.exampe.imagepager;

public class FragmentImageView extends Fragment {

private Integer itemData;
private Bitmap myBitmap;
public ProgressDialog pd;
private ImageView ivImage;

public static FragmentImageView newInstance() {
    FragmentImageView f = new FragmentImageView();
    return f;
}

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
}

@SuppressWarnings("deprecation")
@Override
public View onCreateView

(LayoutInflater inflater, ViewGroup container,Bundle savedInstanceState) 

{

    View root = inflater.inflate(R.layout.imageview, container, false);

    ivImage = (ImageView) root.findViewById(R.id.ivImageView);

    setImageInViewPager();

    return root;
}

public void setImageList(Integer integer) {
    this.itemData = integer;
}

public void setImageInViewPager() {

    try {
        BitmapFactory.Options options = new BitmapFactory.Options();
        options.inJustDecodeBounds = true;
        myBitmap = BitmapFactory.decodeResource(getResources(), itemData,
                options);
        if (options.outWidth > 3000 || options.outHeight > 2000) {
            options.inSampleSize = 4;
        } else if (options.outWidth > 2000 || options.outHeight > 1500) {
            options.inSampleSize = 3;
        } else if (options.outWidth > 1000 || options.outHeight > 1000) {
            options.inSampleSize = 2;
        }
        options.inJustDecodeBounds = false;
myBitmap = BitmapFactory.decodeResource(getResources(), itemData,options);

if (myBitmap != null) {
            try {
                if (ivImage != null) {
                    ivImage.setImageBitmap(myBitmap);
                }
            } 

            catch (Exception e) {
                e.printStackTrace();
            }
        }
    } 
    catch (OutOfMemoryError e) {
        e.printStackTrace();
        System.gc();
    }
}

@Override
public void onDestroyView() {
    super.onDestroyView();
    if (myBitmap != null) {
        myBitmap.recycle();
        myBitmap = null;
    }


 }
}

Xml代码

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/mainview"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:gravity="center"
 >

 <ImageView
  android:id="@+id/ivImageView"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent" />

 </LinearLayout>

1 个答案:

答案 0 :(得分:0)

您可以使用:

<ImageView
    android:id="@+id/ivImageView"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:scaleType="centerCrop" />