转 http://www.tuicool.com/articles/B7JNv2
电子相册的思路:
1.先是考虑布局,我用的是GridView布局
2.GridView中又该怎么显示图片,其实我的这个应用每个图片都是同一个布局,首先要实现适配器接口,再利用充气泵LayoutInflater把布局文件转换成View视图对象
3.怎么从服务器获取图片,又是怎么捉去到的
有思路不等于你会了,直接给你上代码吧:
首先介绍下我的应用的功能:
1.显示的每一张图片,点击后都可以显示出单独的一张,并且是全屏
2.如果点击其中的任意一张图片长时间,会弹出窗口,显示这张图片的基本的信息
不足:
1.现在存在获取资源过多,内存溢出的bug,后期我会处理,可以选择把图片保存到本地,不全部从服务器获取
2.功能不够强大,我还会用另一个布局,把电子相册的效果呈现处理
MainActivity.java源码:
package com.example.photo;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import android.annotation.SuppressLint;
import android.app.Activity;
import android.app.AlertDialog;
import android.content.Intent;
import android.graphics.Bitmap;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.AdapterView.OnItemLongClickListener;
import android.widget.BaseAdapter;
import android.widget.GridView;
import android.widget.ImageView;
import android.widget.ProgressBar;
import android.widget.TextView;
/**
* 2013-6-15 下午12:54:56
*
* @author 乔晓松
*/
public class MainActivity extends Activity {
protected static final int TEXT = 0;
protected static final int ACTIVITY = 1;
public GridView gridView;
public int id;
public Handler handler;
public static Object[] path; // = { "span.jpg", "span1.jpg", "span2.jpg",
// "span3.jpeg", "psb.jpeg", "psbpan.jpeg"
// };
public String basePath = "http://172.22.64.6:8080/lession08_image/images/";
public List<Map<String, Object>> datas;
public LayoutInflater inflater;
public Map<String, Integer> fileMap;
public ProgressBar progressBar;
@SuppressLint({ "HandlerLeak", "CutPasteId" })
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
gridView = (GridView) findViewById(R.id.gridView1);
progressBar = (ProgressBar) findViewById(R.id.progressBar1);
progressBar.setVisibility(View.VISIBLE);
inflater = (LayoutInflater) this
.getSystemService(LAYOUT_INFLATER_SERVICE);
datas = new ArrayList<Map<String, Object>>();
fileMap = new HashMap<String, Integer>();
handler = new Handler() {
@Override
public void handleMessage(Message msg) {
super.handleMessage(msg);
switch (msg.what) {
case TEXT:
progressBar.setVisibility(View.GONE);
gridView.setAdapter(new MyListAdapter());
break;
default:
break;
}
}
};
new Thread(new Runnable() {
@Override
public void run() {
List<String> list = HttpClientTool
.httpClientJSON("http://172.22.64.6:8080/lession08_image/csdn/ImagesAction_httpAllImages.action");
if (list != null) {
path = list.toArray();
}
for (int i = 0; i < path.length; i++) {
Map<String, Object> map = new HashMap<String, Object>();
Map<String, Object> bitmap = HttpClientTool.send(