本文实例讲述了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); } } }
布局文件
凡尔赛法环免广告版 最新版v1.69.1
下载天煞异种 安卓版v1.0.21
下载十三月 (Undecember)最新官方中文版v3.26.0200
下载我不是废柴无限99999钻999999金币游戏 不减反增版v1.2.16
下载我的女友是声优免广告 安卓版v1
我的女友是声优免广告是一款非常好玩的模拟经营类手游,玩家在游
石器帝国去广告版 v8
石器帝国免广告版是游戏的破解版本,在该版本中为玩家去除了广告
校园恋爱模拟器真人版去广告版 v0.0.7
校园恋爱模拟器真人版免广告版是游戏的破解版本,在该版本中为玩
教练我要开球馆内置菜单修改器 安卓版v1.5.4
教练我要开球馆内购版是一款模拟经营类游戏,玩家们将在游戏中创
我的游轮无限砖石 安卓版v1.7.0
我的游轮无限金币钻石版是一款模拟经营类游戏,你将成为一艘游轮