中文字幕在线观看,亚洲а∨天堂久久精品9966,亚洲成a人片在线观看你懂的,亚洲av成人片无码网站,亚洲国产精品无码久久久五月天

Android圖片異步加載

2018-07-20    來源:open-open

容器云強勢上線!快速搭建集群,上萬Linux鏡像隨意使用
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.io.OutputStream;
import java.lang.ref.SoftReference;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.HashMap;
import java.util.Map;
 
import com.mosjoy.ad.MosJoyAdApplication;
import com.mosjoy.ad.R;
import com.mosjoy.ad.utils.FileCache;
import com.mosjoy.ad.utils.ImageCache;
import com.mosjoy.ad.utils.Utils;
 
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.AsyncTask;
import android.os.Environment;
import android.os.StatFs;
import android.util.AttributeSet;
import android.util.Log;
import android.view.View;
import android.widget.ImageView;
 
/**
 * 圖片異步加載工具類
 * @author zouyb
 *
 */
public class RemoteImageView extends ImageView {
 
    /**
     * 下載失敗最大重復(fù)次數(shù)
     */
    private static int MAX_FAILURES = 3;
 
    /**
     * 下載圖片網(wǎng)址
     */
    private String mUrl;
 
    /**
     * 當(dāng)前下載成功圖片網(wǎng)址
     */
    private String mCurrentlyGrabbedUrl;
 
    /**
     * 下載失敗次數(shù)
     */
    private int mFailure;
 
    /**
     * 默認圖片資源id
     */
    private Integer mDefaultImage;
 
    /**
     * 上下文
     */
    private Context mContext;
     
    /**
     *  文件操作
     */
    private FileCache fileCache;
 
    private final static int MB = 1048576;
     
    //圖片寬度
    private int pwidth=-1;
    //圖片高度
    private int pheight=-1;
    //是否顯示默認圖片
    private boolean isShowDefault=false;
    public RemoteImageView(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
        mContext = context;
        init();
    }
 
    public RemoteImageView(Context context, AttributeSet attrs) {
        super(context, attrs);
        mContext = context;
        init();
    }
 
    public RemoteImageView(Context context) {
        super(context);
        mContext = context;
        init();
    }
 
    /**
     * Sharable code between constructors
     */
    private void init() {
        fileCache = new FileCache(mContext);
    }
 
    public void setImageUrl(String url) {
        System.out.println("width="+pwidth+",height="+pheight);
        System.out.println("url="+url);
        if(("").equals(url) || url == null||url.contains("null")){
            if(isShowDefault){
                setImageResource(R.drawable.default_image);
            }else{
                setVisibility(View.GONE);
            }
            return;
        }
        setVisibility(View.VISIBLE);
        url = url.replace("\\", "/");
        mUrl = url;
        MosJoyAdApplication instance = MosJoyAdApplication.getInstance();
        ImageCache imageCache = instance.getImageCache();
        if (imageCache.isCached(mUrl)) {
            // 圖片在緩存中
            this.setImageBitmap(imageCache.get(mUrl).get());
            Log.e(MosJoyAdApplication.TAG, "從內(nèi)存加載圖片 " + url);
        } else {
            File f = new File(mUrl);
            Bitmap b = decodeFile(f);
            if(b != null){
                //從內(nèi)存卡加載
                this.setImageBitmap(b);
                imageCache.put(mUrl,new SoftReference<Bitmap>(b));
                Log.e(MosJoyAdApplication.TAG, "從本地加載圖片 " + url);
            }else{
                //防止重復(fù)下載
                //if(YYQMusicApplication.getInstance().getDownLoadImageList().contains(mUrl)) return;
                //下載圖片
                Log.e(MosJoyAdApplication.TAG, "從網(wǎng)絡(luò)下載圖片 " + url);
                Map<String,Object> map = new HashMap<String,Object>();
                map.put("url", mUrl);
                map.put("file", f);
                try{
                    new DownloadTask().execute(map);
                }catch(Exception e){}
            }
        }
 
    }
 
    public void setDefaultImage(Integer resid) {
        mDefaultImage = resid;
    }
 
    private void loadDefaultImage() {
        if (mDefaultImage != null)
            setImageResource(mDefaultImage);
    }
     
    class DownloadTask extends AsyncTask<Map<String, Object>,Void, Bitmap>{
 
        private String mTaskUrl;
        private File f;
         
        @Override
        public void onPreExecute() {
            loadDefaultImage();
            super.onPreExecute();
        }
         
        @Override
        protected Bitmap doInBackground(Map<String, Object>... params) {
            mTaskUrl = params[0].get("url").toString();
            //YYQMusicApplication.getInstance().getDownLoadImageList().add(mTaskUrl);
            f = (File) params[0].get("file");
            try {
                Bitmap bitmap=null;
                URL imageUrl = new URL(mTaskUrl);
                HttpURLConnection conn = (HttpURLConnection)imageUrl.openConnection();
                conn.setConnectTimeout(30000);
                conn.setReadTimeout(30000);
                InputStream is=conn.getInputStream();
                OutputStream os = new FileOutputStream(f);
                Utils.CopyStream(is, os);
                os.close();
                bitmap = decodeFile(f);
                return bitmap;
            } catch(FileNotFoundException fileNotFoundException){
                fileNotFoundException.printStackTrace();
                return null;
            }
            catch (Exception ex){
               ex.printStackTrace();
            }
            return null;
        }
         
        @Override
        public void onPostExecute(Bitmap b) {
            super.onPostExecute(b);
            if(b == null){
                //下載失敗,繼續(xù)下載
                //RemoteImageView.this.setImageUrl(mTaskUrl);
            }else{
                //下載成功
                MosJoyAdApplication.getInstance().getImageCache().put(mTaskUrl, new SoftReference<Bitmap>(b));
                RemoteImageView.this.setImageBitmap(b);            
                mCurrentlyGrabbedUrl = mTaskUrl;               
            }
        }
         
    };
 
    /**
     * 解碼圖片
     * @param f
     * @return
     */
    private Bitmap decodeFile(File f) {
        if(!f.exists()){
            return null;
        }
        if(pwidth>0&&pheight>0){
            System.out.println("加載縮略圖");
            return decodeFile(f, pwidth, pheight);
        }
        try {
            BitmapFactory.Options options = new BitmapFactory.Options();
            options.inSampleSize = 1;
            return BitmapFactory.decodeFile(f.getAbsolutePath(), options);
        } catch (Exception e) {
        }
        return null;
    }
 
    private int freeSpaceOnSd() {
        StatFs stat = new StatFs(Environment.getExternalStorageDirectory()
                .getPath());
        double sdFreeMB = ((double) stat.getAvailableBlocks() * (double) stat
                .getBlockSize()) / MB;
 
        return (int) sdFreeMB;
    }
     
    private Bitmap decodeFile(File file,int width,int height){
        BitmapFactory.Options options=new BitmapFactory.Options();
        int scale=1;
        if(width>0||height>0){
            //設(shè)置次參數(shù)為true,解碼時返回的Bitmap為空,不分配內(nèi)存
            options.inJustDecodeBounds=true;
            BitmapFactory.decodeFile(file.getAbsolutePath(), options);
            //得到原始圖片的寬度和高度
            int outWidth=options.outWidth;
            int outHeight=options.outHeight;
            while(true){
                if((width>0&&outWidth/2<width)||(height>0&&outHeight/2<height)){
                    break;
                }
                outWidth/=2;
                outHeight/=2;
                scale*=2;
            }
        }
        //縮放比例為1/scale
        options.inSampleSize=scale;
        options.inJustDecodeBounds=false;
        //處理之后,加載進內(nèi)存的就是圖片的縮略圖
        return BitmapFactory.decodeFile(file.getAbsolutePath(), options);
    }
 
    public void setPwidth(int pwidth) {
        this.pwidth = pwidth;
    }
 
    public void setPheight(int pheight) {
        this.pheight = pheight;
    }
 
    public void setShowDefault(boolean isShowDefault) {
        this.isShowDefault = isShowDefault;
    }
 
}

標(biāo)簽: 網(wǎng)絡(luò)

版權(quán)申明:本站文章部分自網(wǎng)絡(luò),如有侵權(quán),請聯(lián)系:west999com@outlook.com
特別注意:本站所有轉(zhuǎn)載文章言論不代表本站觀點!
本站所提供的圖片等素材,版權(quán)歸原作者所有,如需使用,請與原作者聯(lián)系。

上一篇:Android 發(fā)送短信程序

下一篇:Android縮放圖片 Matrix