歡迎來到Linux教程網
Linux教程網
Linux教程網
Linux教程網
Linux教程網 >> Linux編程 >> Linux編程 >> Android多媒體教程:實現仿百度圖片查看功能

Android多媒體教程:實現仿百度圖片查看功能

日期:2017/3/1 11:07:54   编辑:Linux編程

我們知道,進入百度圖片後,輸入一個關鍵字後,首先看到的是很多縮略圖,當我們點擊某張縮略圖時,我們就可以進入到大圖顯示頁面,在

大圖顯示頁面,中包含了一個圖片畫廊,同時當前大圖為剛剛我們點擊的那張圖片。現在我們看看在Android中如何實現類似的效果:

首先,我們需要有一個控件來顯示縮略圖,這裡沒有什麼比GridView更加合適了。

配置文件如下:

  1. <?xml version="1.0" encoding="utf-8"?>
  2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  3. android:orientation="vertical"
  4. android:layout_width="fill_parent"
  5. android:layout_height="fill_parent"
  6. >
  7. <GridView
  8. android:id="@+id/view_photos"
  9. android:layout_width="fill_parent"
  10. android:layout_height="fill_parent"
  11. android:layout_marginTop="10dp"
  12. android:columnWidth="100dp"
  13. android:numColumns="auto_fit"
  14. android:horizontalSpacing="5dp"
  15. android:verticalSpacing="5dp"
  16. android:listSelector="@drawable/frame_select" />
  17. </LinearLayout>

對於GridView中每一項是一張縮略圖,我們需要繼承BaseAdapter,實現自己的一個GridImageAdapter,代碼:

  1. package com.liner.manager;
  2. import java.util.List;
  3. import com.liner.manager.adapter.GridImageAdapter;
  4. import android.app.Activity;
  5. import android.graphics.Bitmap;
  6. import android.os.Bundle;
  7. import android.view.View;
  8. import android.widget.AdapterView;
  9. import android.widget.Gallery;
  10. import android.widget.ImageButton;
  11. import android.widget.AdapterView.OnItemClickListener;
  12. public class GalleryActivity extends Activity{
  13. private ImageButton currentImage;
  14. private Gallery gallery;
  15. private int[] thumbIds;
  16. private int currentPos;
  17. private Bitmap currentBitmap;
  18. private List<Bitmap> bitmapCache;
  19. public void onCreate(Bundle savedInstanceState){
  20. super.onCreate(savedInstanceState);
  21. setContentView(R.layout.gallery);
  22. currentImage = (ImageButton)this.findViewById(R.id.image_current);
  23. gallery = (Gallery)this.findViewById(R.id.image_gallery);
  24. gallery.setOnItemClickListener(galleryItemClickListener);
  25. init();
  26. }
  27. private OnItemClickListener galleryItemClickListener = new OnItemClickListener() {
  28. @Override
  29. public void onItemClick(AdapterView<?> p, View v, int position,
  30. long id) {
  31. // 點擊事件
  32. showCurrentImage(position);
  33. }
  34. };
  35. private void init(){
  36. thumbIds = this.getIntent().getIntArrayExtra("thumbIds");
  37. currentPos = this.getIntent().getIntExtra("currentPos",0);
  38. //galleryIds = this.getThumbnailIds(currentPos); //當前的gallery裡的圖片信息
  39. bitmapCache = BitmapUtils.queryThumbnailListByIds(this, thumbIds);
  40. GridImageAdapter adapter = new GridImageAdapter(this.getApplication(), bitmapCache);
  41. gallery.setAdapter(adapter);
  42. gallery.setSelection(currentPos);
  43. showCurrentImage(currentPos);
  44. }
  45. private void showCurrentImage(int position){
  46. if(currentBitmap != null){
  47. currentBitmap.recycle();
  48. }
  49. currentBitmap = BitmapUtils.queryImageByThumbnailId(GalleryActivity.this, thumbIds[position]);
  50. if(currentBitmap != null){
  51. currentImage.setImageBitmap(currentBitmap);
  52. }else{
  53. //什麼都不做
  54. }
  55. //releaseBitmaps();
  56. }
  57. /**
  58. * 將Gallery當前可見的顯示之前的3張,後3張緩存起來,其余的釋放掉,這樣是為了放置內存不夠用
  59. * 之所以前三張後三張,是為了Gallery可以滑動的更加順暢
  60. */
  61. private void releaseBitmaps(){
  62. int start = gallery.getFirstVisiblePosition()-3; //緩存的起始位置
  63. int end = gallery.getLastVisiblePosition()+3; //緩存的結束位置
  64. Bitmap delBitmap;
  65. for(int i=0; i<start; i++){
  66. delBitmap = bitmapCache.get(i);
  67. if(delBitmap != null){
  68. bitmapCache.remove(i);
  69. delBitmap.recycle();
  70. }
  71. }
  72. for(int i=end+1; i<bitmapCache.size(); i++){
  73. delBitmap = bitmapCache.get(i);
  74. if(delBitmap != null){
  75. bitmapCache.remove(i);
  76. delBitmap.recycle();
  77. }
  78. }
  79. }
  80. /**
  81. * 獲取當前位置的前三個Id和後三個Id
  82. * @param position
  83. * @return
  84. */
  85. private Integer[] getThumbnailIds(int position){
  86. Integer[] ids = new Integer[]{0,0,0,0,0,0,0};
  87. int currPos = 0;
  88. //關於這裡的處理,比較復雜
  89. for(int i=3; i>0; i--){
  90. if(position - i >= 0){
  91. currPos = 3-i;
  92. ids[currPos] = thumbIds[position-i];
  93. }
  94. }
  95. ids[++currPos] = thumbIds[position]; //當前Id
  96. //currGallerySelection = currPos;
  97. //這樣右邊剩下的位置數就是7-currPos-1
  98. for(int i=1; i<=6-currPos;i++){
  99. if(position+i < thumbIds.length){
  100. ids[currPos+i] = thumbIds[position+i];
  101. }
  102. }
  103. return ids;
  104. }
  105. }
Copyright © Linux教程網 All Rights Reserved