Android加载大图
来源:互联网 发布:mysql时间函数 编辑:程序博客网 时间:2024/05/20 20:43
Android加载大图到内存
1.数码相机照片特别大3m以上,内存吃不消,只显示原图的1/8
通过BitmapFactory.Options 来实现
BitmapFactory.Options bmpFactoryOptions = new BitmapFactory.Options();
bmpFactoryOptions.inSampleSize = 8;
Bitmap bmp = BitmapFactory.decodeFile(imageFilePath, bmpFactoryOptions);
imv.setImageBitmap(bmp);
2.根据当前屏幕分辨率的大小,加载图片
Display currentDisplay = getWindowManager().getDefaultDisplay();
int dw = currentDisplay.getWidth();
int dh = currentDisplay.getHeight();
BitmapFactory.Options bmpFactoryOptions = new BitmapFactory.Options();
bmpFactoryOptions.inJustDecodeBounds = true;
Bitmap bmp = BitmapFactory.decodeFile(imageFilePath, bmpFactoryOptions);
int heightRatio = (int)Math.ceil(bmpFactoryOptions.outHeight/(float)dh);
int widthRatio = (int)Math.ceil(bmpFactoryOptions.outWidth/(float)dw);
Log.v("HEIGHTRATIO",""+heightRatio);
Log.v("WIDTHRATIO",""+widthRatio);
//判断是否要进行缩放
if (heightRatio > 1 && widthRatio > 1)
{
if (heightRatio > widthRatio)
{
//高度变化大,按高度缩放
bmpFactoryOptions.inSampleSize = heightRatio;
}
else
{
// 宽度变化大,按宽度缩放
bmpFactoryOptions.inSampleSize = widthRatio;
}
}
bmpFactoryOptions.inJustDecodeBounds = false;
bmp = BitmapFactory.decodeFile(imageFilePath, bmpFactoryOptions);
activity_main.xml:
<LinearLayout 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" android:orientation="vertical" tools:context=".MainActivity" > <Button android:onClick="click" android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="加载图片到内存" /> <ImageView android:id="@+id/iv" android:layout_width="fill_parent" android:layout_height="fill_parent" /></LinearLayout>
public class MainActivity extends Activity {private ImageView iv;@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);iv = (ImageView) findViewById(R.id.iv);}public void click(View view){//相当消耗内存资源 根据图片的分辨率而定// Bitmap bitmap = BitmapFactory.decodeFile("/mnt/sdcard/photo.jpg");// iv.setImageBitmap(bitmap);//1.得到屏幕的宽高信息WindowManager wm = getWindowManager();int screenWidth = wm.getDefaultDisplay().getWidth();int screenHeight = wm.getDefaultDisplay().getHeight();System.out.println("屏幕宽高:"+screenWidth+"-"+screenHeight);//2.得到图片的宽高。BitmapFactory.Options opts = new Options();//解析位图的附加条件opts.inJustDecodeBounds = true;//不去解析真实的位图,只是获取这个位图的头文件信息Bitmap bitmap = BitmapFactory.decodeFile("/mnt/sdcard/photo.jpg", opts);int bitmapWidth = opts.outWidth;int bitmapHeight = opts.outHeight;System.out.println("图片宽高: "+bitmapWidth+"-"+bitmapHeight);//3.计算缩放比例int dx = bitmapWidth/screenWidth;int dy = bitmapHeight/screenHeight;int scale = 1;if(dx>dy&&dy>1){System.out.println("按照水平方法缩放,缩放比例:"+dx); scale = dx;}if(dy>dx&&dx>1){System.out.println("按照垂直方法缩放,缩放比例:"+dy);scale = dy;}//4.缩放加载图片到内存。opts.inSampleSize = scale;opts.inJustDecodeBounds = false;//真正的去解析这个位图。bitmap = BitmapFactory.decodeFile("/mnt/sdcard/photo.jpg", opts);iv.setImageBitmap(bitmap);}}
- Android大图加载方法
- android 高效加载大图
- Android 加载大图片
- android 加载大图片
- android加载大图片
- android高效加载大图
- Android-加载大图片
- android加载大图片
- Android高效加载大图
- android 加载大图片
- android加载大图片
- Android加载大图
- Android完美加载大图
- Android 加载大图片
- Android 加载大图片
- Android加载大图
- Android 大图预览、高效大图加载
- android缩放大图片加载
- NYOJ 202 红黑树
- java多线程系列--AQS-01之独占锁原理浅析
- NPM常用指令
- Ignatius and the Princess II(bfs)
- fully_connected_feed代码说明
- Android加载大图
- u-boot移植
- Python UnicodeEncodeError: 'gbk' codec can't encode character 解决方法
- UIImage和NSData相互转换
- leetcode345: Reverse Vowels of a String
- hrbust1530 pie
- 组合数(dfs)
- Ubuntu 14.04编译安装hadoop 2.7.3
- Android Framework学习(六)之RefBase,SP,WP