Android仿百度图片查看功能

  

我们知道,进入百度图片后,输入一个关键字后,首先看到的是很多缩略图,当我们点击某张缩略图时,我们就可以进入到大图显示页面,在大图显示页面,中包含了一个图片画廊,同时当前大图为刚刚我们点击的那张图片。现在我们看看在Android中如何实现类似的效果:,
  

  

首先,我们需要有一个控件来显示缩略图,这里没有什么比显示数据表格更加合适了只
  

  

配置文件如下:,
  

        & lt; & # 63; xml version=" 1.0 " encoding=" utf - 8 " & # 63;比;   & lt; LinearLayout xmlns: android=" http://schemas.android.com/apk/res/android "   android:取向=按怪薄?   android: layout_width="宽和"   android: layout_height="宽和"   比;   & lt;显示数据表格   android: id=癅 + id/view_photos”   android: layout_width="宽和"   android: layout_height="宽和"   android: layout_marginTop=" 10 dp”   android: columnWidth=" 100 dp”   android: numColumns=" auto_fit "   android: horizontalSpacing=" 5 dp "   android: verticalSpacing=" 5 dp "   android: listSelector=癅drawable/frame_select”/比;   & lt;/LinearLayout>   之前      

对于显示数据表格中每一项是一张缩略图,我们需要继承BaseAdapter,实现自己的一个GridImageAdapter,代码:
  

        包com.liner.manager;   进口并不知道;   进口com.liner.manager.adapter.GridImageAdapter;   进口android.app.Activity;   进口android.graphics.Bitmap;   进口android.os.Bundle;   进口android.view.View;   进口android.widget.AdapterView;   进口android.widget.Gallery;   进口android.widget.ImageButton;   进口android.widget.AdapterView.OnItemClickListener;   公开课GalleryActivity延伸活动{      私人ImageButton currentImage;   私人画廊画廊;      私人int [] thumbIds;   私人int currentPos;      私人位图currentBitmap;      私人ListbitmapCache;      公共空间>   包com.liner.manager;   进口java.util.ArrayList;   进口并不知道;   进口com.liner.manager.adapter.GridImageAdapter;   进口android.app.Activity;   进口android.content.Intent;   进口android.database.Cursor;   进口android.graphics.Bitmap;   进口android.graphics.BitmapFactory;   进口android.net.Uri;   进口android.os.Bundle;   进口android.provider.MediaStore;   进口android.view.View;   进口android.widget.Adapter;   进口android.widget.AdapterView;   进口android.widget.GridView;   进口android.widget.Toast;   公开课MainActivity延伸活动{   私人GridView photoView;   私人GridImageAdapter imageAdapter;      私人光标指针;   私人int [] thumbIds;      @Override   公共空间>   & lt; & # 63; xml version=" 1.0 " encoding=" utf - 8 " & # 63;比;   LinearLayout & lt;   xmlns: android=" http://schemas.android.com/apk/res/android "   android: layout_width="宽和"   android: layout_height="宽和"   面向android:=按怪薄北?   & lt;画廊   android: id=癅 + id/image_gallery”   android: layout_width="宽和"   android: layout_height=" 100 dp”/比;   & lt; ImageButton   android: id=癅 + id/image_current”   android: layout_width="宽和"   android: layout_height="宽和"   android:填充=" 10 dp”   android: layout_marginTop=" 10 dp”/比;   & lt;/LinearLayout>   之前      

然后,对应的活动如下:
  

        包com.liner.manager;   进口并不知道;   进口com.liner.manager.adapter.GridImageAdapter;   进口android.app.Activity;   进口android.graphics.Bitmap;   进口android.os.Bundle;   进口android.view.View;   进口android.widget.AdapterView;   进口android.widget.Gallery;   进口android.widget.ImageButton;   进口android.widget.AdapterView.OnItemClickListener;   公开课GalleryActivity延伸活动{      私人ImageButton currentImage;   私人画廊画廊;      私人int [] thumbIds;   私人int currentPos;      私人位图currentBitmap;      私人ListbitmapCache;      公共空间>   包com.liner.manager;   进口java.util.ArrayList;   进口并不知道;   进口android.app.Activity;   进口android.database.Cursor;   进口android.graphics.Bitmap;   进口android.graphics.BitmapFactory;   进口android.provider.MediaStore;   进口android.util.Log;   最后公共类BitmapUtils {            公共静态位图decodeBitmap (String路径,int displayWidth, int displayHeight) {   BitmapFactory。选择op=new BitmapFactory.Options ();   op.inJustDecodeBounds=true;   位图bmp=BitmapFactory.decodeFile(路径,op);//获取尺寸信息//获取比例大小   int wRatio=(int) Math.ceil (op.outWidth/(浮动)displayWidth);   int hRatio=(int) Math.ceil (op.outHeight/(浮动)displayHeight);//如果超出指定大小,则缩小相应的比例   如果(wRatio比;1,,hRatio祝辞1){   如果(wRatio比;hRatio) {   op.inSampleSize=wRatio;   其他}{   op.inSampleSize=hRatio;   }   }   op.inJustDecodeBounds=false;   bmp=BitmapFactory.decodeFile(路径,op);   返回位图。createScaledBitmap (bmp、displayWidth displayHeight,真实);   }/* *   *采用复杂计算来决定缩放   * @param路径   * @param maxImageSize   * @return   */公共静态位图decodeBitmap (String路径,int maxImageSize) {   BitmapFactory。选择op=new BitmapFactory.Options ();   op.inJustDecodeBounds=true;   位图bmp=BitmapFactory.decodeFile(路径,op);//获取尺寸信息   int规模=1;   如果(凤凰社)。outWidth祝辞maxImageSize | | op.outHeight祝辞maxImageSize) {   规模=(int)数学。战俘(2 (int) Math.round (Math.log (maxImageSize/(双)Math.max (op。outWidth op.outHeight))/Math.log (0.5)));   }   op.inJustDecodeBounds=false;   op.inSampleSize=规模;   bmp=BitmapFactory.decodeFile(路径,op);   返回bmp;   }         公共静态游标queryThumbnails(活动上下文){   新String []={String[]列   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null

Android仿百度图片查看功能