Android百日程序:高效载入大图片
来源:互联网 发布:山东seo网站推广 编辑:程序博客网 时间:2024/04/30 03:33
问题:如果图片很大,全部载入内存,而显示屏又不大,那么再大的图片也不会提高视觉效果的,而且会消耗无谓的内存。
解决办法就是根据实际需要多大的图片,然后动态计算应该载入多大的图片;但是因为不太可能图片大小和实际需要的大小一致,故此需要载入图片大小为一个2的某次方的值,而大于实际需要的大小。
如图,载入一个微缩图大小为100*100
新建一个项目,
参考Google上的方法:http://developer.android.com/training/displaying-bitmaps/load-bitmap.html#load-bitmap
建立一个类,以便调用其中的函数处理图片资源,全部代码如下:
package bill.su.loadbitmap;import android.content.res.Resources;import android.graphics.Bitmap;import android.graphics.BitmapFactory;public class BitmapUtils {public static int calculateInSampleSize(BitmapFactory.Options options,int reqWidth, int reqHeight) {// Raw height and width of imagefinal int height = options.outHeight;final int width = options.outWidth;int inSampleSize = 1;if (height > reqHeight || width > reqWidth) {final int halfHeight = height / 2;final int halfWidth = width / 2;// Calculate the largest inSampleSize value that is a power of 2 and// keeps both// height and width larger than the requested height and width.while ((halfHeight / inSampleSize) > reqHeight&& (halfWidth / inSampleSize) > reqWidth) {inSampleSize *= 2;}}return inSampleSize;}public static Bitmap decodeSampledBitmapFromResource(Resources res,int resId, int reqWidth, int reqHeight) {// First decode with inJustDecodeBounds=true to check dimensionsfinal BitmapFactory.Options options = new BitmapFactory.Options();options.inJustDecodeBounds = true;BitmapFactory.decodeResource(res, resId, options);// Calculate inSampleSizeoptions.inSampleSize = calculateInSampleSize(options, reqWidth,reqHeight);// Decode bitmap with inSampleSize setoptions.inJustDecodeBounds = false;return BitmapFactory.decodeResource(res, resId, options);}}
主界面的xml代码:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" tools:context="bill.su.loadbitmap.MainActivity" > <ImageView android:id="@+id/pandaImageView" android:layout_width="100px" android:layout_height="100px" android:layout_alignParentLeft="true" android:layout_alignParentTop="true" /></RelativeLayout>
使用ImageView来显示图片
主界面的逻辑代码添加代码:
@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);ImageView imageView = (ImageView) findViewById(R.id.pandaImageView);imageView.setImageBitmap(BitmapUtils.decodeSampledBitmapFromResource(getResources(), R.drawable.panda, 100, 100));}注意R.drawable.panda是怎么来的。只要在项目文件夹中的res文件夹的drawable文件夹添加一个图片命名为panda,在Eclipse刷新项目就会显示这个id了。如果没有drawable这个文件夹也不要紧,直接自己新建一个文件夹就可以了。
如果图片没有显示,很可能是图片资源不存在,这样项目是不会提示错误的,直接没有显示出来。
看看项目结构图,就知道如何建立这个项目了:
这里主要学习的代码是BitmapUtils中的代码,这样已经封装好了,以后可以当做自己的一个资源类调用了。
3 0
- Android百日程序:高效载入大图片
- Android-如何高效的载入大图片
- Android 高效显示大图片
- Android 高效加载大图片
- Android 高效加载大图片
- Android 高效加载大图片
- Android 高效加载大图片
- Android 高效加载大图片
- Android--高效地加载大图片
- Android--高效地加载大图片
- Android--高效地加载大图片
- android高效读取大图片 防止OOM
- Android--高效地加载大图片
- android 高效加载大图片,避免OOM
- Android 高效加载大图片避免OOM
- Android高效加载大图片,防止OOM
- Android 高效加载大图片避免OOM
- Android--高效地加载大图片
- 黑马程序员--oc:NSString、NSValue、NSNumber、NSDate
- Java 中经常用到的技术总结-----(流和网络相关)
- 隐马尔可夫模型(HMM)详解
- linux0.11中的fork实现和返回两个不同值的原因
- C流程控制(选择结构与循环结构)
- Android百日程序:高效载入大图片
- 堆排序实现
- ubuntu下安装x264库
- 2014.12.7
- Android 分页控件制成底部菜单.
- io流复习
- 校验银行卡号正确性的oracle源代码
- MFC之GDI+配置篇
- 参数代换:xargs