編輯:關於Android編程
/** * @author [email protected] * @time 20140606 */ package com.intbird.utils; import java.lang.ref.WeakReference; import android.content.Context; import android.graphics.Bitmap; import android.graphics.Bitmap.Config; import android.graphics.BitmapFactory; import android.graphics.Canvas; import android.graphics.Paint; import android.graphics.PorterDuff.Mode; import android.graphics.PorterDuffXfermode; import android.graphics.Rect; import android.graphics.drawable.BitmapDrawable; import android.graphics.drawable.Drawable; import android.media.ThumbnailUtils; import android.os.AsyncTask; import android.widget.ImageView; public class BitmapHelper { private CacheManager cacheManager; public static final int IMG_TYPE_RES=0; public static final int IMG_TYPE_WEB=1; public static final int IMG_TYPE_FILE=3; private Bitmap bmpHolder=null; private Bitmap bmpNoImg=null; public BitmapHelper(Context context,int loadingId,int loadNoId){ cacheManager=CacheManager.getInstance(); bmpHolder=BitmapFactory.decodeResource(context.getResources(),loadingId); bmpNoImg=BitmapFactory.decodeResource(context.getResources(),loadNoId); } /** * 加載圖片 * @param type 網絡文件,本地文件,還是資源文件 * @param fileUrl url * @param imageView 控件 * @param width 要獲取的指定高度 * @param height 要獲取的指定寬度 */ public void commonLoadBitmap(int type,String fileUrl,ImageView imageView,int width,int height){ //內存和文件中沒有圖片,重新獲取; Bitmap bmp=cacheManager.getBitmapFromCache(fileUrl); if(bmp!=null){ imageView.setImageBitmap(bmp); } else{ switch(type){ case IMG_TYPE_WEB: loadMultiBitmapFromWeb(fileUrl, imageView,width,height); break; case IMG_TYPE_FILE: imageView.setImageBitmap(getBitmapFormFile( fileUrl, width, height, true)); break; case IMG_TYPE_RES: imageView.setImageResource(Integer.parseInt(fileUrl)); break; } } } /** * 設置ImageView為獲取指定文件地址的指定高度指定寬度的圖片; * @param fileUrl 文件地址 * @param reqWidth 目標寬度 * @param reqHeight 目標高度 * @return bitmap */ public Bitmap loadSingleBitmapFromFile(String fileUrl,ImageView iv,int reqWidth,int reqHeight){ BitmapFactory.Options options=new BitmapFactory.Options(); options.inJustDecodeBounds=true; options.inSampleSize=calculateInSampleSize(options, reqWidth, reqHeight); BitmapFactory.decodeFile(fileUrl,options); options.inJustDecodeBounds=false; Bitmap bmp=BitmapFactory.decodeFile(fileUrl,options); if(iv!=null) iv.setImageBitmap(bmp); return bmp; } /** * 設置ImageView為需要加載的一張網絡圖片; * @param webUrl * @param imageView * @param width * @param heigth */ public void loadSingleBitmapFromWeb(String webUrl,ImageView imageView,int width,int heigth){ BitmapHelper.BitmapWorkerSingleTask task=new BitmapWorkerSingleTask(imageView); task.execute(webUrl,width+"",heigth+""); } /** * adapter中加載圖片 * @param fileUrl 圖片地址 * @param imageView 圖片控件 * @param width 要得到的寬度 * @param height 要的到的高度 */ public void loadMultiBitmapFromWeb(String fileUrl, ImageView imageView,int width,int height) { if (cancelPotentialWork(fileUrl, imageView)) { final BitmapMultiWorkerTask task = new BitmapMultiWorkerTask(imageView); final AsyncDrawable asyncDrawable =new AsyncDrawable(bmpHolder, task); imageView.setImageDrawable(asyncDrawable); task.execute(fileUrl,width+"",height+""); } } /** * 從網絡中加載一個需要的Bitmap; * @param webUrl * @param reqWidth * @param reqHeight * @return */ public Bitmap getBitmapFormWeb(String webUrl,int reqWidth,int reqHeight){ BitmapFactory.Options options=new BitmapFactory.Options(); options.inJustDecodeBounds=true; options.inSampleSize=calculateInSampleSize(options, reqWidth, reqHeight); return ConnInternet.loadBitmapFromNet(webUrl, options); } /** * 將圖片文件轉換成bitmap * * @param fileUrl * 圖片文件路徑 * @param width * 寬度 * @param height * 高度 * @param isThumbnail * 是否根據高寬生成縮略圖 * @return */ public Bitmap getBitmapFormFile(String fileUrl, int width, int height, boolean isThumbnail) { Bitmap bitmap=loadSingleBitmapFromFile(fileUrl,null, width, height); // 生成固定尺寸的縮略圖 if (isThumbnail) { bitmap = ThumbnailUtils.extractThumbnail(bitmap, width, height, ThumbnailUtils.OPTIONS_RECYCLE_INPUT); } cacheManager.addBitmapToCache(fileUrl, bitmap); return bitmap; } /** * 轉換圖片成圓形 * * @param bitmap * @return */ public static Bitmap changeBitmapToRound(Bitmap bitmap) { if(bitmap==null) return null; int width = bitmap.getWidth(); int height = bitmap.getHeight(); float roundPx; float left, top, right, bottom, dst_left, dst_top, dst_right, dst_bottom; if (width <= height) { roundPx = width / 2; left = 0; top = 0; right = width; bottom = width; height = width; dst_left = 0; dst_top = 0; dst_right = width; dst_bottom = width; } else { roundPx = height / 2; float clip = (width - height) / 2; left = clip; right = width - clip; top = 0; bottom = height; width = height; dst_left = 0; dst_top = 0; dst_right = height; dst_bottom = height; } Bitmap output = Bitmap.createBitmap(width, height, Config.ARGB_8888); Canvas canvas = new Canvas(output); final int color = 0xff424242; final Paint paint = new Paint(); final Rect src = new Rect((int) left, (int) top, (int) right, (int) bottom); final Rect dst = new Rect((int) dst_left, (int) dst_top, (int) dst_right, (int) dst_bottom); paint.setAntiAlias(true);// 設置畫筆無鋸齒 canvas.drawARGB(0, 0, 0, 0); // 填充整個Canvas paint.setColor(color); // 以下有兩種方法畫圓,drawRounRect和drawCircle // canvas.drawRoundRect(rectF, roundPx, roundPx, paint);// // 畫圓角矩形,第一個參數為圖形顯示區域,第二個參數和第三個參數分別是水平圓角半徑和垂直圓角半徑。 canvas.drawCircle(roundPx, roundPx, roundPx, paint); paint.setXfermode(new PorterDuffXfermode(Mode.SRC_IN));// 設置兩張圖片相交時的模式,參考http://trylovecatch.iteye.com/blog/1189452 canvas.drawBitmap(bitmap, src, dst, paint); // 以Mode.SRC_IN模式合並bitmap和已經draw了的Circle return output; } /** * 計算所需大小的圖片 的壓縮比例 * @param options * @param reqWidth 所需寬度 * @param reqHeight 所需高度 * @return 壓縮比例 */ public int calculateInSampleSize(BitmapFactory.Options options, int reqWidth, int reqHeight) { final int height = options.outHeight; final int width = options.outWidth; int inSampleSize = 1; if (height > reqHeight || width > reqWidth) { final int halfHeight = height / 2; final int halfWidth = width / 2; while ((halfHeight / inSampleSize) > reqHeight && (halfWidth / inSampleSize) > reqWidth) { inSampleSize *= 2; } } return inSampleSize; } /** * 檢查啟動的任務 * @param fileUrl 文件路徑 * @param imageView 目標控件 * @return 是否開始加載任務 */ private boolean cancelPotentialWork(String fileUrl, ImageView imageView) { final BitmapMultiWorkerTask bitmapWorkerTask = getBitmapWorkerTask(imageView); if (bitmapWorkerTask != null) { String bitmapUrl = bitmapWorkerTask.fileUrl; if (bitmapUrl!=null&&bitmapUrl != fileUrl) { bitmapWorkerTask.cancel(true); return true; } else { return false; } } return true; } /** * 獲取該圖片控件中的加載任務 * @param imageView * @return 返回該任務 */ private BitmapMultiWorkerTask 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; } private class AsyncDrawable extends BitmapDrawable { private final WeakReferencebitmapWorkerTaskReference; @SuppressWarnings("deprecation") public AsyncDrawable(Bitmap bitmap,BitmapMultiWorkerTask bitmapWorkerTask) { super(bitmap); bitmapWorkerTaskReference =new WeakReference (bitmapWorkerTask); } public BitmapMultiWorkerTask getBitmapWorkerTask() { return bitmapWorkerTaskReference.get(); } } private class BitmapMultiWorkerTask extends AsyncTask { private WeakReference imageViewReference; private String fileUrl; public BitmapMultiWorkerTask(ImageView imageView){ imageViewReference=new WeakReference (imageView); } @Override protected Bitmap doInBackground(String... params) { fileUrl=params[0]; int reqWidth=Integer.valueOf(params[1]); int reqHeight=Integer.valueOf(params[2]); Bitmap bitmap=getBitmapFormWeb(fileUrl,reqWidth,reqHeight); return bitmap; } @Override protected void onPostExecute(Bitmap bitmap) { if (isCancelled()) { bitmap = null; } if (imageViewReference != null) { final ImageView imageView = imageViewReference.get(); final BitmapMultiWorkerTask bitmapWorkerTask =getBitmapWorkerTask(imageView); if (this == bitmapWorkerTask && imageView != null) { if(bitmap!=null){ imageView.setImageBitmap(bitmap); cacheManager.addBitmapToCache(fileUrl, bitmap); } else{ imageView.setImageBitmap(bmpNoImg); } } } } } /** * 異步加載一張圖片 * @author [email protected] * */ private class BitmapWorkerSingleTask extends AsyncTask { private WeakReference imageViewReference; private String fileUrl=""; public BitmapWorkerSingleTask(ImageView imageView){ imageViewReference=new WeakReference (imageView); } @Override protected Bitmap doInBackground(String... params) { fileUrl=params[0]; int reqWidth=Integer.valueOf(params[1]); int reqHeight=Integer.valueOf(params[2]); Bitmap bitmap=getBitmapFormWeb(fileUrl,reqWidth,reqHeight); return bitmap; } @Override protected void onPostExecute(Bitmap bitmap) { if (imageViewReference != null) { final ImageView imageView = imageViewReference.get(); if (imageView != null) { if(bitmap!=null){ imageView.setImageBitmap(bitmap); } else{ imageView.setImageBitmap(bmpNoImg); } } } } } }
Android插件開發初探 對於Android的插件化其實已經討論已久了,但是市面上還沒有非常靠譜成熟的插件框架供我們使用。這裡我們就嘗試性的對比一下Java中,我們使用
Android4.4 fence機制分析 在任何一個系統中,無可避免的都會跟各種buffers打交道,最經典的模式就是消費-生產者模式,一個獨立的buffer在它們之間
今天這篇文章教給大家解決這幾個問題:1、如何通過AndroidStudio的git上傳項目到github2、如何通過AndroidStudio的git將存在的項目增加的類
寫在前面的話 在上一篇實現了通過布局泵拿到不同布局為listitem布局,然後實現聯系人的ListView,這一章要做的是拖拽ListView的Item項,本章原理是在上