不多说 先上图:
点击本地列表中,实现新浪微博中点击缩略图 ,加载成原图! 刚开始做的时候,只是在布局上添加了一个隐藏的xml ,设置熟悉为居中显示,但后面显示的图片
效果有点僵硬,感觉就是叠加在一起,没有视觉的立体感,所以后面百度了下, 说可以直接用dialog 可以实现,但是没有找到demo 应用,所以只好自己动手写了一个
AlertImageDialog.java 类,专门实现点击弹出,通过服务器下载原图,下载的过程中,通过progressbar 经行默认加载,当数据回来时候,隐藏progressbar ,显示原图
点击该Dialog 直接消除,当再次点击,则不会去服务器,加载在本地数据库中,这样实现了基本的功能。
不多说了 ,希望对你有帮助:
/*
* @project dongyi_program
* @package com.jh.dongyi.activity
* @file AlertImageDialog.java
* @version 1.0
* @author yourname
* @time 2011-12-19 上午08:00:41
* CopyRight: */
package com.jh.dongyi.activity;
import com.jh.dongyi.activity.MyWorkProjectActivity.btonclickListener;
import com.jh.dongyi.util.Configuration;
import com.jh.dongyi.util.DBFunction;
import com.jh.dongyi.util.ImageFactory;
import com.jh.dongyi.util.POAException;
import android.app.Dialog;
import android.content.Context;
import android.graphics.Bitmap;
import android.os.Handler;
import android.os.Message;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.ImageView;
import android.widget.ProgressBar;
/**
*
* <code>AlertImageDialog</code>
* @description: TODO(弹出扩大图片的dialog)
* @version 1.0
* @author liaoyp
* @since 2011-12-19
*/
public class AlertImageDialog {
private Context context;
private String imagePath;
private Dialog dialog;
private DBFunction function;
private Bitmap bitmap;
/**
*
* @param context
* @param originalImage 原图的地址
*/
public AlertImageDialog(Context context, String originalImage) {
/**
* constructor
* @param number
*/
this.context = context;
this.imagePath = originalImage;
}
Handler handler = new Handler(){
@Override
public void handleMessage(Message msg) {
// TODO Auto-generated method stub
// 当数据回来时候影藏bar 显示原图
if(msg.what == 1 && bitmap != null){
image.setImageBitmap(bitmap);
bar.setVisibility(View.GONE);
}
}
};
private ImageView image;
private ProgressBar bar;
public void show() {
create();
dialog.show();
}
/**
*
* <code>create</code>
* @description: TODO(创建一个bitmap 当本地有则去本地存储,否则去服务器上下载
* @since 2011-12-19 yourname
*/
private void create(){
if(imagePath !=null){
String path =haveDownld(imagePath);
if(path == null){
new Thread(){
public void run(){
String localPath;
try {
// localPath = ImageFactory.getURLBitmap(imagePath,2);
// function = new DBFunction(context);
// function.addPhotoinfo(Configuration.return_customerId,imagePath,localPath);
考虑到可能没有环境,直接注释了从网络上获取,直接从数据库中加载图片,如果测试的时候,记得在sd卡中放一张图片,否则没有效果显示
this.sleep(3000);
localPath=Configuration.DATADIR+Configuration.TempFile+"red.jpg";
System.out.println("localpath> "+localPath);
if(localPath != null){
bitmap = ImageFactory.getFileBitmap(localPath,300, 300,context);
System.out.println("==>"+bitmap);
handler.sendEmptyMessage(1);
}
} catch (POAException e) {
e.printStackTrace();
} catch (Exception e) {
e.printStackTrace();
}
}
}.start();
}else{
try {
bitmap = ImageFactory.getFileBitmap(path,300, 300,context);
} catch (POAException e) {
e.printStackTrace();
}
}
}
dialog=new Dialog(context);
// 设置影藏dialog的标题栏,太难看了
dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
dialog.getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
dialog.setContentView(R.layout.image_dialog);
initImageView();
}
/**
*
* <code>initImageView</code>
* @description: TODO(初始化弹出对话框显示的图片)
* @since 2011-12-19 yourname
*/
private void initImageView(){
image = (ImageView)dialog.findViewById(R.id.image);
bar = (ProgressBar) dialog.findViewById(R.id.pb);
if(bitmap != null){
image.setImageBitmap(bitmap);
}else{
bar.setVisibility(View.VISIBLE);
}
image.setOnClickListener(new ImageView.OnClickListener() {
public void onClick(View arg0) {
if(dialog!=null){
dialog.dismiss();
}
}
});
}
/**
*
* <code>haveDownld</code>
* @description: TODO(获取数据库中是否下载了该图片)
* @param httpPath
* @return
* @since 2011-12-19 liaoyp
*/
public String haveDownld(String httpPath){
String localPath = null;
// 查询本地数据库
function = new DBFunction(context);
localPath = function.queryImagePath(httpPath, Configuration.return_customerId);
return localPath;
}
}
布局文件 :
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="wrap_content"
android:layout_height="wrap_content" android:background="@color/black">
<ImageView android:id="@+id/image" android:layout_width="wrap_content"
android:layout_height="wrap_content" android:maxWidth="480dip"
android:maxHeight="800dip" android:adjustViewBounds="true"
android:padding="1dip" />
<ProgressBar
android:layout_centerInParent="true"
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:id="@+id/pb"
android:layout_gravity="center"
></ProgressBar>
</RelativeLayout>