Android comes with Bitmap out Memory processing, which is more convenient for third-party open-source users. It is good to practice it on your own. androidbitmap

Source: Internet
Author: User

Android comes with Bitmap out Memory processing, which is more convenient for third-party open-source users. It is good to practice it on your own. androidbitmap

/*** @ Author intbird@163.com * @ 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. porterduxfermode; import Ndroid. 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 BMP holder = null; private Bitmap BMP noimg = null; public BitmapHelper (Context context, int loadingId, int loadNoId) {cacheManager = CacheManager. getInstance (); BMP holder = BitmapFactory. decodeResource (context. getResources (), loadingId); BMP noimg = BitmapFactory. decodeResource (context. getResources (), loadNoId);}/*** load image * @ param type network file, local file, or resource file * @ param fileUrl url * @ param imageView control * @ param width Height * @ param height the specified width to be obtained */public void commonLoadBitmap (int type, String fileUrl, ImageView imageView, int width, int height) {// the memory and file do not contain images. Obtain the image again. 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 ;}}/*** sets ImageView to get the image with the specified height and width of the specified file address; * @ param fileUrl * @ param reqWidth: Target width * @ Param reqHeight target height * @ 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;}/*** sets ImageView as a network image to be loaded; * @ param webUrl * @ param imageView * @ param width * @ param heigth */public void loadSingleBitmapFromWeb (String webUrl, ImageView imageView, int width, int heigth) {BitmapHelper. bitmapWorkerSingleTask task = new bitmapworkersingletask(imageviewmediaworkflow task.exe cute (webUrl, width + "", heigth + "");}/*** the adapter loads an image * @ param fileUrl image address * @ para M imageView image control * @ param width the width to be obtained * @ param height the height to */public void loadMultiBitmapFromWeb (String fileUrl, ImageView imageView, int width, int height) {if (fileUrl, imageView) {final BitmapMultiWorkerTask task = new BitmapMultiWorkerTask (imageView); final AsyncDrawable asyncDrawable = new AsyncDrawable (BMP holder, task); imageView. setImageDrawable (asyncDrawable); task. ex Ecute (fileUrl, width + "", height + "") ;}}/*** load a required Bitmap from the network; * @ 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 );} /*** convert the image file to bitmap ** @ param fileUrl * path of the image file * @ param width * @ param height * @ param isThumbnail * Whether the image file is based on the height and width generate a thumbnail * @ return */public Bitmap getBitmapFormFile (String fileUrl, int width, int height, boolean isThumbnail) {Bitmap bitmap = loadSingleBitmapFromFile (fileUrl, null, width, height); // generate a fixed-size thumbnail if (isThumbnail) {bitmap = ThumbnailUtils. extractThumbnail (bitmap, widt H, height, ThumbnailUtils. OPTIONS_RECYCLE_INPUT);} cacheManager. addBitmapToCache (fileUrl, bitmap); return bitmap;}/*** convert the image to a circle ** @ 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, hei Ght, Config. ARGB_8888); Canvas canvas = new Canvas (output); final int color = 0xff0000242; 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); // you can specify a paint brush that does not have a sawtooth canvas. drawARGB (0, 0, 0, 0); // fill the entire Canvaspaint. setColor (color); // you can use either of the following methods to draw a circle, dra WRounRect and drawCircle // canvas. drawRoundRect (rectF, roundPx, roundPx, paint); // draw a rounded rectangle. The first parameter is the graphic display area, the second and third parameters are the radius of the horizontal and vertical rounded corners. Canvas. drawCircle (roundPx, paint); paint. setXfermode (new porterduduxfermode (Mode. SRC_IN); // set the pattern when two images are intersecting, refer to http://trylovecatch.iteye.com/blog/1189452canvas.drawBitmap (bitmap, src, dst, paint); // to Mode. merge bitmap and draw Circlereturn output in SRC_IN mode ;} /*** calculate the compression ratio of the image of the desired size * @ param options * @ param reqWidth: required width * @ param reqHeight required height * @ return compression ratio */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 inSample Size;}/*** check the startup task * @ param fileUrl file path * @ param imageView target control * @ return whether to start loading the task */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 ;} /*** get the loading task in the image control * @ param imageView * @ return returns this task */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 WeakReference <BitmapMultiWorkerTask> bitmapWorkerTaskReference; @ SuppressWarnings ("deprecation" Rawable (Bitmap bitmap, BitmapMultiWorkerTask bitmapWorkerTask) {super (bitmap); bitmapWorkerTaskReference = new WeakReference <timeout> (bitmapWorkerTask);} public BitmapMultiWorkerTask callback () {return bitmapWorkerTaskReference. get () ;}} private class BitmapMultiWorkerTask extends AsyncTask <String, Void, Bitmap> {private WeakReference <ImageView> imageViewReference; private String fileUrl; public BitmapMultiWorkerTask (ImageView imageView) {imageViewReference = new WeakReference <ImageView> (imageView) ;}@ Overrideprotected 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 (Bit Map 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 (BMP noimg); }}}/ *** asynchronously load an image * @ author intbird@163.com **/private class BitmapWorkerSingleTask extends AsyncTask <String, Void, bitmap> {private WeakReference <ImageView> imageViewReference; private String fileUrl = ""; public BitmapWorkerSingleTask (ImageView imageView) {imageVie WReference = new WeakReference <ImageView> (imageView) ;}@ Overrideprotected 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 ;}@ Overrideprotected void onPostExecute (Bitmap bitmap) {if (imageViewReference! = Null) {final ImageView imageView = imageViewReference. get (); if (imageView! = Null) {if (bitmap! = Null) {imageView. setImageBitmap (bitmap) ;}else {imageView. setImageBitmap (BMP noimg );}}}}}}


How does android restore bitmap processed images?

Have you used Bitmap. createBitmap (...) and other functions? Use two variables for storage. However, I found that Bitmap is very memory-consuming during actual design. When two or three large images are opened, an error is reported. I don't know how you handle this problem.

Android bitmap obtains images from the network and handles problem Overflow

You read it twice in total.
After the first read, that is, after the call method "BitmapFactory. decodeStream (is, null, options,
Is empty, so the second return is null.
Solution:
Use the byte array to cache the read stream first, and then use the "BitmapFactory. decodeByteArray ()" method for parsing.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.