源文章:

图片浏览器功能:

1、可以添加本地图片到该图片浏览器

2、添加的同时可以给图片命名

3、点击添加到列表的按钮,图片就会显示在列表界面。

4、列表界面提供图片编辑功能,包括:点击图片名称可以进行重命名,点击删除操作可以将图片从列表界面移除,点击图片缩略图可以进行该图片全屏显示。

注:该列表界面需要保存相关信息,确保每次启动该图片浏览器时列表显示的内容与上一次退出时显示的内容一致

 

具体实现:

1、新建项目,编写主界面布局文件  ,图片列表通过一个ListView显示



1 <?xml version="1.0" encoding="utf-8"?>
 2 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
 3    android:layout_width="fill_parent"
 4    android:layout_height="fill_parent"
 5    android:orientation="vertical" >
 6  
 7    <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
 8     android:layout_width="fill_parent"
 9     android:layout_height="wrap_content"
10     android:padding="10dip"
11     >
12      <TextView
13             android:id="@+id/textView1"
14             android:layout_width="wrap_content"
15             android:layout_height="wrap_content"
16             android:layout_alignParentLeft="true"
17             android:text="名称:" />
18 
19 
20         <EditText
21             android:id="@+id/editName"
22             android:layout_width="wrap_content"
23             android:layout_height="wrap_content"
24       android:layout_centerInParent="true"
25             android:layout_toRightOf="@id/textView1"
26             android:hint="请输入图片名称"
27 />
28         
29         <Button
30             android:id="@+id/btnPickPic"
31             android:layout_width="wrap_content"
32             android:layout_height="wrap_content"
33             android:layout_alignParentRight="true"
34             android:text="选择图片" />
35     
36       </RelativeLayout>
37       
38      <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
39     android:layout_width="fill_parent"
40     android:layout_height="wrap_content"
41     android:padding="10dip">
42 
43 
44     <TextView
45         android:id="@+id/label"
46         android:layout_width="wrap_content"
47         android:layout_height="wrap_content"
48         android:layout_alignParentLeft="true"
49         android:text="预览图"/>
50 
51 
52     <ImageView
53         android:id="@+id/preView"
54         android:layout_width="wrap_content"
55         android:layout_height="wrap_content"
56         android:layout_centerInParent="true"
57         android:maxWidth="100px"
58         android:maxHeight="100px"
59         android:layout_toRightOf="@id/label"/>
60 
61 
62         <Button
63             android:id="@+id/btnAdd"
64             android:layout_width="wrap_content"
65             android:layout_height="wrap_content"
66             android:layout_alignParentRight="true"
67             android:text="加到列表" />
68   
69 </RelativeLayout>
70       
71    <ListView
72        android:id="@+id/listview"
73        android:layout_width="match_parent"
74        android:layout_height="match_parent"
75        ></ListView>
76  
77 </LinearLayout>



2、listview.xml



<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent" >


    <ImageView
        android:id="@+id/img"
        android:layout_width="80dip"
        android:layout_height="80dip"
        android:layout_alignParentLeft="true"
        android:layout_alignParentTop="true"
        android:layout_marginLeft="10dp"
        android:layout_marginTop="10dp"
        />


    <TextView
        android:id="@+id/title"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignBottom="@+id/img"
        android:layout_toRightOf="@+id/img"
        android:layout_centerVertical="true"
         />


    <Button
        android:id="@+id/view_btn"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentRight="true"
        android:layout_centerVertical="true"
        android:layout_alignTop="@+id/img"
        android:text="删除" />


</RelativeLayout>



3、实现添加本地图片到该应用,添加按钮事件



1 private Button btnPicPicker; //选择图片按钮
 2 private Button btnPicAdd;//添加到列表按钮
 3 private Uri mPicUri;//系统返回的图片 Uri
 4 private Cursor mPictureCursor;              //图片的明细
 5 private TextView tvPicName;//列表中图片的名称
 6 
 7 
 8     /** Called when the activity is first created. */
 9     @Override
10     public void onCreate(Bundle savedInstanceState) {
11         super.onCreate(savedInstanceState);
12         setContentView(R.layout.main);
13         
14         btnPicPicker = (Button)findViewById(R.id.btnPickPic);
15         btnPicPicker.setOnClickListener(this);
16     }
17 
18 
19 
20     /**
21      * 监听UI点击事件
22      */
23 public void onClick(View v) {
24 // TODO Auto-generated method stub
25 int mViewId = v.getId();
26 switch(mViewId){
27 case R.id.btnPickPic:
28 findPicture();
29 break;
30 default:break;
31 }
32 }
33 
34 
35 
36 
37 /**
38 * 查找系统图片
39 */
40 private void findPicture() {
41 // TODO Auto-generated method stub
42 Intent intent = new Intent();
43 intent.setType("image/*");
44 intent.setAction(Intent.ACTION_GET_CONTENT);
45 startActivityForResult(intent,Constanse.PICTURE_SEARCHER_FLAG);
46 }
47 
48 }



4、返回图片后的处理 重写onActivityResult

先创建一个图片的Bean:



1 package com.bellsong.pv;
 2 
 3 import android.graphics.Bitmap;
 4 
 5 
 6 public class Picture {
 7 
 8 private int id;
 9 private String picName;
10 private String picUri;
11 private Bitmap picBitmap;
12 
13 
14 public Picture(){
15 
16 }
17 public Picture (String picName,String picUri){
18 this.picName = picName;
19 this.picUri = picUri;
20 }
21 public String getPicName() {
22 return picName;
23 }
24 public void setPicName(String picName) {
25 this.picName = picName;
26 }
27 public String getPicUri() {
28 return picUri;
29 }
30 public void setPicUri(String picUri) {
31 this.picUri = picUri;
32 }
33 public Bitmap getPicBitmap() {
34 return picBitmap;
35 }
36 public void setPicBitmap(Bitmap picBitmap) {
37 this.picBitmap = picBitmap;
38 }
39 public int getId() {
40 return id;
41 }
42 public void setId(int id) {
43 this.id = id;
44 }
45 public void clear(){
46 picName = null;
47 picUri = null;
48 picBitmap = null;
49 }
50 
51 }



当前PicView16Activity:



1 package com.bellsong.pv;
  2 
  3 
  4 import java.io.FileNotFoundException;
  5 import java.util.ArrayList;
  6 import java.util.List;
  7 
  8 
  9 //
 10 //import com.bellsong.picview.DBManager;
 11 //import com.bellsong.picview.Picture;
 12 //import com.bellsong.picview.PicView15Activity.MyAdapter;
 13 
 14 
 15 import android.app.Activity;
 16 import android.content.ContentResolver;
 17 import android.content.Intent;
 18 import android.database.Cursor;
 19 import android.graphics.Bitmap;
 20 import android.graphics.BitmapFactory;
 21 import android.net.Uri;
 22 import android.os.Bundle;
 23 import android.util.Log;
 24 import android.view.View;
 25 import android.view.View.OnClickListener;
 26 import android.widget.Button;
 27 import android.widget.ImageView;
 28 import android.widget.ListView;
 29 import android.widget.TextView;
 30 
 31 
 32 public class PicView16Activity extends Activity implements OnClickListener {
 33 
 34 private Button btnPicPicker;//选择图片按钮
 35 private Button btnPicAdd;//添加到列表按钮
 36 private TextView tvPicName;//列表中图片的名称
 37 private ImageView ivPicPre;                 //预览图
 38 
 39 private Uri mPicUri;//系统返回的图片 Uri
 40 private Cursor mPictureCursor;              //图片的明细
 41 
 42 
 43 private Picture mCurrentPicture;            //当前图片
 44 
 45 
 46 
 47 
 48 
 49     /** Called when the activity is first created. */
 50     @Override
 51     public void onCreate(Bundle savedInstanceState) {
 52         super.onCreate(savedInstanceState);
 53         setContentView(R.layout.main);
 54         
 55         //实例化数据
 56         mCurrentPicture = new Picture();
 57         
 58         
 59         btnPicPicker = (Button)findViewById(R.id.btnPickPic);
 60         ivPicPre     = (ImageView)findViewById(R.id.preView);
 61         tvPicName    = (TextView)findViewById(R.id.editName);
 62         
 63         btnPicPicker.setOnClickListener(this);
 64     }
 65 
 66 
 67 
 68 
 69 
 70 
 71     /**
 72      * 监听UI点击事件
 73      */
 74 public void onClick(View v) {
 75 // TODO Auto-generated method stub
 76 int mViewId = v.getId();
 77 switch(mViewId){
 78 case R.id.btnPickPic:
 79 findPicture();
 80 break;
 81 default:break;
 82 }
 83 }
 84 
 85 
 86 
 87 
 88 /**
 89 * 查找系统图片
 90 */
 91 private void findPicture() {
 92 // TODO Auto-generated method stub
 93 Intent intent = new Intent();
 94 intent.setType("image/*");
 95 intent.setAction(Intent.ACTION_GET_CONTENT);
 96 startActivityForResult(intent,Constanse.PICTURE_SEARCHER_FLAG);
 97 }
 98 
 99 
100 
101 
102 
103 
104 /**
105 * 返回图片后的处理 
106 * 根据uri获得图片的信息,显示在界面上
107 */
108 @Override
109 protected void onActivityResult(int requestCode, int resultCode, Intent data) {
110 // TODO Auto-generated method stub
111 super.onActivityResult(requestCode, resultCode, data);
112 
113 if(requestCode == Constanse.PICTURE_SEARCHER_FLAG){
114 if(resultCode == RESULT_OK){
115 //取得图片的Uri
116 mPicUri = data.getData();
117 ContentResolver mContenResolver = this.getContentResolver();
118 //解析Uri里边的信息
119    mPictureCursor = mContenResolver.query(mPicUri, null, null, null, null);
120 mPictureCursor.moveToFirst();
121 
122 //获取图片明细信息   ---开发时使用
123 // for(int i = 0; i<mPictureCursor.getColumnCount();i++){
124 // Log.i("test",i+"-----------------"+mPictureCursor.getString(i));
125 // }
126 try {
127 //清空当前图片
128 mCurrentPicture.clear();
129 Bitmap bitmap =BitmapFactory.decodeStream(mContenResolver.openInputStream(mPicUri));
130 mCurrentPicture.setPicBitmap(bitmap);
131 String picname = mPictureCursor.getString(5);
132 mCurrentPicture.setPicName(picname);
133 mCurrentPicture.setPicUri(mPictureCursor.getString(1));
134 
135 //显示在UI中的预览图和图片名字
136 ivPicPre.setImageBitmap(bitmap);
137 tvPicName.setText(picname);
138 } catch (FileNotFoundException e) {
139 e.printStackTrace();
140 }finally{
141 mPictureCursor.close();
142 }
143 }
144 }
145 }
146 }



5、添加数据库

1)创建数据库,保存图片的名字 uri



1 DatabaseHelper.java
 2 
 3 package com.bellsong.pv;
 4 
 5 
 6 import android.content.Context;
 7 import android.database.sqlite.SQLiteDatabase;
 8 import android.database.sqlite.SQLiteOpenHelper;
 9 import android.database.sqlite.SQLiteDatabase.CursorFactory;
10 
11 
12 public class DatabaseHelper extends SQLiteOpenHelper{
13 
14 
15 
16 
17 public DatabaseHelper(Context context, String name, CursorFactory factory,
18 int version) {
19 super(context, name, factory, version);
20 // TODO Auto-generated constructor stub
21 }
22 
23 
24 public DatabaseHelper(Context context) {
25 // TODO Auto-generated constructor stub
26 super(context, Constanse.DATABASE_NAME, null, Constanse.DATABASE_VERSION);
27 }
28 
29 
30 @Override
31 public void onCreate(SQLiteDatabase db) {
32 // TODO Auto-generated method stub
33 db.execSQL("CREATE TABLE IF NOT EXISTS pic" +  
34         "(_id INTEGER PRIMARY KEY AUTOINCREMENT, name VARCHAR, uri VARCHAR)");  
35 }
36 
37 
38 @Override
39 public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
40 // TODO Auto-generated method stub
41 db.execSQL("ALTER TABLE pic ADD COLUMN other STRING"); 
42 }
43 }



2)封装一个数据库工具类



1 package com.bellsong.pv;
  2 
  3 
  4 import java.util.ArrayList;
  5 import java.util.List;
  6 
  7 
  8 import android.content.ContentValues;
  9 import android.content.Context;
 10 import android.database.Cursor;
 11 import android.database.SQLException;
 12 import android.database.sqlite.SQLiteDatabase;
 13 import android.graphics.BitmapFactory;
 14 
 15 
 16 /**
 17  * 数据库工具类
 18  * @author Administrator
 19  *
 20  */
 21 public class DBManager {
 22 private DatabaseHelper helper;
 23 private SQLiteDatabase db;
 24 
 25 /**
 26 * @param context
 27 * 简要说明:因为getWritableDatabase内部调用了mContext.openOrCreateDatabase(mName, 0, mFactory);  
 28 * 所以要确保context已初始化,我们可以把实例化DBManager的步骤放在Activity的onCreate里  
 29 */
 30 public DBManager(Context context) {  
 31        helper = new DatabaseHelper(context);  
 32        db = helper.getWritableDatabase();  
 33    }  
 34  
 35 /**
 36  * 添加图片
 37  * 
 38  */
 39 public int addOne(Picture picture){
 40 int tableId = -1;
 41 db.beginTransaction();  //开始事务  
 42 try{
 43 ContentValues cv = new ContentValues();  
 44     cv.put("name", picture.getPicName()); 
 45     cv.put("uri", picture.getPicUri()); 
 46     //返回一个id,用来判断是否插入成功
 47     tableId = (int)db.insert("pic", null, cv);
 48 db.setTransactionSuccessful();  //设置事务成功完成  
 49 }
 50 catch(SQLException e){
 51 }
 52 finally{
 53 db.endTransaction();    //结束事务  
 54 }
 55 return tableId;
 56 }
 57 
 58 
 59    /** 
 60     * 更新图片名称
 61     * @param Picture 
 62     */  
 63    public boolean updateName(Picture picture) {  
 64     db.beginTransaction();  //开启事务
 65     try{
 66     ContentValues cv = new ContentValues();  
 67     cv.put("name", picture.getPicName());  
 68     db.update("pic", cv, "_id = "+picture.getId(), null);  
 69     db.setTransactionSuccessful();  //设置事务成功完成  
 70     }
 71        catch(SQLException e){
 72 return false;
 73 }finally{
 74          db.endTransaction();    //结束事务  
 75        }
 76        return true;
 77    }  
 78      
 79    /** 
 80     * 删除图片
 81     * @param Picture 
 82     */  
 83    public boolean deleteOldPicture(Picture picture) {  
 84        
 85 db.beginTransaction();  //开始事务  
 86 try{
 87 db.delete("pic", "_id = " + picture.getId(), null);  
 88 db.setTransactionSuccessful();  //设置事务成功完成  
 89 }
 90 catch(SQLException e){
 91 return false;
 92 }
 93 finally{
 94   
 95 db.endTransaction();    //结束事务  
 96 }
 97 return true;
 98        
 99    }  
100      
101    /** 
102     * 查询图片
103     * @return List<Picture> 
104     */  
105    public List<Picture> query() {  
106        ArrayList<Picture> pictures = new ArrayList<Picture>();  
107        Cursor c = queryTheCursor();  
108        while (c.moveToNext()) {  
109            Picture picture = new Picture();  
110            picture.setPicName(c.getString(c.getColumnIndex("name")));  
111            picture.setPicUri(c.getString(c.getColumnIndex("uri")));
112            picture.setPicBitmap(BitmapFactory.decodeFile(picture.getPicUri()));
113            picture.setId(c.getInt(c.getColumnIndex("_id")));
114            pictures.add(picture);  
115        }  
116        c.close();  
117        return pictures;  
118    }  
119    
120    /**
121     * 判断是否已经存在该图片
122     * @param picture
123     * @return
124     */
125    public boolean queryIfExist(Picture picture){
126     Cursor cr = db.query("pic", null, "name = ?", new String[]{picture.getPicName()}, null, null, null);
127     if(cr.getCount()!=0){
128     return true;
129     }else{
130     return false;
131     }
132    }
133    /** 
134     * 查询图片
135     * @return  Cursor 
136     */  
137    public Cursor queryTheCursor() {  
138        Cursor c = db.rawQuery("SELECT * FROM pic", null);  
139        return c;  
140    }  
141    /** 
142     * 关闭数据库 
143     */  
144    public void closeDB() {  
145        db.close();  
146    }  
147 }



6、实现名字更改,动态增加删除,点击图片预览等功能

详细代码:



1 PicView16Activity.java
  2 
  3 package com.bellsong.pv;
  4 
  5 
  6 import java.io.FileNotFoundException;
  7 import java.util.ArrayList;
  8 import java.util.List;
  9 
 10 
 11 import android.app.Activity;
 12 import android.app.AlertDialog;
 13 import android.content.ContentResolver;
 14 import android.content.Context;
 15 import android.content.DialogInterface;
 16 import android.content.Intent;
 17 import android.database.Cursor;
 18 import android.graphics.Bitmap;
 19 import android.graphics.BitmapFactory;
 20 import android.net.Uri;
 21 import android.os.AsyncTask;
 22 import android.os.Bundle;
 23 import android.util.Log;
 24 import android.view.LayoutInflater;
 25 import android.view.View;
 26 import android.view.View.OnClickListener;
 27 import android.view.ViewGroup;
 28 import android.widget.BaseAdapter;
 29 import android.widget.Button;
 30 import android.widget.EditText;
 31 import android.widget.ImageView;
 32 import android.widget.ListView;
 33 import android.widget.TextView;
 34 import android.widget.Toast;
 35 
 36 
 37 public class PicView16Activity extends Activity implements OnClickListener {
 38 
 39 private Button btnPicPicker;//选择图片按钮
 40 private Button btnPicAdd;//添加到列表按钮
 41 private Button btnPicDel;//删除图片按钮
 42 private TextView tvPicName;//列表中图片的名称
 43 private ImageView ivPicPre;                 //预览图
 44 
 45 private ListView listview;//图片列表
 46 private View reNameView;//重命名视图
 47 
 48 private ImageView  img;
 49 private TextView   title;
 50 
 51 private Uri mPicUri;//系统返回的图片 Uri
 52 private Cursor mPictureCursor;              //图片的明细
 53 private DBManager mPicDatabase;//数据库操作类
 54 
 55 
 56 private Picture mCurrentPicture;            //当前图片
 57 private List<Picture> mData;//Adapter的数据源
 58 private MyAdapter adapter;
 59 
 60 private ArrayList<String> mPictureUris;//图片路径集
 61 
 62 
 63     /** Called when the activity is first created. */
 64     @Override
 65     public void onCreate(Bundle savedInstanceState) {
 66         super.onCreate(savedInstanceState);
 67         setContentView(R.layout.main);
 68         
 69         //实例化数据
 70         mCurrentPicture = new Picture();
 71         mPicDatabase    = new DBManager(this);
 72         mData           = new ArrayList<Picture>();
 73         mPictureUris    = new ArrayList<String>();
 74         
 75         listview = (ListView)findViewById(R.id.listview);
 76         adapter  = new MyAdapter(this);
 77         listview.setAdapter(adapter);
 78         
 79         //异步Task查询数据库
 80         new LoadData().execute();
 81         
 82         
 83         btnPicPicker = (Button)findViewById(R.id.btnPickPic);
 84         btnPicAdd    = (Button)findViewById(R.id.btnAdd);
 85         
 86         ivPicPre     = (ImageView)findViewById(R.id.preView);
 87         tvPicName    = (TextView)findViewById(R.id.editName);
 88         
 89         
 90         btnPicPicker.setOnClickListener(this);
 91         btnPicAdd.setOnClickListener(this);
 92    
 93         
 94         
 95     }
 96 
 97 
 98 
 99 
100 
101 
102     /**
103      * 监听UI点击事件
104      */
105 public void onClick(View v) {
106 // TODO Auto-generated method stub
107 int mViewId = v.getId();
108 switch(mViewId){
109 case R.id.btnPickPic:
110 findPicture();
111 break;
112 case R.id.btnAdd:
113 addPicture();
114 break;
115 default:break;
116 }
117 }
118 
119 
120 /**
121 * 添加图片到本地数据库
122 */
123 private void addPicture() {
124 // TODO Auto-generated method stub
125 //判断图片名字和图片路径是否为空
126   if(!(tvPicName.getText().toString().equals("")) && mCurrentPicture.getPicUri() != null && mCurrentPicture.getPicUri().length() > 0){
127     //取得当前EditText中的图片名字
128     mCurrentPicture.setPicName(tvPicName.getText().toString());
129       //判断是否重命名  方法:根据名字 先查询数据库
130       if(!mPicDatabase.queryIfExist(mCurrentPicture)){
131         int mTableId = mPicDatabase.addOne(mCurrentPicture);
132          if(mTableId != -1 ){
133              Picture mPicture = new Picture();
134              mPicture.setId(mCurrentPicture.getId());
135              mPicture.setPicName(mCurrentPicture.getPicName());
136              mPicture.setPicBitmap(mCurrentPicture.getPicBitmap());
137              mPicture.setPicUri(mCurrentPicture.getPicUri());
138              mData.add(mPicture);
139              adapter.notifyDataSetChanged();
140            }else{
141               System.out.println("插入失败");
142            }
143         }else{
144            Toast.makeText(PicView16Activity.this, "该名字已存在,请重命名", Toast.LENGTH_LONG).show();
145         }
146   }
147   else{
148      Toast.makeText(PicView16Activity.this, "请选择图片", Toast.LENGTH_LONG).show();
149 
150   }
151 }
152 
153 
154 /**
155 * 查找系统图片
156 */
157 private void findPicture() {
158 // TODO Auto-generated method stub
159 Intent intent = new Intent();
160 intent.setType("image/*");
161 intent.setAction(Intent.ACTION_GET_CONTENT);
162 startActivityForResult(intent,Constanse.PICTURE_SEARCHER_FLAG);
163 }
164 
165 
166 public void showPicture() {
167 // TODO Auto-generated method stub
168 
169 }
170 
171 
172 
173 /**
174 * 返回图片后的处理 
175 * 根据uri获得图片的信息,显示在界面上
176 */
177 @Override
178 protected void onActivityResult(int requestCode, int resultCode, Intent data) {
179 // TODO Auto-generated method stub
180 super.onActivityResult(requestCode, resultCode, data);
181 
182 if(requestCode == Constanse.PICTURE_SEARCHER_FLAG){
183 if(resultCode == RESULT_OK){
184 //取得图片的Uri
185 mPicUri = data.getData();
186 ContentResolver mContenResolver = this.getContentResolver();
187 //解析Uri里边的信息
188    mPictureCursor = mContenResolver.query(mPicUri, null, null, null, null);
189 mPictureCursor.moveToFirst();
190 
191 //获取图片明细信息   ---开发时使用
192 // for(int i = 0; i<mPictureCursor.getColumnCount();i++){
193 // Log.i("test",i+"-----------------"+mPictureCursor.getString(i));
194 // }
195 
196 try {
197 //清空当前图片
198 mCurrentPicture.clear();
199 Bitmap bitmap =BitmapFactory.decodeStream(mContenResolver.openInputStream(mPicUri));
200 mCurrentPicture.setPicBitmap(bitmap);
201 String picname = mPictureCursor.getString(5);
202 mCurrentPicture.setPicName(picname);
203 mCurrentPicture.setPicUri(mPictureCursor.getString(1));
204 
205 //显示在UI中的预览图和图片名字
206 ivPicPre.setImageBitmap(bitmap);
207 tvPicName.setText(picname);
208 } catch (FileNotFoundException e) {
209 e.printStackTrace();
210 }finally{
211 mPictureCursor.close();
212 }
213 }
214 }
215 }
216 
217 
218 private class MyAdapter extends BaseAdapter{
219 
220 private Context context;
221 private LayoutInflater mInflater;
222  
223 public MyAdapter(Context context){
224        this.mInflater = LayoutInflater.from(context);
225        this.context = context;
226    }
227 
228 
229 public int getCount() {
230 // TODO Auto-generated method stub
231 return mData.size();
232 }
233 
234 
235 public Picture getItem(int position) {
236 // TODO Auto-generated method stub
237 return mData.get(position);
238 }
239 
240 
241 public long getItemId(int position) {
242 // TODO Auto-generated method stub
243 return 0;
244 }
245 
246 
247 public View getView(int position, View convertView, ViewGroup parent) {
248 // TODO Auto-generated method stub
249 final int selectId = position;
250 if(convertView == null){
251 convertView=mInflater.inflate(R.layout.listview, null);
252 }
253 //获得控件对象
254 img = (ImageView)convertView.findViewById(R.id.img);
255 img.setAdjustViewBounds(true);
256 title = (TextView)convertView.findViewById(R.id.title);
257 btnPicDel = (Button)convertView.findViewById(R.id.btnDel);
258 final Picture mPictureView = mData.get(position);
259 
260 
261 img.setImageBitmap(mPictureView.getPicBitmap());
262 title.setText(mPictureView.getPicName());
263 
264 final int mPosition = position;
265 final Picture tempPicture = mPictureView;
266 
267 //删除图片按钮操作
268 btnPicDel.setOnClickListener(new OnClickListener() {
269 
270 public void onClick(View v) {
271 // TODO Auto-generated method stub
272 delPictureInfo(tempPicture,selectId);
273 }
274 });
275 
276 //重命名按钮操作
277 title.setOnClickListener(new OnClickListener() {
278 
279 public void onClick(View v) {
280 Log.i("bell", tempPicture.getId()+"");
281 reNamePicture(tempPicture,mPosition);
282 }
283 });
284 
285 //预览图片按钮操作
286 img.setOnClickListener(new OnClickListener() {
287 
288 public void onClick(View v) {
289 // TODO Auto-generated method stub
290 Intent intent = new Intent();
291 intent.setClass(PicView16Activity.this, PictureView.class);
292 for(Picture pic : mData){
293 mPictureUris.add(pic.getPicUri());
294 Log.i("bell", pic.getPicUri());
295 }
296 Log.i("bell", ""+mPictureUris.size());
297 
298 Bundle bunde = new Bundle();
299 bunde.putStringArrayList("picUris", mPictureUris);
300 bunde.putInt("position", mPosition);
301 intent.putExtras(bunde);
302 startActivity(intent);
303 // PicView16Activity.this.finish();
304 
305 }
306 });
307 
308 return convertView;
309 }
310 
311 
312 
313 
314 
315 /**
316 * 删除图片操作
317 */
318 public boolean delPictureInfo(final Picture mDelPicture,final int mPosition) {
319 // TODO Auto-generated method stub
320 // final Picture mDelPicture = mData.get(position);
321 new AlertDialog.Builder(PicView16Activity.this)
322 .setTitle("通知")
323 .setMessage("是否删除?")
324 .setPositiveButton("确定", new DialogInterface.OnClickListener() {
325 
326 public void onClick(DialogInterface dialog, int which) {
327 // TODO Auto-generated method stub
328 Log.i("bell", mDelPicture.getId()+"");
329 if(mPicDatabase.deleteOldPicture(mDelPicture)){
330 Log.i("bell", "删除成功");
331 mData.remove(mPosition);
332 adapter.notifyDataSetChanged();
333 }
334 }
335 }).show();
336 return true;
337 }
338 
339 /**
340 * 重命名图片
341 */
342 public void reNamePicture(final Picture picture,final int mPosition) {
343 // TODO Auto-generated method stub
344 //引用重命名视图的布局文件
345 LayoutInflater inflater = (LayoutInflater)PicView16Activity.this.getSystemService(LAYOUT_INFLATER_SERVICE);
346 reNameView = inflater.inflate(R.layout.rename, null);
347 
348 final EditText oldName = (EditText)reNameView.findViewById(R.id.rename);
349 oldName.setText(picture.getPicName());
350 
351 new AlertDialog.Builder(PicView16Activity.this)
352 .setTitle("重命名")
353 .setView(reNameView)
354 .setPositiveButton("确认",new DialogInterface.OnClickListener() {
355 
356 public void onClick(DialogInterface dialog, int which) {
357 // TODO Auto-generated method stub
358 String rename =oldName.getText().toString().trim();//空指针异常
359              Picture reNamePic = new Picture();
360              reNamePic.setPicName(rename);
361              reNamePic.setId(picture.getId());
362              if(mPicDatabase.updateName(reNamePic)){
363              mData.get(mPosition).setPicName(rename);
364              adapter.notifyDataSetChanged();
365              }else{
366              Toast.makeText(PicView16Activity.this, "重命名不成功!", Toast.LENGTH_LONG).show();
367              }
368 }
369 }).setNegativeButton("取消", new DialogInterface.OnClickListener() {
370 
371 public void onClick(DialogInterface dialog, int which) {
372 // TODO Auto-generated method stub
373 
374 }
375 }).show();
376 }
377 }
378 
379 
380 /**
381 * 异步加载数据类
382 * @author Administrator
383 *
384 */
385 class LoadData extends AsyncTask<Object, String, List<Picture>>{
386 
387 @Override
388 protected List<Picture> doInBackground(Object... params) {
389 List<Picture> temp =mPicDatabase.query();
390 return temp;
391 }
392 
393 protected void onPostExecute(final List<Picture> pictureList) {
394 mData.clear();
395 mData = pictureList;
396 adapter.notifyDataSetChanged();
397 }
398 }
399 
400 @Override
401 protected void onDestroy() {
402 // TODO Auto-generated method stub
403 super.onDestroy();
404 if(mData != null){
405 mData.clear();
406 }
407 if(mPictureUris != null){
408 mPictureUris.clear();
409 }
410 if(listview != null){
411 listview.setAdapter(null);
412 }
413 mPicDatabase.closeDB();
414 }
415 
416 
417 }



7、图片预览,使用gallery,实现了动态上下翻转效果



1 package com.bellsong.pv;
  2 
  3 
  4 import java.util.List;
  5 
  6 
  7 import android.app.Activity;
  8 import android.content.Context;
  9 import android.graphics.Bitmap;
 10 import android.graphics.BitmapFactory;
 11 import android.os.Bundle;
 12 import android.util.Log;
 13 import android.view.View;
 14 import android.view.ViewGroup;
 15 import android.view.animation.AccelerateInterpolator;
 16 import android.view.animation.Animation;
 17 import android.view.animation.DecelerateInterpolator;
 18 import android.widget.AdapterView;
 19 import android.widget.AdapterView.OnItemClickListener;
 20 import android.widget.BaseAdapter;
 21 import android.widget.Gallery;
 22 import android.widget.ImageView;
 23 
 24 
 25 public class PictureView extends Activity{
 26 
 27 private Context mContext;
 28 private List<String> mPictureUris;
 29 private Bitmap mBitmap;
 30 private Gallery mGallery;
 31 private ImageView mImageView;
 32 private int mPosition;
 33 
 34 @Override
 35 protected void onCreate(Bundle savedInstanceState) {
 36 // TODO Auto-generated method stub
 37 super.onCreate(savedInstanceState);
 38 setContentView(R.layout.gallery);
 39 
 40 Bundle bunde = getIntent().getExtras();
 41 mPictureUris = bunde.getStringArrayList("picUris");
 42 mPosition    = bunde.getInt("position");
 43 for(String uri : mPictureUris){
 44 Log.i("note",uri+"  selectedId:"+mPosition + "  size: "+mPictureUris.size());
 45 }
 46 
 47 mGallery = (Gallery)findViewById(R.id.gallery);
 48 mGallery.setAdapter(new ImageAdapter(this));
 49 
 50 mGallery.setPersistentDrawingCache(ViewGroup.PERSISTENT_ANIMATION_CACHE);
 51 
 52 mGallery.setOnItemClickListener(new OnItemClickListener() {
 53 
 54 
 55 public void onItemClick(AdapterView<?> parent, View view,
 56 int position, long id) {
 57 // TODO Auto-generated method stub
 58 // Animation myanim =AnimationUtils.loadAnimation(mContext, R.anim.myanim);
 59 //             view.startAnimation(myanim);
 60 
 61 applyRotation(position, 0, 360);
 62 
 63 }
 64 });
 65 
 66 }
 67 
 68 /**
 69      * Setup a new 3D rotation on the container view.
 70      *
 71      * @param position the item that was clicked to show a picture, or -1 to show the list
 72      * @param start the start angle at which the rotation must begin
 73      * @param end the end angle of the rotation
 74      */
 75     private void applyRotation(int position, float start, float end) {
 76         // Find the center of the container
 77         final float centerX = mImageView.getWidth() / 2.0f;
 78         final float centerY = mImageView.getHeight() / 2.0f;
 79 
 80 
 81         // Create a new 3D rotation with the supplied parameter
 82         // The animation listener is used to trigger the next animation
 83         final Rotate3dAnimation rotation =
 84                 new Rotate3dAnimation(start, end, centerX, centerY, 0, false);
 85         rotation.setDuration(2000);//动画时间
 86         rotation.setFillAfter(true);//是否保存动画后的改变
 87         rotation.setInterpolator(new AccelerateInterpolator());//加速器
 88 //        rotation.setAnimationListener(new DisplayNextView(position));
 89 
 90 
 91         mImageView.startAnimation(rotation);
 92     }
 93 
 94 
 95     /**
 96      * This class listens for the end of the first half of the animation.
 97      * It then posts a new action that effectively swaps the views when the container
 98      * is rotated 90 degrees and thus invisible.
 99      */
100     private final class DisplayNextView implements Animation.AnimationListener {
101         private final int mPosition;
102 
103 
104         private DisplayNextView(int position) {
105             mPosition = position;
106         }
107 
108 
109         public void onAnimationStart(Animation animation) {
110         }
111 
112 
113         public void onAnimationEnd(Animation animation) {
114 //         mImageView.post(new SwapViews(mPosition));
115         }
116 
117 
118         public void onAnimationRepeat(Animation animation) {
119         }
120     }
121     
122     
123     /**
124      * This class is responsible for swapping the views and start the second
125      * half of the animation.
126      */
127     private final class SwapViews implements Runnable {
128         private final int mPosition;
129         public SwapViews(int position) {
130             mPosition = position;
131         }
132 
133 
134         public void run() {
135             final float centerX = mImageView.getWidth() / 1.0f;
136             final float centerY = mImageView.getHeight() / 2.0f;
137 //         final float centerX = mImageView.getWidth();
138 //         final float centerY = mImageView.getHeight();
139             Rotate3dAnimation rotation;
140 //            
141 //            if (mPosition > -1) {
142 //                mImageView.setVisibility(View.VISIBLE);
143 //                mImageView.requestFocus();
144 
145 
146                 rotation = new Rotate3dAnimation(0, 360, centerX, centerY,  0, false);
147 //            } else {
148 //                mImageView.setVisibility(View.GONE);
149 
150 
151 //                rotation = new Rotate3dAnimation(90, 0, centerX, centerY, 310.0f, false);
152 //            }
153 
154 
155             rotation.setDuration(2000);
156             rotation.setFillAfter(true);
157             rotation.setInterpolator(new DecelerateInterpolator());
158             mImageView.startAnimation(rotation);
159         }
160     }
161 
162 
163 public class ImageAdapter extends BaseAdapter{
164 private Context mContext;
165 
166 public ImageAdapter(Context context){
167 mContext = context;
168 
169 }
170 
171 
172 public int getCount() {
173 // TODO Auto-generated method stub
174 return mPictureUris.size();
175 }
176 
177 
178 public Object getItem(int position) {
179 // TODO Auto-generated method stub
180 return null;
181 }
182 
183 
184 public long getItemId(int position) {
185 // TODO Auto-generated method stub
186 return 0;
187 }
188 
189 
190 public View getView(int position, View convertView, ViewGroup parent) {
191 // TODO Auto-generated method stub
192 mImageView  = new ImageView(mContext);
193 // mImageView.setImageBitmap(BitmapFactory.decodeFile(mPictureUris.get(mPosition)));
194 mBitmap = BitmapFactory.decodeFile(mPictureUris.get(position));
195 
196 
197 mImageView.setImageBitmap(mBitmap);
198 mImageView.setScaleType(ImageView.ScaleType.CENTER);
199 mImageView.setLayoutParams(new Gallery.LayoutParams(400,500));
200 return mImageView;
201 }
202 
203 }
204 
205 
206 @Override
207 protected void onDestroy() {
208 // TODO Auto-generated method stub
209 super.onDestroy();
210 if(mPictureUris != null){
211 mPictureUris.clear();
212 }
213 }
214 
215 
216 }
217 
218 动画类,直接从ApiDemo中 copy Rotate3dAnimation.java
219 
220 /*
221  * Copyright (C) 2007 The Android Open Source Project
222  *
223  * Licensed under the Apache License, Version 2.0 (the "License");
224  * you may not use this file except in compliance with the License.
225  * You may obtain a copy of the License at
226  *
227  *      http://www.apache.org/licenses/LICENSE-2.0
228  *
229  * Unless required by applicable law or agreed to in writing, software
230  * distributed under the License is distributed on an "AS IS" BASIS,
231  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
232  * See the License for the specific language governing permissions and
233  * limitations under the License.
234  */
235 
236 
237 package com.bellsong.pv;
238 
239 
240 import android.view.animation.Animation;
241 import android.view.animation.Transformation;
242 import android.graphics.Camera;
243 import android.graphics.Matrix;
244 
245 
246 /**
247  * An animation that rotates the view on the Y axis between two specified angles.
248  * This animation also adds a translation on the Z axis (depth) to improve the effect.
249  */
250 public class Rotate3dAnimation extends Animation {
251     private final float mFromDegrees;
252     private final float mToDegrees;
253     private final float mCenterX;
254     private final float mCenterY;
255     private final float mDepthZ;
256     private final boolean mReverse;
257     private Camera mCamera;
258 
259 
260     /**
261      * Creates a new 3D rotation on the X axis. The rotation is defined by its
262      * start angle and its end angle. Both angles are in degrees. The rotation
263      * is performed around a center point on the 2D space, definied by a pair
264      * of X and Y coordinates, called centerX and centerY. When the animation
265      * starts, a translation on the Z axis (depth) is performed. The length
266      * of the translation can be specified, as well as whether the translation
267      * should be reversed in time.
268      *
269      * @param fromDegrees the start angle of the 3D rotation
270      * @param toDegrees the end angle of the 3D rotation
271      * @param centerX the X center of the 3D rotation
272      * @param centerY the Y center of the 3D rotation
273      * @param reverse true if the translation should be reversed, false otherwise
274      */
275     public Rotate3dAnimation(float fromDegrees, float toDegrees,
276             float centerX, float centerY, float depthZ, boolean reverse) {
277         mFromDegrees = fromDegrees;
278         mToDegrees = toDegrees;
279         mCenterX = centerX;
280         mCenterY = centerY;
281         mDepthZ = depthZ;
282         mReverse = reverse;
283     }
284 
285 
286     @Override
287     public void initialize(int width, int height, int parentWidth, int parentHeight) {
288         super.initialize(width, height, parentWidth, parentHeight);
289         mCamera = new Camera();
290     }
291 
292 
293     @Override
294     protected void applyTransformation(float interpolatedTime, Transformation t) {
295         final float fromDegrees = mFromDegrees;
296         float degrees = fromDegrees + ((mToDegrees - fromDegrees) * interpolatedTime);
297 
298 
299         final float centerX = mCenterX;
300         final float centerY = mCenterY;
301         final Camera camera = mCamera;
302 
303 
304         final Matrix matrix = t.getMatrix();
305 
306 
307         camera.save();
308         if (mReverse) {
309             camera.translate(0.0f, 0.0f, mDepthZ * interpolatedTime);
310         } else {
311             camera.translate(0.0f, 0.0f, mDepthZ * (1.0f - interpolatedTime));
312         }
313         camera.rotateX(degrees);//以X轴
314         camera.getMatrix(matrix);
315         camera.restore();
316 
317 
318         matrix.preTranslate(-centerX, -centerY);
319         matrix.postTranslate(centerX, centerY);
320     }
321 }
322 
323 
324 gallery.xml
325 
326 <Gallery xmlns:android="http://schemas.android.com/apk/res/android" 
327     android:id="@+id/gallery"
328 android:layout_width="fill_parent"
329 android:layout_height="fill_parent"
330 android:gravity="center_vertical"
331 android:spacing="150dp"
332 android:unselectedAlpha="1.2"
333 />
334 rename.xml
335 
336 <?xml version="1.0" encoding="utf-8"?>
337 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
338     android:layout_width="match_parent"
339     android:layout_height="match_parent"
340     android:orientation="vertical" 
341     >
342 
343 
344     <TextView 
345         android:layout_width="match_parent"
346         android:layout_height="match_parent"
347         android:text="图片名称:"
348         />
349     <EditText 
350         android:id="@+id/rename"
351         android:layout_width="fill_parent"
352         android:layout_height="wrap_content"
353             />
354 
355 
356 </LinearLayout>
357 
358 main.xml
359 
360 
361 <?xml version="1.0" encoding="utf-8"?>
362 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
363    android:layout_width="fill_parent"
364    android:layout_height="fill_parent"
365    android:orientation="vertical" >
366  
367    <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
368     android:layout_width="fill_parent"
369     android:layout_height="wrap_content"
370     android:padding="10dip"
371     >
372      <TextView
373             android:id="@+id/textView1"
374             android:layout_width="wrap_content"
375             android:layout_height="wrap_content"
376             android:layout_alignParentLeft="true"
377             android:text="名称:" />
378 
379 
380         <EditText
381             android:id="@+id/editName"
382             android:layout_width="wrap_content"
383             android:layout_height="wrap_content"
384       android:layout_centerInParent="true"
385             android:layout_toRightOf="@id/textView1"
386             android:hint="请输入图片名称"
387 />
388         
389         <Button
390             android:id="@+id/btnPickPic"
391             android:layout_width="wrap_content"
392             android:layout_height="wrap_content"
393             android:layout_alignParentRight="true"
394             android:text="选择图片" />
395     
396       </RelativeLayout>
397       
398      <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
399     android:layout_width="fill_parent"
400     android:layout_height="wrap_content"
401     android:padding="10dip">
402 
403 
404     <TextView
405         android:id="@+id/label"
406         android:layout_width="wrap_content"
407         android:layout_height="wrap_content"
408         android:layout_alignParentLeft="true"
409         android:text="预览图"/>
410 
411 
412     <ImageView
413         android:id="@+id/preView"
414         android:layout_width="100px"
415         android:layout_height="100px"
416         android:layout_centerInParent="true"
417         android:maxWidth="100px"
418         android:maxHeight="100px"
419         android:layout_toRightOf="@id/label"/>
420 
421 
422         <Button
423             android:id="@+id/btnAdd"
424             android:layout_width="wrap_content"
425             android:layout_height="wrap_content"
426             android:layout_alignParentRight="true"
427             android:text="加到列表" />
428   
429 </RelativeLayout>
430       
431    <ListView
432        android:id="@+id/listview"
433        android:layout_width="match_parent"
434        android:layout_height="match_parent"
435        ></ListView>
436  
437 </LinearLayout>
438 
439 listview.xml
440 
441 <?xml version="1.0" encoding="utf-8"?>
442 <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
443     android:layout_width="match_parent"
444     android:layout_height="match_parent" >
445 
446 
447     <ImageView
448         android:id="@+id/img"
449         android:layout_width="80dip"
450         android:layout_height="80dip"
451         android:layout_alignParentLeft="true"
452         android:layout_alignParentTop="true"
453         android:layout_marginLeft="10dp"
454         android:layout_marginTop="10dp"
455         />
456 
457 
458     <TextView
459         android:id="@+id/title"
460         android:layout_width="wrap_content"
461         android:layout_height="wrap_content"
462         android:layout_alignBottom="@+id/img"
463         android:layout_toRightOf="@+id/img"
464         android:layout_centerVertical="true"
465         android:layout_marginLeft="20dp"
466          />
467 
468 
469     <Button
470         android:id="@+id/btnDel"
471         android:layout_width="wrap_content"
472         android:layout_height="wrap_content"
473         android:layout_alignParentRight="true"
474         android:layout_centerVertical="true"
475         android:layout_alignTop="@+id/img"
476         android:layout_marginTop="25dp"
477         android:text="删除" />
478 
479 
480 </RelativeLayout>



8、未解决问题

如何实现点击图片跳到gallery时不是从第一个开始显示,而是默认显示选中那个。

增加手势功能,实现随手势上下滑动不停翻转,并且有加速度,当不滑动时,图片有个慢慢停下来的动作