Back to project page Common-Library.
The source code is released under:
Apache License
If you think the Android project Common-Library listed in this page is inappropriate, such as containing malicious code/tools or violating the copyright, please email info at java2s dot com, thanks.
package com.morgan.library.utils; /* w ww .j a va2s .c o m*/ import java.io.ByteArrayInputStream; import java.io.ByteArrayOutputStream; import android.graphics.Bitmap; import android.graphics.Bitmap.Config; import android.graphics.BitmapFactory; import android.graphics.Canvas; import android.graphics.LinearGradient; import android.graphics.Matrix; import android.graphics.Paint; import android.graphics.PixelFormat; import android.graphics.PorterDuff.Mode; import android.graphics.PorterDuffXfermode; import android.graphics.Rect; import android.graphics.RectF; import android.graphics.Shader.TileMode; import android.graphics.drawable.Drawable; /** * ????????????????? * * @author Morgan.Ji * */ public class ImageUtils { /** * ?????????????????????????? * * @param originalImage * @return */ public static Bitmap createReflectedImage(Bitmap originalImage) { // The gap we want between the reflection and the original image final int reflectionGap = 4; int width = originalImage.getWidth(); int height = originalImage.getHeight(); // This will not scale but will flip on the Y axis Matrix matrix = new Matrix(); matrix.preScale(1, -1); // Create a Bitmap with the flip matrix applied to it. // We only want the bottom half of the image Bitmap reflectionImage = Bitmap.createBitmap(originalImage, 0, height / 2, width, height / 2, matrix, false); // Create a new bitmap with same width but taller to fit reflection Bitmap bitmapWithReflection = Bitmap.createBitmap(width, (height + height / 2), Config.ARGB_8888); // Create a new Canvas with the bitmap that's big enough for // the image plus gap plus reflection Canvas canvas = new Canvas(bitmapWithReflection); // Draw in the original image canvas.drawBitmap(originalImage, 0, 0, null); // Draw in the gap Paint defaultPaint = new Paint(); canvas.drawRect(0, height, width, height + reflectionGap, defaultPaint); // Draw in the reflection canvas.drawBitmap(reflectionImage, 0, height + reflectionGap, null); // Create a shader that is a linear gradient that covers the reflection Paint paint = new Paint(); LinearGradient shader = new LinearGradient(0, originalImage.getHeight(), 0, bitmapWithReflection.getHeight() + reflectionGap, 0x70ffffff, 0x00ffffff, TileMode.CLAMP); // Set the paint to use this shader (linear gradient) paint.setShader(shader); // Set the Transfer mode to be porter duff and destination in paint.setXfermode(new PorterDuffXfermode(Mode.DST_IN)); // Draw a rectangle using the paint with our linear gradient canvas.drawRect(0, height, width, bitmapWithReflection.getHeight() + reflectionGap, paint); return bitmapWithReflection; } /** * drawable ?????bitmap * * @param drawable * @return */ public static Bitmap drawableToBitmap(Drawable drawable) { Bitmap bitmap = Bitmap .createBitmap( drawable.getIntrinsicWidth(), drawable.getIntrinsicHeight(), drawable.getOpacity() != PixelFormat.OPAQUE ? Bitmap.Config.ARGB_8888 : Bitmap.Config.RGB_565); Canvas canvas = new Canvas(bitmap); drawable.setBounds(0, 0, drawable.getIntrinsicWidth(), drawable.getIntrinsicHeight()); drawable.draw(canvas); return bitmap; } /** * ????? * * @param bit * @return */ public static Bitmap toRoundBitmap(Bitmap bitmap) { 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; top = 0; bottom = width; left = 0; right = 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); final RectF rectF = new RectF(dst); paint.setAntiAlias(true); canvas.drawARGB(0, 0, 0, 0); paint.setColor(color); canvas.drawRoundRect(rectF, roundPx, roundPx, paint); paint.setXfermode(new PorterDuffXfermode(Mode.SRC_IN)); canvas.drawBitmap(bitmap, src, dst, paint); if (bitmap != null && !bitmap.isRecycled()) { bitmap.recycle(); } return output; } /** * ???????????? * * @param src * ??????? * @param watermark * ????????? * @return ????????????? */ public static Bitmap addWaterMark(Bitmap src, Bitmap watermark) { if (src == null) return null; int w = src.getWidth(); int h = src.getHeight(); int ww = watermark.getWidth(); int wh = watermark.getHeight(); // ???????SRC?????????? Bitmap newb = Bitmap.createBitmap(w, h, Config.ARGB_8888); // ?????? Canvas cv = new Canvas(newb); // ? 0?0????????src cv.drawBitmap(src, 0, 0, null); // ?src???????????? cv.drawBitmap(watermark, w - ww + 5, h - wh + 5, null); // ???????????Canvas?????save???????????Canvas????????????????????????? cv.save(Canvas.ALL_SAVE_FLAG); // ????????????Canvas?????????????save????Canvas??????????????????? cv.restore(); return newb; } /** * ?????????????? * * @param bitmap * ????????? * @param pixels * ????? * @return ????? */ public static Bitmap toRoundCorner(Bitmap bitmap, int pixels) { Bitmap output = Bitmap.createBitmap(bitmap.getWidth(), bitmap.getHeight(), Config.ARGB_8888); Canvas canvas = new Canvas(output); final int color = 0xff424242; final Paint paint = new Paint(); final Rect rect = new Rect(0, 0, bitmap.getWidth(), bitmap.getHeight()); final RectF rectF = new RectF(rect); final float roundPx = pixels; paint.setAntiAlias(true); canvas.drawARGB(0, 0, 0, 0); paint.setColor(color); canvas.drawRoundRect(rectF, roundPx, roundPx, paint); paint.setXfermode(new PorterDuffXfermode(Mode.SRC_IN)); canvas.drawBitmap(bitmap, rect, rect, paint); bitmap.recycle(); return output; } /** * ?????????????? * * @param byteArray * ??????? * @return ??? */ public static Bitmap byteToBitmap(byte[] byteArray) { if (byteArray.length != 0) { return BitmapFactory .decodeByteArray(byteArray, 0, byteArray.length); } else { return null; } } /** * ???????????Drawable?? * * @param byteArray * ??????? * @return Drawable?? */ public static Drawable byteToDrawable(byte[] byteArray) { ByteArrayInputStream ins = new ByteArrayInputStream(byteArray); return Drawable.createFromStream(ins, null); } /** * ???????????????? * * @param bm * ??? * @return ??????? */ public static byte[] Bitmap2Bytes(Bitmap bm) { ByteArrayOutputStream baos = new ByteArrayOutputStream(); bm.compress(Bitmap.CompressFormat.PNG, 100, baos); return baos.toByteArray(); } /** * ????????????? * * @param first * @param second * @return */ public static Bitmap mergeBitmap(Bitmap first, Bitmap second) { if (first == null || first.isRecycled() || second == null || second.isRecycled()) { return null; } int fWidth = first.getWidth(); int fHeight = first.getHeight(); int sHeight = second.getHeight(); // create the new blank bitmap ???????SRC?????????? Bitmap newbmp = Bitmap.createBitmap(fWidth, fHeight + sHeight, Config.ARGB_8888); Canvas cv = new Canvas(newbmp); // ? 0?0????????bg cv.drawBitmap(first, 0, 0, null); // ? 0?0????????fg ?????????????? cv.drawBitmap(second, 0, fHeight, null); // save all clip cv.save(Canvas.ALL_SAVE_FLAG);// ??? // store cv.restore();// ?? return newbmp; } /** * ?????????????????? * * @param bg * ??? * @param bitmap * ????? * @return */ public static Bitmap componentBitmap(Bitmap bg, Bitmap bitmap) { int fWidth = bg.getWidth(); int fHeight = bg.getHeight(); int sWidth = bitmap.getWidth(); int sHeight = bitmap.getHeight(); // create the new blank bitmap ???????SRC?????????? Bitmap newbmp = Bitmap.createBitmap(fWidth, fHeight, Config.ARGB_8888); Canvas cv = new Canvas(newbmp); // ? 0?0????????bg cv.drawBitmap(bg, 0, 0, null); // ?0?0????????fg?????????????? cv.drawBitmap(bitmap, (fWidth - sWidth) / 2, (fHeight - sHeight) / 2, null); // ??? cv.save(Canvas.ALL_SAVE_FLAG); // store cv.restore();// ?? return newbmp; } }