<1> Introduction
It was often done with bitmap, drawable, and canvas, and it was necessary to write a series of complicated logic to narrow down the original image to get a thumbnail image.
Now I will introduce a relatively simple method: (online)
In the Android 2.2 version, a new Thumbnailutils tool class is implemented to implement thumbnails, the functionality of this tool class is powerful, the use is simple, it provides a constant and three methods. With these constants and methods, it is easy and quick to realize the thumbnail function of pictures and videos.
<2>thumbnailutils Tool Class
Constant:
Options_recycle_input
This constant is used to indicate that extractThumbnail(Bitmap, int, int, int)
the input source picture (the first parameter) should be reclaimed, unless the output picture is the input picture.
Method:
Bitmap Createvideothumbnail (String filePath, int kind)
Creates a thumbnail image of a video. May return NULL if the video is corrupted or the format does not support it.
Parameters:
FilePath: Video File path
Kind: File type, can be Mini_kind or micro_kind
Bitmap Extractthumbnail (Bitmap source, int width, int height, int options)
Creates a bitmap that is centered and scaled for the desired size.
Parameters:
Source: Bitmap object that needs to be created thumbnail
Width: Create target widths
Height: The altitude of the build target
Options: Option provided when thumbnail extraction
Bitmap Extractthumbnail (Bitmap source, int width, int height)
Creates a bitmap that is centered and scaled for the desired size.
Parameters:
Source: Bitmap object that needs to be created thumbnail
Width: Create target widths
Height: The altitude of the build target
<3> Specific implementation:
Package Xiaosi.thumbnail;import Android.app.activity;import android.graphics.bitmap;import Android.graphics.bitmapfactory;import Android.graphics.drawable.bitmapdrawable;import Android.graphics.drawable.drawable;import Android.media.thumbnailutils;import Android.os.Bundle;import Android.widget.ImageView; Public classThumbnailactivity extends Activity {PrivateBitmap Bitmap =NULL; PrivateImageView image; @Override Public voidonCreate (Bundle savedinstancestate) {super.oncreate (savedinstancestate); Setcontentview (R.layout.main); Image=(ImageView) Findviewbyid (r.id.image); //get the original pictureBitmap =Bitmapfactory.decoderesource (Getresources (), R.DRAWABLE.V); //Get thumbnail imageBitmap = Thumbnailutils.extractthumbnail (Bitmap, -, -); Image.setimagebitmap (bitmap); } }
Main.xml
<?xml version="1.0"encoding="Utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"android:orientation="Vertical"Android:background="#999999"Android:layout_width="fill_parent"Android:layout_height="fill_parent"> <ImageView android:layout_width="wrap_content"Android:layout_height="wrap_content"android:src="@drawable/V"Android:layout_marginleft="10dip"/> <TextView android:layout_width="wrap_content"Android:layout_height="wrap_content"Android:text="thumbnail Image:"Android:textcolor="#000000"/> <imageview android:id="@+id/image"Android:layout_width="wrap_content"Android:layout_height="wrap_content"Android:layout_marginleft="10dip"/> </LinearLayout>
Thumbnail image of Android image