17、加载大张图片到内存

---------------------------------------main.java----------------------


package com.itheima.loadimg;


import android.app.Activity;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.BitmapFactory.Options;
import android.os.Bundle;
import android.view.View;
import android.view.WindowManager;
import android.widget.ImageView;


public class MainActivity extends Activity {
private ImageView iv;


@Override
protected 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);
}
}


。。。。。。。。。。。。。。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>

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值