png加载失去内存android(the png loading get out of memory android)

编程入门 行业动态 更新时间:2024-10-25 06:28:42
png加载失去内存android(the png loading get out of memory android)

我有一个有网格视图的片段。我在网格视图中放置了70个图像。但是当我运行应用程序时它会从内存中解脱出来。解决它吗?我应该将所有图像转换为位图吗? 这是我的片段类:

public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { View rootView = inflater.inflate(R.layout.fragment_games, container, false); mDrawerList = (GridView) rootView.findViewById(R.id.list_slidermenu); icons=new int[50]; for(int i=1 ;i<icons.length ;i++) { icon_id="@drawable/data_" + i; icons[i]=getResources().getIdentifier(icon_id, null, MainActivity.PACKAGE_NAME); Log.e("log",""+icons[i]); } navDrawerItems = new ArrayList<NavDrawerItem>(); for(int i=0 ;i<icons.length ;i++) { navDrawerItems.add(new NavDrawerItem(icons[i])); } adapter = new NavDrawerListAdapter(getActivity(), navDrawerItems); mDrawerList.setAdapter(adapter); return rootView; }

这是我的适配器类:

public View getView(int position, View convertView, ViewGroup parent) { if (convertView == null) { LayoutInflater mInflater = (LayoutInflater) context.getSystemService(Activity.LAYOUT_INFLATER_SERVICE); convertView = mInflater.inflate(R.layout.itemlist, null); } ImageView imgIcon = (ImageView) convertView.findViewById(R.id.icon); imgIcon.setImageResource(navDrawerItems.get(position).getIcon()); // displaying count // check whether it set visible or not return convertView; }

I have a fragment that have a grid view.I'm putting 70 images in grid view.but when i run the app it get out of memory.how o solve it?should i convert all images to bitmap? this is my fragment class:

public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { View rootView = inflater.inflate(R.layout.fragment_games, container, false); mDrawerList = (GridView) rootView.findViewById(R.id.list_slidermenu); icons=new int[50]; for(int i=1 ;i<icons.length ;i++) { icon_id="@drawable/data_" + i; icons[i]=getResources().getIdentifier(icon_id, null, MainActivity.PACKAGE_NAME); Log.e("log",""+icons[i]); } navDrawerItems = new ArrayList<NavDrawerItem>(); for(int i=0 ;i<icons.length ;i++) { navDrawerItems.add(new NavDrawerItem(icons[i])); } adapter = new NavDrawerListAdapter(getActivity(), navDrawerItems); mDrawerList.setAdapter(adapter); return rootView; }

and this is my adapter class:

public View getView(int position, View convertView, ViewGroup parent) { if (convertView == null) { LayoutInflater mInflater = (LayoutInflater) context.getSystemService(Activity.LAYOUT_INFLATER_SERVICE); convertView = mInflater.inflate(R.layout.itemlist, null); } ImageView imgIcon = (ImageView) convertView.findViewById(R.id.icon); imgIcon.setImageResource(navDrawerItems.get(position).getIcon()); // displaying count // check whether it set visible or not return convertView; }

最满意答案

你必须在这里考虑几个事实。

如果70张图片给你一个OOM例外,那意味着你的图片必须更大。 但大多数时候,你不需要图像。 您的图像为1024 * 1024像素,但您可以在100 * 100比例图像视图中显示它。 在这种情况下,你必须找到所需的大小,并缩小图像,它不会浪费堆,图像也不会像素化。 在这里阅读

您必须使用某种加载技术,例如进行加载,而不是一次性加载所有70到网格中。 然后只有用户滚动网格时才会加载图像。 它不会浪费记忆。

对于给定的特定时刻,您已取消分配当前未显示在网格上的所有图像。

您必须为应用程序设置大堆,但不建议使用,但必须在这些类型的应用程序中使用。

You have to consider about several facts here .

If 70 images gave you a OOM exception, that means your images are must larger in size. But most of the time, you do not need the image as it is. Your image is 1024 * 1024 pixels, but you show it within 100 * 100 scale image view. In such cases you have to find the required size and scale down the image where it doesnt waste heap and also image doesnt get pixalated. read here

You have to use some kind of loading technique such as progressing loading instead of loading all 70 at once into the grid. Then images will be load when only user scroll the grid. It will not waste the memory.

You have de-allocate all the images which are not currently shown on the grid for a given particular moment.

You have to set the large heap for the app though it is not recommended but have to use in these kind of apps.

更多推荐

本文发布于:2023-08-07 11:13:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1464210.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:加载   内存   png   memory   loading

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!