DiskLrucCache使用Demo,disklruccachedemo

來源:互聯網
上載者:User

DiskLrucCache使用Demo,disklruccachedemo

DiskLrucCache使用的Demo,這個demo是從網路擷取一張圖片,儲存到本機快取中(sdcard和記憶體),當下載成功後,再開啟不會重新向網路請求圖片,而是世界使用本地資源。

要使用DiskLrucCache需要先下載此類.   點這裡

主類:

/** * DiskLrucCache使用Demo *  * @author pangzf * @date 2014年8月12日 下午2:13:26 */public class DemoActivity extends Activity {    private DiskLruCache mDiskLrucache;    private ImageView mIvShow;    private String mBitMapUrl;    private String mKey;    private ProgressDialog mPd;    private Handler mHandler = new Handler() {        @Override        public void dispatchMessage(Message msg) {            super.dispatchMessage(msg);            // 10.下載圖片之後展示            boolean isSuccess = (boolean) msg.obj;            if (isSuccess) {                Snapshot snapshot;                try {                    snapshot = mDiskLrucache.get(mKey);                    InputStream is = snapshot.getInputStream(0);                    mIvShow.setImageBitmap(BitmapFactory.decodeStream(is));                } catch (IOException e) {                    e.printStackTrace();                }            }            if (mPd != null && mPd.isShowing()) {                mPd.dismiss();            }        }    };    @Override    protected void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        setContentView(R.layout.activity_demo);        mIvShow = (ImageView) findViewById(R.id.iv_show);        mPd = new ProgressDialog(DemoActivity.this);        try {            // 1.存放緩衝的目錄            File directory = DiskLrucacheUtils.getDiskCache(DemoActivity.this,                    "bitmap");            // 2.版本號碼            int appVersion = DiskLrucacheUtils.getAppVersion(DemoActivity.this);            int valueCount = 1;            // 3.緩衝的最大值,這裡設定10m            long maxSize = 10 * 1024 * 1024;            // 4.開啟disklrucache            mDiskLrucache = DiskLruCache.open(directory, appVersion,                    valueCount, maxSize);            mBitMapUrl = "https://raw.githubusercontent.com/pangzaifei/LineChartView/master/LineChartView/effice_picture/a.jpg";            if (mDiskLrucache != null) {                // 5.如果在緩衝中存在就用緩衝中的bitmap,如果不存在上網上下載                mKey = DiskLrucacheUtils.getKey(mBitMapUrl);                Snapshot snapshot = mDiskLrucache.get(mKey);                if (snapshot != null) {                    InputStream is = snapshot.getInputStream(0);                    Bitmap bitmap = BitmapFactory.decodeStream(is);                    if (bitmap != null) {                        mIvShow.setImageBitmap(bitmap);                    }                } else {                    mPd.show();                    new Thread() {                        public void run() {                            // 6.下載圖片                            mPd.setMessage("正在載入資料....");                            userDiskLrucache();                        };                    }.start();                }            }        } catch (Exception e) {            e.printStackTrace();        }    }    private void userDiskLrucache() {        try {            String bitMapUrl = "https://raw.githubusercontent.com/pangzaifei/LineChartView/master/LineChartView/effice_picture/a.jpg";            String key = DiskLrucacheUtils.getKey(bitMapUrl);            Editor edit = mDiskLrucache.edit(key);            // 7.從伺服器下載圖片            boolean isSuccess = DiskLrucacheUtils.downloadBitmap(bitMapUrl,                    edit.newOutputStream(0));            if (isSuccess) {                // 8.提交到緩衝                edit.commit();                // 9.下載成功去展示圖片                Message msg = mHandler.obtainMessage();                msg.obj = true;                mHandler.sendMessage(msg);            } else {                edit.abort();            }        } catch (Exception e) {            e.printStackTrace();        }    }    @Override    protected void onPause() {        try {            if (mDiskLrucache != null) {                mDiskLrucache.flush();            }        } catch (IOException e) {            // TODO Auto-generated catch block            e.printStackTrace();        }        super.onPause();    }    @Override    protected void onDestroy() {        try {            if (mDiskLrucache != null) {                mDiskLrucache.close();            }        } catch (IOException e) {            e.printStackTrace();        }        super.onDestroy();    }}
使用DiskLrucache自己寫的工具類.

package com.pangzaifei.disklrucachedemo.libcore;import java.io.BufferedInputStream;import java.io.BufferedOutputStream;import java.io.File;import java.io.IOException;import java.io.OutputStream;import java.net.HttpURLConnection;import java.net.URL;import java.security.MessageDigest;import java.security.NoSuchAlgorithmException;import android.content.Context;import android.content.pm.PackageInfo;import android.content.pm.PackageManager.NameNotFoundException;import android.os.Environment;/** * diskLrucache的工具類 *  * @author pangzf * @date 2014年8月12日 上午10:58:50 */public class DiskLrucacheUtils {    /**     * 獲得緩衝目錄,當sdcard存在的時候使用,sdcard圖片緩衝,如果sdcard不存在使用data/data下的圖片緩衝     *      * @param context     * @param uniqueName     * @return     */    public static File getDiskCache(Context context, String uniqueName) {        String path;        if (Environment.getExternalStorageDirectory().equals(                Environment.MEDIA_MOUNTED)) {            // 存在sdcard            path = Environment.getExternalStorageDirectory().getPath();        } else {            // 不存在sdcard使用手機記憶體            path = context.getCacheDir().getPath();        }        return new File(path + File.separator + uniqueName);    }    /**     * 獲得app版本號碼     *      * @param context     * @return     * @throws NameNotFoundException     */    public static int getAppVersion(Context context)            throws NameNotFoundException {        PackageInfo packageInfo = context.getPackageManager().getPackageInfo(                context.getPackageName(), 0);        return packageInfo.versionCode;    }    /**     * 將圖片url進行md5加密產生一個字串,因為有的url地址裡面存在特殊字元     *      * @param urlStr     * @return     * @throws NoSuchAlgorithmException     */    public static String getKey(String urlStr) throws NoSuchAlgorithmException {        MessageDigest messageDigest = MessageDigest.getInstance("md5");        messageDigest.update(urlStr.getBytes());        return bytesToString(messageDigest.digest());    }    /**     * byte轉string     *      * @param bytes     */    private static String bytesToString(byte[] bytes) {        StringBuilder sb = new StringBuilder();        for (int i = 0; i < bytes.length; i++) {            String hex = Integer.toHexString(0xFF & bytes[i]);            if (hex.length() == 1) {                sb.append(0);            }            sb.append(hex);        }        return sb.toString();    }    /**     * 下載圖片到cache     *      * @param imageString     * @param ops     * @return     */    public static boolean downloadBitmap(String imageString, OutputStream ops) {        URL url;        HttpURLConnection conn = null;        BufferedInputStream bis = null;        BufferedOutputStream bos = null;        try {            url = new URL(imageString);            conn = (HttpURLConnection) url.openConnection();            bis = new BufferedInputStream(conn.getInputStream());            bos = new BufferedOutputStream(ops);            int b;            while ((b = bis.read()) != -1) {                bos.write(b);            }            return true;        } catch (IOException e) {            e.printStackTrace();        } finally {            try {                if (conn != null) {                    conn.disconnect();                }                if (bos != null) {                    bos.close();                }                if (bis != null) {                    bis.close();                }            } catch (IOException e) {                e.printStackTrace();            }        }        return false;    }}

源碼


感謝guolin的部落格對我的協助。


demo怎使用

先解壓縮,然後:
1、使用CS本身播放:
先把demo複製到目錄裡的cstrike下,再把這個demo改成輸入比較簡單的檔案名稱(比如1或者2等等),然後用CS裡的控制台打這個命令:playdemo 檔案名稱.dem,或者打viewdemo 檔案名稱.dem ,後者可以在觀看的時候按F2選擇播放速度。

2、使用GeekPlay-5.1 DEMO播放器:

  在你下載了一個geekplay和一個demo後,你就可以來欣賞了。

  當然第一次使用geekplay的時候要指定你cs目錄下cstrike.exe

  先按那個Browse,它會自動到cs安裝目錄裡,你只要再點一下cstrike.exe就可以了,(商業版一般為C:SIERRACounter-Strikecstrike.exe)。正版軟體店賣出的帶有合法cdkey的都是商業版。(至於你不會裝偽正版和盜版那就不關我事了)

  當你指定好cstrike.exe後,可以按照圖例,就是在輸入(Command line Options)這個參數-console

  以上步驟完成以後按一下SAVE儲存一下,再按一下browse,匯入你下載好的一個demo,也就是說把它指向以下載好的***.dem檔案。

  你也可以用滑鼠右鍵裡的開啟檔案裡選擇Geekplay,再選扣一下始終使用該程式開啟就可以了。

  convert resolution是用來調整原本demo的解析度的,如果你的顯卡只支援到800×600那就有用了(幾乎不可能),要麼你喜歡把demo調整成自己常用的解析度。

  demospeed用來調節demo播放速度
 
軟體使用的Demo該使用什軟體製作?

很高興回答你的問題,希望對你有協助。螢幕錄相這個軟體可以錄製。用下還不錯。我就是用這個螢幕錄相的!
 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.