Android大图片之缩略图,以及对原图按照指定宽高裁剪成缩略图
2015-08-22 10:42
501 查看
《Android大图片之变换缩略图,以及对原始大图片按照指定宽、高裁剪成缩略图》
在Android的ImageView加载图像资源过程中,出于性能和内存开销的需要,有时候需要把一个原始的超大图片按照一定比例等比例缩放成较小的缩略图,或者需要把原始的超大图片,裁剪成指定宽高值的较小图片,针对这种开发需求,可以使用Android SDK自身提供的工具类:ThumbnailUtils完成。
ThumbnailUtils的在Android官方的开发文档链接地址:
http://developer.android.com/reference/android/media/ThumbnailUtils.html
下面以一个超大图片为例,使用ThumbnailUtils等比例缩放成缩略图和按照指定的宽高裁剪成满足需要的缩略图。
测试用的MainActivity.java :
MainActivity.java需要的布局文件activity_main.xml :
需要处理的目标的大图,原始大图资源:
代码运行处理结果如图所示:
《Android大图片之变换缩略图,以及对原始大图片按照指定宽、高裁剪成缩略图》
在Android的ImageView加载图像资源过程中,出于性能和内存开销的需要,有时候需要把一个原始的超大图片按照一定比例等比例缩放成较小的缩略图,或者需要把原始的超大图片,裁剪成指定宽高值的较小图片,针对这种开发需求,可以使用Android SDK自身提供的工具类:ThumbnailUtils完成。
ThumbnailUtils的在Android官方的开发文档链接地址:
http://developer.android.com/reference/android/media/ThumbnailUtils.html
下面以一个超大图片为例,使用ThumbnailUtils等比例缩放成缩略图和按照指定的宽高裁剪成满足需要的缩略图。
测试用的MainActivity.java :
package zhangphil.thumbnail; import android.app.Activity; import android.graphics.Bitmap; import android.graphics.BitmapFactory; import android.media.ThumbnailUtils; import android.os.Bundle; import android.widget.ImageView; public class MainActivity extends Activity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); ImageView imageView0 = (ImageView) findViewById(R.id.imageView0); // 对原始资源图片不做任何处理和调整,直接加载到ImageView。 imageView0.setImageResource(R.drawable.image); Bitmap sourceBitmap = BitmapFactory.decodeResource(this.getResources(), R.drawable.image); // 把原始bitmap截取成一个 MIN_SIZE * MIN_SIZE 的正方形缩略图。注意:默认是以中心为原点截取成缩略图。 int MIN_SIZE = 300; ImageView imageView1 = (ImageView) findViewById(R.id.imageView1); Bitmap bmp1 = ThumbnailUtils.extractThumbnail(sourceBitmap, MIN_SIZE, MIN_SIZE); imageView1.setImageBitmap(bmp1); // 获得原始bitmap的高和宽,下面将对原始Bitmap等比例缩放成缩略图加载。 int h = sourceBitmap.getHeight(); int w = sourceBitmap.getWidth(); // 缩略图缩放的比例尺 int THUMB_SIZE; THUMB_SIZE = 5; // 对原始图片Bitmap等比例缩小5倍的缩略图 ImageView imageView2 = (ImageView) findViewById(R.id.imageView2); Bitmap bmp2 = ThumbnailUtils.extractThumbnail(sourceBitmap, w / THUMB_SIZE, h / THUMB_SIZE); imageView2.setImageBitmap(bmp2); // 对原始图片Bitmap等比例缩小10倍的缩略图。 THUMB_SIZE = 10; ImageView imageView3 = (ImageView) findViewById(R.id.imageView3); Bitmap bmp3 = ThumbnailUtils.extractThumbnail(sourceBitmap, w / THUMB_SIZE, h / THUMB_SIZE); imageView3.setImageBitmap(bmp3); } }
MainActivity.java需要的布局文件activity_main.xml :
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" > <LinearLayout android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" > <ImageView android:id="@+id/imageView0" android:layout_width="wrap_content" android:layout_height="wrap_content" /> <ImageView android:id="@+id/imageView1" android:layout_width="wrap_content" android:layout_height="wrap_content" /> <ImageView android:id="@+id/imageView2" android:layout_width="wrap_content" android:layout_height="wrap_content" /> <ImageView android:id="@+id/imageView3" android:layout_width="wrap_content" android:layout_height="wrap_content" /> </LinearLayout> </ScrollView>
需要处理的目标的大图,原始大图资源:
代码运行处理结果如图所示:
相关文章推荐
- Android Studio R.java文件在哪
- android学习8(ListView高级使用)
- Android开发笔记----radioGroup的使用
- Android版本分布——2015年8月更新
- Android 学习笔记1
- Android studio的下载安装配置详解 (上)
- Android软硬整合技术(HAL&Framework)
- Android SDK目录含义介绍
- Android 上下文对象
- Android Fragment(二)---使用
- 【Android】自定义view-拖动小球移动
- Android Api Demos登顶之路(四十三)Fragment-->Retain Instance
- 阅读安卓在线(Android)系统源代码
- 关于java、Android中Math的一些用法
- Android应用退出代码各种方式
- Android编码规范
- 《AndroidStudio每日一贴》4.如何快速插入引用?
- Android Studio Windows 10 Terminal 无法输入的解决方法
- Android中BaseAdapter的基本用法和加载自定义布局!
- Android开源项目分类