2015-12-11 32 views
-1

我想从可绘制到gridview设置大图像。我已经完成了这个代码,但我得到了错误。我怎样才能把可绘制的大图像到android的gridview?

这是我的片段布局名称color_fragment.xml

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:orientation="vertical" 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent" 
    android:background="@color/black_overlay"> 
    <GridView 
     android:id="@+id/gridview" 
     android:layout_width="fill_parent" 
     android:layout_height="fill_parent" 
     android:numColumns="3" 
     android:columnWidth="90dp" 
     android:horizontalSpacing="10dp" 
     android:verticalSpacing="10dp" 
     android:gravity="center" 
     android:stretchMode="columnWidth" > 
    </GridView> 
</LinearLayout> 

这里,是我的ColorFragment.java其中充气上述布局

public class ColorFragment extends Fragment { 
    View view; 
    @Override 
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { 
     view=inflater.inflate(R.layout.watercolor_fragment, container, false); 
     GridView gridView=(GridView)view.findViewById(R.id.gridview); 
     gridView.setAdapter(new ImageAdapter(view.getContext())); 
     return view; 
    } 
} 

这里,是我的ImageAdapter

public class ImageAdapter extends BaseAdapter { 

    private Context context; 

    private Integer[] mThumbIds={R.drawable.natureone,R.drawable.naturetwo, 
           R.drawable.naturethree,R.drawable.naturefour, 
           R.drawable.naturefive,R.drawable.naturesix, 
           R.drawable.natureseven,R.drawable.natureight, 
      R.drawable.naturenine,R.drawable.natureten,R.drawable.natureleven,R.drawable.naturetwelve, 
      R.drawable.naturethrtineen,R.drawable.naturefourthyeen,R.drawable.naturefifteen,R.drawable.naturesixteen, 
      R.drawable.natureseventeen,R.drawable.natureone}; 
    public ImageAdapter(Context context){ 
     this.context=context; 
    } 
    @Override 
    public int getCount() { 
     return mThumbIds.length; 
    } 

    @Override 
    public Object getItem(int position) { 
     return null; 
    } 

    @Override 
    public long getItemId(int position) { 
     return 0; 
    } 

    @Override 
    public View getView(int position, View convertView, ViewGroup parent) { 
     ImageView imageView; 
     if(convertView==null){ 
      imageView=new ImageView(this.context); 
      imageView.setLayoutParams(new GridView.LayoutParams(85,85)); 
      imageView.setScaleType(ImageView.ScaleType.CENTER_CROP); 
      imageView.setPadding(8,8,8,8); 
     } 
     else{ 
      imageView=(ImageView)convertView; 
     } 
     imageView.setImageResource(mThumbIds[position]);//Got Error here 
     return imageView; 
    } 
} 

在上面的代码中,所有的图像都是高清的,它已经存在于xhdpi文件夹中。

我得到了错误

java.lang.OutOfMemoryError: Failed to allocate a 4798092 byte allocation with 94076 free bytes and 91KB until OOM 
                at dalvik.system.VMRuntime.newNonMovableArray(Native Method) 
                at android.graphics.BitmapFactory.nativeDecodeAsset(Native Method) 
                at android.graphics.BitmapFactory.decodeStream(BitmapFactory.java:609) 
                at android.graphics.BitmapFactory.decodeResourceStream(BitmapFactory.java:444) 
                at android.graphics.drawable.Drawable.createFromResourceStream(Drawable.java:973) 
                at android.content.res.Resources.loadDrawableForCookie(Resources.java:2423) 
                at android.content.res.Resources.loadDrawable(Resources.java:2330) 
                at android.content.res.Resources.getDrawable(Resources.java:758) 
                at android.content.Context.getDrawable(Context.java:402) 
                at android.widget.ImageView.resolveUri(ImageView.java:741) 
                at android.widget.ImageView.setImageResource(ImageView.java:397) 
                at com.domore.angelnx.Adapter.ImageAdapter.getView(ImageAdapter.java:63) 
                at android.widget.AbsListView.obtainView(AbsListView.java:2344) 
                at android.widget.GridView.makeAndAddView(GridView.java:1433) 
                at android.widget.GridView.makeRow(GridView.java:361) 
                at android.widget.GridView.fillDown(GridView.java:302) 
                at android.widget.GridView.fillFromTop(GridView.java:437) 
                at android.widget.GridView.layoutChildren(GridView.java:1276) 
                at android.widget.AbsListView.onLayout(AbsListView.java:2148) 
                at android.view.View.layout(View.java:15596) 
                at android.view.ViewGroup.layout(ViewGroup.java:4966) 
                at android.widget.LinearLayout.setChildFrame(LinearLayout.java:1703) 
                at android.widget.LinearLayout.layoutVertical(LinearLayout.java:1557) 
                at android.widget.LinearLayout.onLayout(LinearLayout.java:1466) 
                at android.view.View.layout(View.java:15596) 
                at android.view.ViewGroup.layout(ViewGroup.java:4966) 
                at android.support.v4.view.ViewPager.onLayout(ViewPager.java:1627) 
                at android.view.View.layout(View.java:15596) 
                at android.view.ViewGroup.layout(ViewGroup.java:4966) 
                at android.widget.LinearLayout.setChildFrame(LinearLayout.java:1703) 
                at android.widget.LinearLayout.layoutVertical(LinearLayout.java:1557) 
                at android.widget.LinearLayout.onLayout(LinearLayout.java:1466) 
                at android.view.View.layout(View.java:15596) 
                at android.view.ViewGroup.layout(ViewGroup.java:4966) 
                at android.widget.LinearLayout.setChildFrame(LinearLayout.java:1703) 
                at android.widget.LinearLayout.layoutVertical(LinearLayout.java:1557) 
                at android.widget.LinearLayout.onLayout(LinearLayout.java:1466) 
                at android.view.View.layout(View.java:15596) 
                at android.view.ViewGroup.layout(ViewGroup.java:4966) 
                at android.widget.FrameLayout.layoutChildren(FrameLayout.java:573) 
                at android.widget.FrameLayout.onLayout(FrameLayout.java:508) 
                at android.view.View.layout(View.java:15596) 
                at android.view.ViewGroup.layout(ViewGroup.java:4966) 
                at android.support.v7.widget.ActionBarOverlayLayout.onLayout(ActionBarOverlayLayout.java:435) 
                at android.view.View.layout(View.java:15596) 
                at android.view.ViewGroup.layout(ViewGroup.java:4966) 
                at android.widget.FrameLayout.layoutChildren(FrameLayout.java:573) 
                at android.widget.FrameLayout.onLayout(FrameLayout.java:508) 
                at android.view.View.layout(View.java:15596) 
                at android.view.ViewGroup.layout(ViewGroup.java:4966) 
                at android.widget.LinearLayout.setChildFrame(LinearLayout.java:1703) 
                at android.widget.LinearLayout.layoutVertical(LinearLayout.java:1557) 
                at android.widget.LinearLayout.onLayout(LinearLayout.java:1466) 
                at android.view.View.layout(View.java:15596) 
                at android.view.ViewGroup.layout(ViewGroup.java:4966) 
                at android.widget.FrameLayout.layoutChildren(FrameLayout.java:573) 
                at android.widget.FrameLayout.onLayout(FrameLayout.java:508) 
                at android.view.View.layout(View.java:15596) 
                at android.view.ViewGroup.layout(ViewGroup.java:4966) 
                at android.view.ViewRootImpl.performLayout(ViewRootImpl.java:2072) 
                at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1829) 
                at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1054) 
                at android.view.ViewRootImpl$Tra 

如何解决这个问题? 我的问题是为什么我得到OOM错误?请帮助我解决这个错误。

+0

您的图片可能是高分辨率图片。 – Piyush

+0

你想调整图像位图吗? –

+0

如何使用位图调整图像大小以及如何放入imageView? –

回答

0

这将调整图像Bitmap。 用于从drawable获取图像位图。

Bitmap bitmap = BitmapFactory.decodeResource(getResources(),R.drawable.yourImageName); 

之后,重新大小,该图像位图使用下面的代码。

int height = (bitmap.getHeight() * 512/bitmap.getWidth()); 
Bitmap scale = Bitmap.createScaledBitmap(bitmap, 512,height, true); 

创建图像位图后,使用下面的代码设置该位图。

imageview.setImageBitmap(scale); 
+0

但我有一个图像阵列。我不是在谈论单个图像。 –

+0

如何在片段中调用 –

+0

什么是getResources()? –

0

四件事情,我需要在ImageAdpter.java

添加
Bitmap bitmap=BitmapFactory.decodeResource(context.getResources(),mThumbIds[position]); 
     int height = (bitmap.getHeight() * 512/bitmap.getWidth()); 
     Bitmap scale = Bitmap.createScaledBitmap(bitmap, 512,height, true); 
     imageView.setImageBitmap(scale); 

这条线之下,从我的岗位在ImageAdapter.java

imageview.setImageResources(mThumbIds[position]);//Remove this line 

因为,我需要调整//删除所有的图像。位图提供了一种调整图像大小和存储到imageview的方法。

相关问题