package util;
import android.content.res.Resources;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
//工具类
public class Utils {
public Bitmap setimg(Resources resources,Integer imgs){
//配置
BitmapFactory.Options options = new BitmapFactory.Options();
//设置为true 得到宽高
options.inJustDecodeBounds =true;
//第一次获取
Bitmap bitmap = BitmapFactory.decodeResource(resources, imgs, options);
//配置二次数据
options.inJustDecodeBounds=false;
//比例换算
options.inSampleSize=8;
//阿尔法通道
options.inPreferredConfig=Bitmap.Config.RGB_565;
//二次获取
Bitmap bitmap1 = BitmapFactory.decodeResource(resources, imgs, options);
return bitmap1;
}
}
//在activity中调用
package com.baidu.a2temp;
import android.graphics.Bitmap;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.ImageView;
import Util.Utils;
public class MainActivity extends AppCompatActivity {
private ImageView img;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
img = (ImageView) findViewById(R.id.img);
Utils utils = new Utils();
Bitmap setimg = utils.setimg(getResources(), R.drawable.qmsht);
img.setImageBitmap(setimg);
}
}
//一个简单的图片布局
<?xml version="1.0" encoding="utf-8"?><ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/img"
/>