2025-04-22

不多說 先上圖:

 

點擊本地列表中,實現新浪微博中點擊縮略圖 ,加載成原圖! 剛開始做的時候,隻是在佈局上添加瞭一個隱藏的xml ,設置熟悉為居中顯示,但後面顯示的圖片

效果有點僵硬,感覺就是疊加在一起,沒有視覺的立體感,所以後面百度瞭下, 說可以直接用dialog 可以實現,但是沒有找到demo 應用,所以隻好自己動手寫瞭一個

AlertImageDialog.java 類,專門實現點擊彈出,通過服務器下載原圖,下載的過程中,通過progressbar 經行默認加載,當數據回來時候,隱藏progressbar  ,顯示原圖

點擊該Dialog 直接消除,當再次點擊,則不會去服務器,加載在本地數據庫中,這樣實現瞭基本的功能。

不多說瞭 ,希望對你有幫助:

[html] /* 
 * @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 顯示原圖 
/*
 * @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 顯示原圖[html]                 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); 
    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);
[html] 考慮到可能沒有環境,直接註釋瞭從網絡上獲取,直接從數據庫中加載圖片,如果測試的時候,記得在sd卡中放一張圖片,否則沒有效果顯示 
考慮到可能沒有環境,直接註釋瞭從網絡上獲取,直接從數據庫中加載圖片,如果測試的時候,記得在sd卡中放一張圖片,否則沒有效果顯示[html]                         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的標題欄,太難看瞭  
        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的標題欄,太難看瞭 [html]             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; 
    } 

<pre class="html" name="code">  
      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;
 }
}
<pre class="html" name="code"> [html] 佈局文件 : 
佈局文件 :[html] <?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> 

摘自 android小益的專欄

發佈留言

發佈留言必須填寫的電子郵件地址不會公開。 必填欄位標示為 *