Android 加载压缩的图片
2016-05-16 22:46
447 查看
解析:
图片压缩的关键就是options.inSampleSize = scale;
如果scale > 0,表示图片进行了压缩
布局文件
图片压缩的关键就是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); } } }
布局文件
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" > <ImageView android:id="@+id/imageView" android:layout_width="fill_parent" android:layout_height="0dp" android:layout_weight="1" /> <EditText android:id="@+id/editText1" android:layout_width="match_parent" android:layout_height="wrap_content" android:ems="10" > <requestFocus /> </EditText> <Button android:id="@+id/btnLoadImage" android:layout_width="fill_parent" android:layout_height="wrap_content" android:onClick="loadImage" android:text="加载图片" /> </LinearLayout>
相关文章推荐
- AndroidStudio如何配置NDK/JNI开发环境
- Android JNI基础:什么是JNI
- Android自定义控件3——边缘凹凸的卡劵效果View
- android .9资源简单使用方法
- Android开发之本地音乐播放器(增强版)
- 怎么关联android-support-v4源码
- android html5 的弹出窗设置
- Android Surface View绘图API详解
- Android开发工具之Android Studio---版本控制SVN使用(二)
- Android LinearLayout布局的layout_weight属性探究
- Android Scroll分析(一)——滑动效果是如何产生的
- Android事件传递机制
- 快速集成Bugly Android SDK
- android studio 使用+错误总结
- Rxandroid中doOnSubscribe(),如何指定其运行的线程?
- Android Studio 魅族手机调试LogCat不显示解决方法
- android Callable Future FutureTask 解析
- Android wpa_supplcant 之--配置文件
- Android学习记录(十四) digest鉴权的文件播放
- Rxandroid中doOnSubscribe()执行默认情况下所在的线程