图片二次采样

布局文件:
<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"
   >

    <ImageView
        android:id="@+id/iv"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
       />

</LinearLayout>
MainActivit.java
package com.example.day025_exloadertwince;

import android.os.Bundle;
import android.app.Activity;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.view.Menu;
import android.widget.ImageView;

public class MainActivity extends Activity {
    ImageView iv;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//初始化控件
iv=(ImageView) findViewById(R.id.iv);
//获取屏幕宽度和高度
int width=getResources().getDisplayMetrics().widthPixels;
int height=getResources().getDisplayMetrics().heightPixels;
//二次采样解析
Bitmap bm=getBitmap(width, height, R.drawable.oom);
iv.setImageBitmap(bm);
}
    /**
     * 二次采样解析方法
     * @param newWidth 屏幕宽度
     * @param newHeight屏幕高度
     * @param imageId图片id
     * @return  返回bitmap
     */
public Bitmap getBitmap(int newWidth,int newHeight,int imageId){
Bitmap bm=null;
BitmapFactory.Options opt=new BitmapFactory.Options();
opt.inJustDecodeBounds=true;
BitmapFactory.decodeResource(getResources(), imageId,opt);
int width=opt.outWidth;
int height=opt.outHeight;
int scaleX=width/newWidth;
int scaleY=height/newHeight;
int scale=scaleX>scaleY?scaleX:scaleY;
opt.inJustDecodeBounds=false;
opt.inSampleSize=scale;
              bm= BitmapFactory.decodeResource(getResources(),imageId,opt);
return bm;
}

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值