Android 關於GridView控件的處理
本人一直以為做一般的Android應用開發,其中創意也就是有好的Idea是至關重要的,其次是要有解決一般問題的想法,如如何實現一個應用兼容多種屏幕分辨率的問題(即使android對於圖片的處理,默認情況先是自適應的,即等比例或縮放圖片),如何自己實現網絡圖片的分段下載,如何實現不同Activity之間的數據傳遞問題,當Listview中數據量過大時,如何實現數據的分批下載等問題。結果上周去參加了一個面試,嗚嗚---,讓當場編寫一個GridView實現手機SDcard內置圖片的Thumb nail以及圖片的修剪也翻轉問題。以前都是現用現查文檔或者幫助的,以至於當時傻眼了,控件都不知道怎麼用,哎,離開了網絡感覺什麼都干不了啊,今天下午有些閒暇就整理了一個應用,實現其功能。先看下源代碼實現了對圖片的Thumbnails功能:
- package com.daisy.android.components;
-
- import java.io.File;
- import java.util.ArrayList;
- import java.util.HashMap;
- import java.util.List;
- import java.util.Map;
-
- import android.app.Activity;
- import android.content.Context;
- import android.content.Intent;
- import android.graphics.Bitmap;
- import android.graphics.BitmapFactory;
- import android.net.Uri;
- import android.os.Bundle;
- import android.util.Log;
- import android.view.View;
- import android.widget.AdapterView;
- import android.widget.GridView;
- import android.widget.ImageView;
- import android.widget.ListAdapter;
- import android.widget.SimpleAdapter;
- import android.widget.AdapterView.OnItemClickListener;
-
- public class GridViewActivity extends Activity {
- public static String TAG = "Grid_view";
- private GridView gridView;
- private ArrayList<HashMap<String, String>> list;
-
- /** Called when the activity is first created. */
- @Override
- public void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.main);
- findView();
- }
-
- private void findView() {
- gridView = (GridView) findViewById(R.id.gridview);
- list = new ArrayList();
- filepath("/sdcard");
- // test the contents in the list
- for (int i = 0; i < list.size(); i++) {
- Log.i(TAG, list.get(i).get("path") + "-----");
- }
- String[] from = { "path" };
- int[] to = { R.id.imageView };
- ListAdapter adapter = new GridAdapter(this, list, R.layout.item, from,
- to);
- gridView.setAdapter(adapter);
- gridView.setOnItemClickListener(listener);
- }
-
- OnItemClickListener listener = new OnItemClickListener() {
-
- @Override
- public void onItemClick(AdapterView<?> parent, View view, int position,
- long id) {
- // TODO Auto-generated method stub
- String imagePath = list.get(position).get("path");
- Intent intent = new Intent(GridViewActivity.this, ImageViewer.class);
- intent.putExtra("path", imagePath);
- startActivity(intent);
- }
- };
-
- public void filepath(String str) {
- Log.i(TAG, str + "---path---");
- File file = new File(str);
- if (file.isDirectory()) {
- String[] filePath = file.list();
- for (String path : filePath) {
- file = new File(str + "/" + path);
- if (file.isDirectory() && !path.equals("DCIM")) {
- filepath(str + "/" + path);
- } else if (path.endsWith(".jpg")) {
- HashMap hash = new HashMap();
- hash.put("path", str + "/" + path);
- list.add(hash);
- }
- }
- }
- }
-
- class GridAdapter extends SimpleAdapter {
-
- public GridAdapter(Context context,
- List<? extends Map<String, ?>> data, int resource,
- String[] from, int[] to) {
- super(context, data, resource, from, to);
- // TODO Auto-generated constructor stub
- }
-
- // set the imageView using the path of image
- public void setViewImage(ImageView v, String value) {
- try {
- Bitmap bitmap = BitmapFactory.decodeFile(value);
- Bitmap newBit = Bitmap
- .createScaledBitmap(bitmap, 100, 80, true);
- v.setImageBitmap(newBit);
- } catch (NumberFormatException nfe) {
- v.setImageURI(Uri.parse(value));
- }
- }
- }
-
- }