Android圖片瀏覽器之縮圖

來源:互聯網
上載者:User

標籤:

 

  最近在自學Android,嘗試實現一般手機上都存在的圖片瀏覽器,從縮圖開始。

       

  直接,這是google手機內建Photos圖片瀏覽器。

  

  通過查閱資料瞭解到,在實現縮圖之前需要做的工作有:

    1、尋找手機中所有圖片,包括、拍照、通過USB從電腦中黏貼過來等方式儲存在若干個檔案夾的,

       將圖片完整路徑儲存在字串數組中;

    2、對於某一幅圖片,不管是縮減也好,全螢幕顯示也好,需要在一種組件進行顯示(只是圖片的大小不一樣);

    3、尋找一種組件,可以利用來存放各個縮圖,完成網格般的效果;

 

  對於第一點,說白了就是檔案搜尋與選擇功能,在電腦和手機中都適用。

  因為這裡只用到圖片檔案,那隻需要將常規的幾種映像格式過濾出來即可(jpg,jpeg,tif,tiff,png,gif,bmp)。

  關鍵代碼如下:

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     }

  上面三段代碼的作用分別為:定義一個String類型的ArrayList變數fullPathImg,用來儲存圖片路徑;擷取手機SD卡路徑;搜尋圖片檔案實現函數,是一個遞迴過程。

 

  對於第二點,在Android中可以顯示圖片的組件太多了,如ImageView、ImageButton,連Button、Toast等有時候都可以來湊下熱鬧。

  這裡採用ImageView,給其賦於圖片也有很多種方式:

    1、setImageBitmap(BitmapFactory.decodeFile(imagePath));  imagePath為String類型,指圖片完整名稱;

    2、setImageResource(R.mipmap.ic_launcher);  通過資源來賦值,需要事先匯入工程中,或者調用Android內建資源;

    3、setImageDrawable(getResources().getDrawable(R.mipmap.ic_launcher));  和第二種很相近,通過Drawable來賦值,只是多了若干步驟;

  由於本應用是要通過檔案名稱來讀取圖片資源,故採用的是第一種。

 

  要完成第三點,由於是初學,採用的是較好實現的GridView組件。但是需要實現一個繼承自BaseAdapter的類ImageAdapter,代碼如下:

 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     }

  ImageAdapter類主要實現五個方法,分別為構造、擷取總數、擷取元素位置、擷取元素ID、擷取視圖,最為關鍵的是最後一個getView()。

  需要注意兩點:

    1、不用手動一個個去將所有圖片資源和組件GridView關聯起來,通過下面的語句制定路徑變數和位置資訊即可,

    imageView.setImageBitmap(BitmapFactory.decodeFile(fullPathImg.get(position)));   imageView為ImageView類的變數;

    2、對imageView的大小進行設定時,已手機螢幕的解析度作為參考,擷取方式如下,

    int windowWidth = getWindowManager().getDefaultDisplay().getWidth();   擷取寬度,高度類似;

  ImageAdapter實現了,下面給出GridView定義的activity_main.xml檔案內容:

 

 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>

 

  對其進行設定的屬性包括大小、透明度、自身與父類及內容之間的空隙、縮圖列數、置中、滑動條,挺簡單就不一一解釋了。

 

  

  一切準備好後,在主程式檔案MainActivity中進行載入即可,下面給出關鍵代碼:

1 setContentView(R.layout.activity_main);2 gridView = (GridView)findViewById(R.id.gridView);3 adapter = new ImageAdapter(this);4 gridView.setAdapter(adapter);

  

  運行後的效果還不錯,工程名為ImageScan。

      

 

  當然,對於一款真正的圖片瀏覽器來說,這隻是其中最基礎的功能,即第一步,需要實現還有很多,

  如單擊某一幅圖片立刻放大顯示,隨後可以左右或上下滑動;對放大瀏覽的圖片可以任意縮放、編輯等。

  有興趣的朋友可以一起學習交流,有錯誤或不足的還望大神們指點一二。

 

Android圖片瀏覽器之縮圖

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.