Android编程实现压缩图片并加载显示的方法

所属分类: 软件编程 / Android 阅读数: 45
收藏 0 赞 0 分享

本文实例讲述了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);
    }
  }
}

布局文件

<?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>

更多关于Android相关内容感兴趣的读者可查看本站专题:《Android图形与图像处理技巧总结》、《Android开发入门与进阶教程》、《Android调试技巧与常见问题解决方法汇总》、《Android基本组件用法总结》、《Android视图View技巧总结》、《Android布局layout技巧总结》及《Android控件用法总结

希望本文所述对大家Android程序设计有所帮助。

更多精彩内容其他人还在看

Retrofit2日志拦截器的使用

这篇文章主要介绍了Retrofit2日志拦截器的使用,小编觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随小编过来看看吧
收藏 0 赞 0 分享

Android创建外部lib库及自定义View的图文教程

这篇文章主要给大家介绍了关于Android创建外部lib库及自定义View的相关资料,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
收藏 0 赞 0 分享

Android分享微信小程序失败的一些事小结

这篇文章主要给大家介绍了关于Android分享微信小程序失败一些事,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
收藏 0 赞 0 分享

Android分享微信小程序技巧之图片优化

这篇文章主要给大家介绍了关于Android分享微信小程序技巧之图片优化的相关资料,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
收藏 0 赞 0 分享

Android Viewpager实现无限循环轮播图

这篇文章主要为大家详细介绍了Android Viewpager实现无限循环轮播图,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
收藏 0 赞 0 分享

Android中的Bitmap序列化失败的解决方法

这篇文章主要介绍了Android中的Bitmap序列化失败的解决方法,本文通过实例代码给大家介绍的非常详细,具有一定的参考借鉴价值,需要的朋友可以参考下
收藏 0 赞 0 分享

Android自定义通用标题栏CustomTitleBar

这篇文章主要为大家详细介绍了Android自定义通用标题栏CustomTitleBar,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
收藏 0 赞 0 分享

Android组合控件自定义标题栏

这篇文章主要为大家详细介绍了Android组合控件自定义标题栏,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
收藏 0 赞 0 分享

Android自定义复合控件实现通用标题栏

这篇文章主要为大家详细介绍了Android自定义复合控件实现通用标题栏,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
收藏 0 赞 0 分享

ExpandableListView实现简单二级列表

这篇文章主要为大家详细介绍了ExpandableListView实现简单二级列表,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
收藏 0 赞 0 分享
查看更多