Android Learning notes Advanced 20 get thumbnail image of the picture

Source: Internet
Author: User

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

[Java]View PlainCopy
  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. //Get the original picture
  19. Bitmap = Bitmapfactory.decoderesource (Getresources (), R.DRAWABLE.V);
  20. //Get thumbnail image
  21. Bitmap = Thumbnailutils.extractthumbnail (bitmap, 100);
  22. Image.setimagebitmap (bitmap);
  23. }
  24. }


Main.xml

[Java]View PlainCopy
  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="thumbnail:"
  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 Learning notes Advanced 20 get thumbnail image of the picture

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.