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)); } } } }