2014-08-27 17 views
-2

我想在此ImagePagerView中設置FullWidth圖像。每張圖片的全寬和高度均可在每頁中查看。請幫助。 Java代碼如下所示。任何幫助請。如何在Android中的PagerView中設置FullWidth圖像?

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> 

回答

0

例如,你可以使用:

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