This is a simple Demo. The purpose is to read a folder to generate a thumbnail and click to display a large image.
Create a new project, create a ThumbnailsWindows class, and inherit LinearLayout. The Code is as follows:
[Java]
<Span style = "font-size: 16px;"> <span style = "font-size: 16px;"> package org. winplus. thum. view;
Import java. io. File;
Import java. io. FileInputStream;
Import java. io. FileNotFoundException;
Import java. io. InputStream;
Import java. util. ArrayList;
Import java. util. Collections;
Import java. util. Iterator;
Import java. util. Map;
Import java. util. TreeMap;
Import android. content. Context;
Import android. graphics. Bitmap;
Import android. graphics. BitmapFactory;
Import android. util. AttributeSet;
Import android. view. LayoutInflater;
Import android. view. MotionEvent;
Import android. view. View;
Import android. widget. ImageButton;
Import android. widget. ImageView;
Import android. widget. LinearLayout;
Import cn. embel. thum. R;
Public class ThumbnailsWindows extends LinearLayout {
Private static final String TAG = "ThumbnailsWindows ";
Private Context mContext;
Private static ArrayList <String> paths = new ArrayList <String> ();
Private ImageView imageView;
Public ThumbnailsWindows (Context context ){
Super (context );
MContext = context;
SetupViews ();
}
Public ThumbnailsWindows (Context context, AttributeSet attrs ){
Super (context, attrs );
MContext = context;
SetupViews ();
}
Public void setupViews (){
/**
* It must be used to display a large image. Of course, it can be defined directly in this class! This makes it easier to control ~ Let's change it later. What about the Chinese New Year?
*/
Final LayoutInflater mLayoutInflater = LayoutInflater. from (getContext ());
View v = mLayoutInflater. inflate (R. layout. original_photo, null );
ImageView = (ImageView) v. findViewById (R. id. original );
Map <String, Bitmap> maps = new TreeMap <String, Bitmap> ();
Try {
Maps = buildThum ();
} Catch (FileNotFoundException e ){
E. printStackTrace ();
}
Iterator <String> it = maps. keySet (). iterator ();
Int I = 0;
While (it. hasNext ()){
String path = (String) it. next ();
Bitmap bm = maps. get (path );
ImageButton image = new ImageButton (mContext );
Image. setImageBitmap (bm );
Image. setId (I ++ );
AddView (image );
Image. setOnTouchListener (listener );
}
AddView (v );
}
/**
* Define the Touch event of the button control
*/
OnTouchListener listener = new OnTouchListener (){
@ Override
Public boolean onTouch (View v, MotionEvent event ){
/**
* When the control is pressed, the current thumbnail is displayed.
*/
If (event. getAction () = MotionEvent. ACTION_DOWN ){
String path = paths. get (v. getId ());
InputStream inputStream = null;
Try {
InputStream = new FileInputStream (path );
} Catch (FileNotFoundException e ){
E. printStackTrace ();
}
Bitmap bitmap = BitmapFactory. decodeStream (inputStream );
ImageView. setImageBitmap (bitmap );
}
Return false;
}
};
/**
* Obtain the image address list
* @ Param file
* @ Return
*/
Private static ArrayList <String> imagePath (File file ){
ArrayList <String> list = new ArrayList <String> ();
File [] files = file. listFiles ();
For (File f: files ){
List. add (f. getAbsolutePath ());
}
Collections. sort (list );
Return list;
}
/**
* Read the images in the sdcard folder and generate a thumbnail.
* @ Return
* @ Throws FileNotFoundException
*/
Private Map <String, Bitmap> buildThum () throws FileNotFoundException {
File baseFile = new File ("/mnt/sdcard/tflash/image /");
// With TreeMap, the sorting problem does not need to be entangled.
Map <String, Bitmap> maps = new TreeMap <String, Bitmap> ();
If (baseFile! = Null & baseFile. exists ()){
Paths = imagePath (baseFile );
If (! Paths. isEmpty ()){
For (int I = 0; I <paths. size (); I ++ ){
BitmapFactory. Options options = new BitmapFactory. Options ();
Options. inJustDecodeBounds = true; // you must remember to set this attribute to false.
Bitmap bitmap = BitmapFactory. decodeFile (paths. get (I), options );
Options. inJustDecodeBounds = false;
Int be = options. outHeight/40;
If (be <= 0 ){
Be = 10;
}
Options. inSampleSize = be;
Bitmap = BitmapFactory. decodeFile (paths. get (I), options );
Maps. put (paths. get (I), bitmap );
}
}
}
Return maps;
}
}
</Span>
Modify the mail. xml file
[Html]
<Span style = "font-size: 16px;"> <? Xml version = "1.0" encoding = "UTF-8"?>
<LinearLayout xmlns: android = "http://schemas.android.com/apk/res/android"
Android: layout_width = "fill_parent"
Android: layout_height = "fill_parent"
Android: orientation = "vertical">
<Org. winplus. thum. view. ThumbnailsWindows
Android: layout_width = "fill_parent"
Android: layout_height = "fill_parent"/>
</LinearLayout> </span>
There are bugs in this Demo. You can modify it later to see if it can be changed to the same effect as the Ihone image browser. Well, this article will be written here!
Source code download: http://www.bkjia.com/uploadfile/2012/0208/20120208093646461.rar
From Tang Cheng's technical Column