在android中,要明确一点,图片占用的内存大小与图片的实际大小无关,只与图片的分辨率有关,图片占用的内存空间为图片分辨率(高*宽)*4byte。

布局文件

<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:id="@+id/bt"        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:text="加载大图片"/>    <ImageView        android:id="@+id/iv"        android:layout_width="match_parent"        android:layout_height="match_parent"/></LinearLayout>

实现代码

package cn.test.ning.imagedemo;import android.app.Activity;import android.graphics.Bitmap;import android.graphics.BitmapFactory;import android.os.Bundle;import android.view.View;import android.view.WindowManager;import android.widget.Button;import android.widget.ImageView;public class MainActivity extends Activity {    private ImageView iv;    private Button bt;    private int screenWidth;    private int screenHight;    @Override    protected void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        setContentView(R.layout.activity_bigimage);        //获取屏幕信息        WindowManager windowManager = (WindowManager) getSystemService(WINDOW_SERVICE);        //获取屏幕宽度        screenWidth = windowManager.getDefaultDisplay().getWidth();        //获取屏幕高度        screenHight = windowManager.getDefaultDisplay().getHeight();        iv = (ImageView) this.findViewById(R.id.iv);        // Bitmap bitmap = BitmapFactory.decodeResource(getResources(), R.mipmap.ic_launcher);        // iv.setImageBitmap(bitmap);        bt = (Button) this.findViewById(R.id.bt);        bt.setOnClickListener(new View.OnClickListener() {            @Override            public void onClick(View v) {               // Bitmap bitmap = BitmapFactory.decodeResource(getResources(), R.drawable.very_large_photo);                BitmapFactory.Options opts = new BitmapFactory.Options();                //不真实的解析位图,只是返回位图的相关信息                opts.inJustDecodeBounds = true;                Bitmap bitmap = BitmapFactory.decodeResource(getResources(), R.drawable.very_large_photo,opts);                int width = opts.outWidth;//图片的宽度                int higth = opts.outHeight;//图片的高度                //计算缩放比                int scale = 1;                int scaleX = width /screenWidth;                int scaleY = higth /screenHight;                scale = (scaleX>scaleY?scaleX:scaleY);                if (scale<1){                    scale = 1;                }                //设置图片的缩放比                opts.inSampleSize = scale;                //真实解析位图                opts.inJustDecodeBounds = false;                //加载图片 重要的步骤                bitmap = BitmapFactory.decodeResource(getResources(), R.drawable.very_large_photo,opts);                iv.setImageBitmap(bitmap);            }        });    }}

使用到的图片

android中加载大图片到内存_第1张图片



更多相关文章

  1. android 网络访问-图片处理优秀开源项目 (201
  2. Android Fresco图片处理库用法API英文原文文档2-2(Facebook开源An
  3. android button background图片被拉伸
  4. Android 图片拖动 放大
  5. android 处理图片的工具类
  6. Android 选择图片裁剪,上传
  7. android加载大图片内存问题
  8. android图片异步加载解决步骤
  9. android 大图片拖拽并缩放实现原理

随机推荐

  1. 第二个Activity和Android(安卓)ListView
  2. android Notification (2)
  3. 2018-06-15 Android加载GIF图片的两种方
  4. Android传感器分析 seneor
  5. 【转】Android(安卓)Audio System
  6. 运行时权限方案:PermissionX
  7. 在 Kindle Fire 小米2 上调试 android 应
  8. Activity启动流程
  9. [Android(安卓)Traffic] 根据网络类型更
  10. Android(安卓)UI控件之Gallery实现循环效