加载大图

加载大图

布局文件

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/activity_main"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context="com.example.a510.MainActivity">

<Button
    android:layout_centerHorizontal="true"
    android:id="@+id/btn_show"
    android:text="加载大图"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content" />

<ImageView
    android:layout_below="@id/btn_show"
    android:id="@+id/iv_show"
    android:layout_centerHorizontal="true"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content" />
</RelativeLayout>

初始化控件

 //初始化控件
    iv_show = (ImageView) findViewById(R.id.iv_show);
    btn_show = (Button) findViewById(R.id.btn_show);

将Mainactivity实现接口,并实现里面的onclick方法

public class MainActivity extends AppCompatActivity implements View.OnClickListener
//给按钮设置点击事件
 btn_show.setOnClickListener(this);
//实现点击事件
public void onClick(View v) {
    //获取图片的数据信息,当 inJustDecodeBounds为true时,只获取图片大小等信息,而不对图片进行解码
    BitmapFactory.Options options = new BitmapFactory.Options();
    options.inJustDecodeBounds = true;

    //通过BitmaFactory读取图片的格式信息
    Bitmap bitmap = BitmapFactory.decodeFile(path,options);
    //获取图片的宽度
    int outWidth = options.outWidth;
    //获取图片的高度
    int outHeight = options.outHeight;

    //判断图片的分辨率大小是否大于屏幕的大小,然后进行压缩
    if (outHeight>height||outWidth>width){
        int xIndex = outWidth/width;
        int yIndex = outHeight/height;

        int max = Math.max(xIndex, yIndex);
        options.inSampleSize = max;
        Log.e("MainActivity",max+"");
    }

计算好压缩的比例之后,对 inJustDecodeBounds设置为false后才能对图片进行解码

    options.inJustDecodeBounds = false;
    bitmap = BitmapFactory.decodeFile(path,options);
    //将图片设置在ImageView中
    iv_show.setImageBitmap(bitmap);
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值