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)); } } }}
首先,对于手机内部,当余户第一次浏览图片时,会在其/sdcard/DCIM目录生成图片对应的Thumbnails。可能在File Explorer中看不到,但是通过代码可以看到其thumbnail文件(可参考附件)。当然如果你想直接使用thumbnail的图片,这样你就要解决一个Thumbnails图片与原始图片的一个映射关系。关于其映射关系,这个以后会谈到。说先在这里,我做的是对原始图片直接的一个Scale【Android图片的处理大部分是通过Bitmap的图像处理方法实现的】。
下面代码实现了原始图片的剪切和反转:
package com.daisy.android.components;import android.app.Activity;import android.content.Intent;import android.graphics.Bitmap;import android.graphics.BitmapFactory;import android.graphics.Matrix;import android.graphics.drawable.Drawable;import android.os.Bundle;import android.util.Log;import android.view.Menu;import android.view.MenuItem;import android.widget.ImageView;import android.widget.Toast;/** [email protected] Andrew.Lee [email protected] 2011-6-8 下午03:11:13 [email protected] 1.0 [email protected] */public class ImageViewer extends Activity { private static String TAG = "ImageView"; private ImageView imageView; private Intent intent; private Drawable drawable; private String imagePath; public void onCreate(Bundle bundle) { super.onCreate(bundle); setContentView(R.layout.image); findViews(); } public void findViews() { imageView = (ImageView) findViewById(R.id.image); intent = this.getIntent(); imagePath = intent.getStringExtra("path"); Log.i(TAG, "image path:" + imagePath + "======"); drawable = Drawable.createFromPath(imagePath); imageView.setImageDrawable(drawable); } public boolean onCreateOptionsMenu(Menu menu) { boolean result = super.onCreateOptionsMenu(menu); menu.add("process"); return result; } @Override public boolean onOptionsItemSelected(MenuItem item) { // TODO Auto-generated method stub if (item.getTitle().equals("process")) { Bitmap bitmap = BitmapFactory.decodeFile(imagePath); int width = bitmap.getWidth(); int height = bitmap.getHeight(); Matrix m = new Matrix(); m.setRotate(45); bitmap = Bitmap.createBitmap(bitmap, (width - 100) / 2, (height - 100) / 2, 100, 100, m, true); imageView.setScaleType(ImageView.ScaleType.CENTER); imageView.setImageBitmap(bitmap); } return super.onOptionsItemSelected(item); }}
从上面代码可以看到,对于图片的处理,都是通过生成Bitmap对象再进行操作的。
1 楼 Javafedora 2011-07-23
能给一下XML文件不??
2 楼 W89910410 2011-09-25
Javafedora 写道
能给一下XML文件不??
能不能给下xml文件