Thumbnails of Android image browsers

Source: Internet
Author: User

Thumbnails of Android image browsers
After reading the materials, I learned that the work to be done before thumbnails are: 1. Search for all the pictures on the mobile phone, the full path of the image is stored in a string array, including taking photos and sticking it to a computer by means of USB. 2. For an image, whether scaling or full screen display, You need to display the image in a component (only the image size is different). 3. Find a component that can be used to store various thumbnails, complete the grid effect. For the first point, simply put, it is the file search and selection function, which is applicable to both computers and mobile phones. Because only image files are used here, you only need to filter out common image formats (jpg, jpeg, tif, tiff, png, gif, bmp ). The key code is as follows: 1 ArrayList <String> fullPathImg = new ArrayList <String> (); 1 String pathRoot = Environment. getExternalStorageDirectory (). getPath () + "/"; 2 File root = new File (pathRoot); 3 getAllFiles (root );

 1 private void getAllFiles(File root){ 2         File files[] = root.listFiles(); 3         if(files != null){ 4             for (File f : files){ 5                 if(f.isDirectory()){ 6                     getAllFiles(f); 7                 }else{ 8                     String strf = f.toString(); 9                     String str = strf.substring(strf.length() - 4, strf.length());10                     if(str.equals(".bmp")||str.equals(".jpg")||str.equals(".jpeg")||str.equals(".png")||str.equals(".gif")||str.equals(".tif")||str.equals(".tiff")){11                         boolean bool = fullPathImg.add(strf);12                     }13                 }14             }15         }16     } 

 

The above three pieces of code define a String-type ArrayList variable fullPathImg to store the image path, obtain the path of the mobile phone SD card, and search for the image file implementation function, which is a recursive process. For the second point, there are too many components that can display images in Android, such as ImageView, ImageButton, and even Button and Toast. ImageView is used here, and there are also many ways to assign it to images: 1. setImageBitmap (BitmapFactory. decodeFile (imagePath); imagePath is of the String type, which indicates the complete image name; 2. setImageResource (R. mipmap. ic_launcher); assign values through resources. Import the values to the project in advance or call the Android built-in resources. 3. setImageDrawable (getResources (). getDrawable (R. mipmap. ic_launcher); it is very similar to the second type. It only takes several steps to assign values through Drawable. Because this application uses file names to read image resources, the first type is used. To complete the third step, because it is a beginner, a better implemented GridView component is used. However, you need to implement a class ImageAdapter that inherits from BaseAdapter. The Code is as follows:
 1 public class ImageAdapter extends BaseAdapter { 2  3         private Context context; 4  5         int countImg; 6  7         public ImageAdapter(Context context){ 8             this.context = context; 9             this.countImg = fullPathImg.size();10         }11 12         @Override13         public int getCount(){14             return countImg;15         }16 17         public Object getItem(int position){18             return position;19         }20 21         public long getItemId(int position)22         {23             return position;24         }25 26         @Override27         public View getView(int position, View convertView, ViewGroup parent)28         {29             windowWidth = getWindowManager().getDefaultDisplay().getWidth();30             int pad = 4;31             if(convertView == null){32                 imageView = new ImageView(context);33             }34             else{35                 imageView = (ImageView)convertView;36             }37             imageView.setLayoutParams(new GridView.LayoutParams((windowWidth - pad * 12) / 4, (windowWidth - pad * 12) / 4));38             imageView.setScaleType(ImageView.ScaleType.CENTER_CROP);39             imageView.setImageBitmap(BitmapFactory.decodeFile(fullPathImg.get(position)));40             return imageView;41         }

 

42} The ImageAdapter class mainly implements five methods: Construction, total acquisition, element position acquisition, element ID acquisition, and view acquisition. The most important thing is the last getView (). Note two points: 1. Do not manually associate all image resources with the component GridView one by one. Use the following statement to specify the PATH variables and location information. setImageBitmap (BitmapFactory. decodeFile (fullPathImg. get (position); imageView is a variable of the ImageView class; 2. When setting the imageView size, the resolution of the screen on the mobile phone is used as a reference. The Obtaining method is as follows, int required wwidth = getWindowManager (). getdefadisplay display (). getWidth (); get the width, the height is similar; ImageAdapter is implemented, the content of the activity_main.xml file defined by the GridView is given below:
 1 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 2     xmlns:tools="http://schemas.android.com/tools" 3     android:layout_width="match_parent" 4     android:layout_height="match_parent" > 5  6     <GridView 7         android:id="@+id/gridView" 8         android:layout_width="match_parent" 9         android:layout_height="match_parent"10         android:listSelector="@android:color/transparent"11         android:cacheColorHint="@android:color/transparent"12         android:horizontalSpacing="4dp"13         android:verticalSpacing="4dp"14         android:layout_marginLeft="8dp"15         android:layout_marginRight="8dp"16         android:layout_marginTop="8dp"17         android:layout_marginBottom="8dp"18         android:stretchMode="columnWidth"19         android:gravity="center"20         android:fadeScrollbars="true"21         android:fastScrollEnabled="true"22         android:numColumns="4" >23     </GridView>24 25 </LinearLayout> 

 

The setting attributes include the size, transparency, the gap between itself and its parent class and content, the number of thumbnail columns, center, and slide, which are not explained in detail. After everything is ready, load it in the main program file MainActivity. The key code is as follows:
1 setContentView(R.layout.activity_main);2 gridView = (GridView)findViewById(R.id.gridView);3 adapter = new ImageAdapter(this);4 gridView.setAdapter(adapter);  

 

After running, the project name is ImageScan. Of course, for a real image browser, this is only the most basic function, that is, the first step, there are many more to be implemented, such as clicking an image to immediately enlarge the display, you can then slide between the left and right or up or down. You can zoom in or edit the enlarged image. If you are interested, you can study and communicate with others. If you have any mistakes or problems, please give us some advice.

Related Article

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.