Skip to content

Instantly share code, notes, and snippets.

View duanhong169's full-sized avatar
💭
I may be slow to respond.

Hong Duan duanhong169

💭
I may be slow to respond.
View GitHub Profile
@duanhong169
duanhong169 / LICENSE.txt
Created February 20, 2012 14:59 — forked from aemkei/LICENSE.txt
Binary Tetris - 140byt.es
DO WHAT THE FUCK YOU WANT TO PUBLIC LICENSE
Version 2, December 2004
Copyright (C) 2011 YOUR_NAME_HERE <YOUR_URL_HERE>
Everyone is permitted to copy and distribute verbatim or modified
copies of this license document, and changing it is allowed as long
as the name is changed.
DO WHAT THE FUCK YOU WANT TO PUBLIC LICENSE
//设置状态
private void setPresence(int code){
if(connection == null) return;
Presence presence;
switch(code){
case 0:
presence = new Presence(Presence.Type.available);
connection.sendPacket(presence);
Log.v(TAG, "设置在线");
break;
public static Bitmap decodeSampledBitmapFromDescriptor(
FileDescriptor fileDescriptor, int reqWidth,
int reqHeight, ImageCache cache) {
// 设置inJustDecodeBounds=true来获取图片的尺寸
final BitmapFactory.Options options = new BitmapFactory.Options();
options.inJustDecodeBounds = true;
BitmapFactory.decodeFileDescriptor(fileDescriptor, null, options);
// 计算采样率
public static int calculateInSampleSize(BitmapFactory.Options options,
int reqWidth, int reqHeight) {
// 从BitmapFactory.Options中获取图片的原始高度和宽度
final int height = options.outHeight;
final int width = options.outWidth;
int inSampleSize = 1;
if (height > reqHeight || width > reqWidth) {
// 计算原始尺寸与显示尺寸的比例
/**
* 该Drawable能够为与之绑定的ImageView保持一个BitmapWorkerTask(AsyncTask)的引用,使得
* BitmapWorkerTask能够在需要的时候被取消,并且保证只有最后与ImageView绑定的BitmapWorkerTask
* 才能够更新ImageView
*/
private static class AsyncDrawable extends BitmapDrawable {
private final WeakReference<BitmapWorkerTask> bitmapWorkerTaskReference;
/**
* bitmap参数是占位图片(placeholder),代表图片正在加载中
private static BitmapWorkerTask getBitmapWorkerTask(ImageView imageView) {
if (imageView != null) {
final Drawable drawable = imageView.getDrawable();
if (drawable instanceof AsyncDrawable) {
final AsyncDrawable asyncDrawable = (AsyncDrawable) drawable;
return asyncDrawable.getBitmapWorkerTask();
}
}
return null;
}
/**
* 比较之前传入的ImageView在当前时刻关联的`AsyncTask`是否等于自身
* 来确定是否需要将结果返回给ImageView
*/
private ImageView getAttachedImageView() {
final ImageView imageView = imageViewReference.get();
final BitmapWorkerTask bitmapWorkerTask = getBitmapWorkerTask(imageView);
if (this == bitmapWorkerTask) {
return imageView;
/**
* 如果当前存在一致的任务,返回false,代表不需要新建任务,否则返回true
*/
public static boolean cancelPotentialWork(Object data, ImageView imageView) {
final BitmapWorkerTask bitmapWorkerTask = getBitmapWorkerTask(imageView);
if (bitmapWorkerTask != null) {
final Object bitmapData = bitmapWorkerTask.data;
if (bitmapData == null || !bitmapData.equals(data)) {
bitmapWorkerTask.cancel(true);
private LruCache<String, BitmapDrawable> mMemoryCache;
final int maxMemory = (int) (Runtime.getRuntime().maxMemory() / 1024);
// 使用最大可使用内存的1/8作为缓存
final int cacheSize = maxMemory / 8;
mMemoryCache = new LruCache<String, BitmapDrawable>(cacheSize) {
/**
public void addBitmapToMemoryCache(String key, Bitmap bitmap) {
if (getBitmapFromMemCache(key) == null) {
mMemoryCache.put(key, bitmap);
}
}
public Bitmap getBitmapFromMemCache(String key) {
return mMemoryCache.get(key);
}