本文实例讲述了Android编程实现压缩图片并加载显示的方法。分享给大家供大家参考,具体如下:
解析:
图片压缩的关键就是
options.inSampleSize = scale;
如果scale > 0,表示图片进行了压缩
/**
* 压缩图片
* @author chen.lin
*
*/
public class LoadImageActivity extends Activity implements OnClickListener {
private Button mBtnLoad;
private ImageView mImageView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_image_load);
initViews();
}
private void initViews() {
mBtnLoad = (Button) findViewById(R.id.btnLoadImage);
mImageView = (ImageView) findViewById(R.id.imageView);
mBtnLoad.setOnClickListener(this);
}
@Override
public void onClick(View v) {
if (v == mBtnLoad) {
Options options = new Options();
BitmapFactory.decodeFile("/sdcard/images/1.jpg", options);
//不去真的解析图片,只是获取图片的头部信息,宽高
options.inJustDecodeBounds = true;
//得到图片的真实宽高
int imageHeight = options.outHeight;
int imageWidth = options.outWidth;
//得到屏幕的宽高
WindowManager wm = (WindowManager) getSystemService(WINDOW_SERVICE);
int screenHeight = wm.getDefaultDisplay().getHeight();
int screenWidth = wm.getDefaultDisplay().getWidth();
//得到缩放比例
int scale = 1;
int scaleX = imageWidth / screenWidth;
int scaleY = imageHeight / screenHeight;
if (scaleX > scaleY & scaleX >=1) {//表示如果宽的缩放比例大于高的,并且scaleX>=1都为true
scale = scaleX;
}
if (scaleY > scaleX & scaleY >=1) {//表示如果高的缩放比例大于宽的,并且scaleY>=1都为true
scale = scaleY;
}
//解析图片
options.inJustDecodeBounds = false;
//修改图片的缩放比例,如果scale=4说明图片缩小4倍,像数=1/16
options.inSampleSize = scale;
Bitmap bm = BitmapFactory.decodeFile("/sdcard/images/1.jpg", options);
mImageView.setImageBitmap(bm);
}
}
}
布局文件
疯狂医院达什医生中文版(Crazy Hospital)
疯狂医院达什医生最新版是一款医院模拟经营类游戏,逼真的场景画
宝宝庄园官方版
宝宝庄园官方版是一款超级经典好玩的模拟经营类型的手游,这个游
桃源记官方正版
桃源记是一款休闲娱乐类的水墨手绘风格打造的模拟经营手游。玩家
长途巴士模拟器手机版
长途巴士模拟器汉化版是一款十分比真好玩的大巴车模拟驾驶运营类
房东模拟器最新版2024
房东模拟器中文版是一个超级有趣的模拟经营类型的手游,这个游戏