<1>简介

之前往往是通过Bitmap、Drawable和Canvas配合完成,需要写一系列繁杂的逻辑去缩小原有图片,从而得到缩略图。

现在我给大家介绍一种比较简单的方法:(网上有)

Android 2.2版本中,新增了一个ThumbnailUtils工具类来是实现缩略图,此工具类的功能是强大的,使用是简单,它提供了一个常量和三个方法。利用这些常数和方法,可以轻松快捷的实现图片和视频的缩略图功能。

<2>ThumbnailUtils工具类

常量:

OPTIONS_RECYCLE_INPUT

从此常量用于表示应该回收extractThumbnail(Bitmap, int, int, int)输入源图片(第一个参数),除非输出图片就是输入图片。

方法:

    Bitmap createVideoThumbnail(String filePath, int kind)

    创建一张视频的缩略图。如果视频已损坏或者格式不支持可能返回null。

参数:

filePath: 视频文件路径

                     kind:  文件种类,可以是 MINI_KIND 或 MICRO_KIND

    Bitmap extractThumbnail(Bitmap source, int width, int height, int options)

    创建所需尺寸居中缩放的位图。

    参数:

          source: 需要被创造缩略图的源位图对象

          width: 生成目标的宽度

         height: 生成目标的高度

         options:在缩略图抽取时提供的选项

    Bitmap extractThumbnail(Bitmap source, int width, int height)

    创建所需尺寸居中缩放的位图。

    参数:

        source: 需要被创造缩略图的源位图对象

        width: 生成目标的宽度

        height: 生成目标的高度

<3>具体实现:

  1. package xiaosi.thumbnail;
  2. import android.app.Activity;
  3. import android.graphics.Bitmap;
  4. import android.graphics.BitmapFactory;
  5. import android.graphics.drawable.BitmapDrawable;
  6. import android.graphics.drawable.Drawable;
  7. import android.media.ThumbnailUtils;
  8. import android.os.Bundle;
  9. import android.widget.ImageView;
  10. public class ThumbnailActivity extends Activity {
  11. private Bitmap bitmap = null;
  12. private ImageView image;
  13. @Override
  14. public void onCreate(Bundle savedInstanceState) {
  15. super.onCreate(savedInstanceState);
  16. setContentView(R.layout.main);
  17. image = (ImageView) findViewById(R.id.image);
  18. //得到原图片
  19. bitmap = BitmapFactory.decodeResource(getResources(), R.drawable.v);
  20. //得到缩略图
  21. bitmap = ThumbnailUtils.extractThumbnail(bitmap, 100, 100);
  22. image.setImageBitmap(bitmap);
  23. }
  24. }

main.xml

  1. <?xml version="1.0" encoding="utf-8"?>
  2. <LinearLayout
  3. xmlns:android="http://schemas.android.com/apk/res/android"
  4. android:orientation="vertical"
  5. android:background="#999999"
  6. android:layout_width="fill_parent"
  7. android:layout_height="fill_parent">
  8. <ImageView
  9. android:layout_width="wrap_content"
  10. android:layout_height="wrap_content"
  11. android:src="@drawable/v"
  12. android:layout_marginLeft="10dip"/>
  13. <TextView
  14. android:layout_width="wrap_content"
  15. android:layout_height="wrap_content"
  16. android:text="缩略图:"
  17. android:textColor="#000000"/>
  18. <ImageView android:id="@+id/image"
  19. android:layout_width="wrap_content"
  20. android:layout_height="wrap_content"
  21. android:layout_marginLeft="10dip"/>
  22. </LinearLayout>

Android学习笔记进阶20之得到图片的缩略图的更多相关文章

  1. Android学习笔记进阶20 之得到图片的缩略图

    <1>简介 之前往往是通过Bitmap.Drawable和Canvas配合完成,需要写一系列繁杂的逻辑去缩小原有图片,从而得到缩略图. 现在我给大家介绍一种比较简单的方法:(网上有) 在A ...

  2. Android学习笔记进阶19 之给图片加边框

    //设置颜色 public void setColour(int color){ co = color; } //设置边框宽度 public void setBorderWidth(int width ...

  3. Android学习笔记进阶19之给图片加边框

    //设置颜色 public void setColour(int color){ co = color; } //设置边框宽度 public void setBorderWidth(int width ...

  4. Android学习笔记进阶之在图片上涂鸦(能清屏)

    Android学习笔记进阶之在图片上涂鸦(能清屏) 2013-11-19 10:52 117人阅读 评论(0) 收藏 举报 HandWritingActivity.java package xiaos ...

  5. Android学习笔记进阶16之BitmapShader

    <1>简介 具体的看一下博文:Android学习笔记进阶15之Shader渲染 public   BitmapShader(Bitmap bitmap,Shader.TileMode ti ...

  6. Android学习笔记进阶17之LinearGradient

    具体的看一下博文:Android学习笔记进阶15之Shader渲染 package xiaosi.BitmapShader; import android.app.Activity; import a ...

  7. Android学习笔记进阶18 之画图并保存图片到本地

    1.首先创建一个Bitmap图片,并指定大小:   2.在该图片上创建一个新的画布Canvas,然后在画布上绘制,并保存即可:   3.需要保存的目录File,注意如果写的目录如“/sdcard/so ...

  8. Android学习笔记进阶18之画图并保存图片到本地

    1.首先创建一个Bitmap图片,并指定大小:   2.在该图片上创建一个新的画布Canvas,然后在画布上绘制,并保存即可:   3.需要保存的目录File,注意如果写的目录如“/sdcard/so ...

  9. Android 学习笔记进阶十二之裁截图片

    package xiaosi.cut; import java.io.File; import android.app.Activity; import android.content.Intent; ...

随机推荐

  1. Linux学习总结(9)——Linux 新手必知必会的 10 条 Linux 基本命令

    Linux 对我们的生活产生了巨大的冲击.至少你的安卓手机使用的就是 Linux 核心.尽管如此,在第一次开始使用 Linux 时你还是会感到难以下手.因为在 Linux 中,通常需要使用终端命令来取 ...

  2. MySQL auttoReconnect

    com.mysql.jdbc.exceptions.jdbc4.CommunicationsException: The last packet successfully received from ...

  3. Ubuntu下安装git工具

    环境:Ubuntu 9.10 git-1.8.2.3.tar.bz2 1.将安装包下载到所选文件夹下,如:/tmp 2.tar -xjf git-1.8.2.3.tar.bz2 3.cd git-1. ...

  4. double long float类型读入读出 double取模 fmod

    The library of fmod is #include <cmath> #include<cstdio> #include<cstdlib> #includ ...

  5. spring security oauth2 架构---官方

    原文地址:https://projects.spring.io/spring-security-oauth/docs/oauth2.html Introduction This is the user ...

  6. Codeforces 344D Alternating Current 简单使用栈

    Description Mad scientist Mike has just finished constructing a new device to search for extraterres ...

  7. Codefroces432 div2 A,B,C

    A. Arpa and a research in Mexican wave Arpa is researching the Mexican wave. There are n spectators ...

  8. Aix下查看内存命令

    1. 查看物理内存配置 # lsdev -Cc memory  查看配置的物理内存设备,下面为其输出示例:  L2cache0 Available L2 Cache   mem0 Available ...

  9. Thinkpad Access Connections实现快速的在各种网络间进行切换

    Thinkpad Access Connections快速的在各种网络间进行切换 因为工作的原因要经常在多个无线或有线之间切换,每次要切换到不同的固定IP地址的网络更是一种折磨,换一次就要从新输入一次 ...

  10. Linux学习总结(5)——CentOS常用的目录文件操作命令

    CentOS常用的目录文件操作命令 一.路径操作的CentOS常用命令  cd pwd  NO1. 显示当前路径  [root@rehat root]# pwd  NO2. 返回用户主目录  [roo ...