您好,登錄后才能下訂單哦!
如何正確的使用Android中的圖片工具類?針對這個問題,這篇文章詳細介紹了相對應的分析和解答,希望可以幫助更多想解決這個問題的小伙伴找到更簡單易行的方法。
使用方法如下:
import java.io.ByteArrayOutputStream; import java.io.File; import java.io.FileNotFoundException; import java.io.FileOutputStream; import java.io.IOException; import android.content.Context; import android.graphics.Bitmap; import android.graphics.Bitmap.CompressFormat; import android.graphics.Bitmap.Config; import android.graphics.BitmapFactory; import android.graphics.Canvas; import android.graphics.ColorMatrix; import android.graphics.ColorMatrixColorFilter; import android.graphics.LinearGradient; import android.graphics.Matrix; import android.graphics.Paint; import android.graphics.PorterDuff; import android.graphics.PorterDuff.Mode; import android.graphics.PorterDuffXfermode; import android.graphics.Rect; import android.graphics.RectF; import android.graphics.Shader; import android.graphics.drawable.BitmapDrawable; import android.graphics.drawable.Drawable; /** * 圖片工具類 * @author wwj * 2013/7/3 */ public class ImageUtil { /**圖片的八個位置**/ public static final int TOP = 0; //上 public static final int BOTTOM = 1; //下 public static final int LEFT = 2; //左 public static final int RIGHT = 3; //右 public static final int LEFT_TOP = 4; //左上 public static final int LEFT_BOTTOM = 5; //左下 public static final int RIGHT_TOP = 6; //右上 public static final int RIGHT_BOTTOM = 7; //右下 /** * 圖像的放大縮小方法 * @param src 源位圖對象 * @param scaleX 寬度比例系數 * @param scaleY 高度比例系數 * @return 返回位圖對象 */ public static Bitmap zoomBitmap(Bitmap src, float scaleX, float scaleY) { Matrix matrix = new Matrix(); matrix.setScale(scaleX, scaleY); Bitmap t_bitmap = Bitmap.createBitmap(src, 0, 0, src.getWidth(), src.getHeight(), matrix, true); return t_bitmap; } /** * 圖像放大縮小--根據寬度和高度 * @param src * @param width * @param height * @return */ public static Bitmap zoomBimtap(Bitmap src, int width, int height) { return Bitmap.createScaledBitmap(src, width, height, true); } /** * 將Drawable轉為Bitmap對象 * @param drawable * @return */ public static Bitmap drawableToBitmap(Drawable drawable) { return ((BitmapDrawable)drawable).getBitmap(); } /** * 將Bitmap轉換為Drawable對象 * @param bitmap * @return */ public static Drawable bitmapToDrawable(Bitmap bitmap) { Drawable drawable = new BitmapDrawable(bitmap); return drawable; } /** * Bitmap轉byte[] * @param bitmap * @return */ public static byte[] bitmapToByte(Bitmap bitmap) { ByteArrayOutputStream out = new ByteArrayOutputStream(); bitmap.compress(Bitmap.CompressFormat.PNG, 100, out); return out.toByteArray(); } /** * byte[]轉Bitmap * @param data * @return */ public static Bitmap byteToBitmap(byte[] data) { if(data.length != 0) { return BitmapFactory.decodeByteArray(data, 0, data.length); } return null; } /** * 繪制帶圓角的圖像 * @param src * @param radius * @return */ public static Bitmap createRoundedCornerBitmap(Bitmap src, int radius) { final int w = src.getWidth(); final int h = src.getHeight(); // 高清量32位圖 Bitmap bitmap = Bitmap.createBitmap(w, h, Config.ARGB_8888); Paint paint = new Paint(); Canvas canvas = new Canvas(bitmap); canvas.drawARGB(0, 0, 0, 0); paint.setColor(0xff424242); // 防止邊緣的鋸齒 paint.setFilterBitmap(true); Rect rect = new Rect(0, 0, w, h); RectF rectf = new RectF(rect); // 繪制帶圓角的矩形 canvas.drawRoundRect(rectf, radius, radius, paint); // 取兩層繪制交集,顯示上層 paint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.SRC_IN)); // 繪制圖像 canvas.drawBitmap(src, rect, rect, paint); return bitmap; } /** * 創建選中帶提示圖片 * @param context * @param srcId * @param tipId * @return */ public static Drawable createSelectedTip(Context context, int srcId, int tipId) { Bitmap src = BitmapFactory.decodeResource(context.getResources(), srcId); Bitmap tip = BitmapFactory.decodeResource(context.getResources(), tipId); final int w = src.getWidth(); final int h = src.getHeight(); Bitmap bitmap = Bitmap.createBitmap(w, h, Config.ARGB_8888); Paint paint = new Paint(); Canvas canvas = new Canvas(bitmap); //繪制原圖 canvas.drawBitmap(src, 0, 0, paint); //繪制提示圖片 canvas.drawBitmap(tip, (w - tip.getWidth()), 0, paint); return bitmapToDrawable(bitmap); } /** * 帶倒影的圖像 * @param src * @return */ public static Bitmap createReflectionBitmap(Bitmap src) { // 兩個圖像間的空隙 final int spacing = 4; final int w = src.getWidth(); final int h = src.getHeight(); // 繪制高質量32位圖 Bitmap bitmap = Bitmap.createBitmap(w, h + h / 2 + spacing, Config.ARGB_8888); // 創建燕X軸的倒影圖像 Matrix m = new Matrix(); m.setScale(1, -1); Bitmap t_bitmap = Bitmap.createBitmap(src, 0, h / 2, w, h / 2, m, true); Canvas canvas = new Canvas(bitmap); Paint paint = new Paint(); // 繪制原圖像 canvas.drawBitmap(src, 0, 0, paint); // 繪制倒影圖像 canvas.drawBitmap(t_bitmap, 0, h + spacing, paint); // 線性渲染-沿Y軸高到低渲染 Shader shader = new LinearGradient(0, h + spacing, 0, h + spacing + h / 2, 0x70ffffff, 0x00ffffff, Shader.TileMode.MIRROR); paint.setShader(shader); // 取兩層繪制交集,顯示下層。 paint.setXfermode(new PorterDuffXfermode(Mode.DST_IN)); // 繪制渲染倒影的矩形 canvas.drawRect(0, h + spacing, w, h + h / 2 + spacing, paint); return bitmap; } /** * 獨立的倒影圖像 * @param src * @return */ public static Bitmap createReflectionBitmapForSingle(Bitmap src) { final int w = src.getWidth(); final int h = src.getHeight(); // 繪制高質量32位圖 Bitmap bitmap = Bitmap.createBitmap(w, h / 2, Config.ARGB_8888); // 創建沿X軸的倒影圖像 Matrix m = new Matrix(); m.setScale(1, -1); Bitmap t_bitmap = Bitmap.createBitmap(src, 0, h / 2, w, h / 2, m, true); Canvas canvas = new Canvas(bitmap); Paint paint = new Paint(); // 繪制倒影圖像 canvas.drawBitmap(t_bitmap, 0, 0, paint); // 線性渲染-沿Y軸高到低渲染 Shader shader = new LinearGradient(0, 0, 0, h / 2, 0x70ffffff, 0x00ffffff, Shader.TileMode.MIRROR); paint.setShader(shader); // 取兩層繪制交集。顯示下層。 paint.setXfermode(new PorterDuffXfermode(Mode.DST_IN)); // 繪制渲染倒影的矩形 canvas.drawRect(0, 0, w, h / 2, paint); return bitmap; } public static Bitmap createGreyBitmap(Bitmap src) { final int w = src.getWidth(); final int h = src.getHeight(); Bitmap bitmap = Bitmap.createBitmap(w, h, Config.ARGB_8888); Canvas canvas = new Canvas(bitmap); Paint paint = new Paint(); // 顏色變換的矩陣 ColorMatrix matrix = new ColorMatrix(); // saturation 飽和度值,最小可設為0,此時對應的是灰度圖;為1表示飽和度不變,設置大于1,就顯示過飽和 matrix.setSaturation(0); ColorMatrixColorFilter filter = new ColorMatrixColorFilter(matrix); paint.setColorFilter(filter); canvas.drawBitmap(src, 0, 0, paint); return bitmap; } /** * 保存圖片 * @param src * @param filepath * @param format:[Bitmap.CompressFormat.PNG,Bitmap.CompressFormat.JPEG] * @return */ public static boolean saveImage(Bitmap src, String filepath, CompressFormat format) { boolean rs = false; File file = new File(filepath); try { FileOutputStream out = new FileOutputStream(file); if(src.compress(format, 100, out)) { out.flush(); //寫入流 } out.close(); } catch (FileNotFoundException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } return rs; } /** * 添加水印效果 * @param src 源位圖 * @param watermark 水印 * @param direction 方向 * @param spacing 間距 * @return */ public static Bitmap createWatermark(Bitmap src, Bitmap watermark, int direction, int spacing) { final int w = src.getWidth(); final int h = src.getHeight(); Bitmap bitmap = Bitmap.createBitmap(w, h, Config.ARGB_8888); Canvas canvas = new Canvas(bitmap); canvas.drawBitmap(src, 0, 0, null); if(direction == LEFT_TOP) { canvas.drawBitmap(watermark, spacing, spacing, null); } else if(direction == LEFT_BOTTOM){ canvas.drawBitmap(watermark, spacing, h - watermark.getHeight() - spacing, null); } else if(direction == RIGHT_TOP) { canvas.drawBitmap(watermark, w - watermark.getWidth() - spacing, spacing, null); } else if(direction == RIGHT_BOTTOM) { canvas.drawBitmap(watermark, w - watermark.getWidth() - spacing, h - watermark.getHeight() - spacing, null); } return bitmap; } /** * 合成圖像 * @param direction * @param bitmaps * @return */ public static Bitmap composeBitmap(int direction, Bitmap... bitmaps) { if(bitmaps.length < 2) { return null; } Bitmap firstBitmap = bitmaps[0]; for (int i = 0; i < bitmaps.length; i++) { firstBitmap = composeBitmap(firstBitmap, bitmaps[i], direction); } return firstBitmap; } /** * 合成兩張圖像 * @param firstBitmap * @param secondBitmap * @param direction * @return */ private static Bitmap composeBitmap(Bitmap firstBitmap, Bitmap secondBitmap, int direction) { if(firstBitmap == null) { return null; } if(secondBitmap == null) { return firstBitmap; } final int fw = firstBitmap.getWidth(); final int fh = firstBitmap.getHeight(); final int sw = secondBitmap.getWidth(); final int sh = secondBitmap.getHeight(); Bitmap bitmap = null; Canvas canvas = null; if(direction == TOP) { bitmap = Bitmap.createBitmap(sw > fw ? sw : fw, fh + sh, Config.ARGB_8888); canvas = new Canvas(bitmap); canvas.drawBitmap(secondBitmap, 0, 0, null); canvas.drawBitmap(firstBitmap, 0, sh, null); } else if(direction == BOTTOM) { bitmap = Bitmap.createBitmap(fw > sw ? fw : sw, fh + sh, Config.ARGB_8888); canvas = new Canvas(bitmap); canvas.drawBitmap(firstBitmap, 0, 0, null); canvas.drawBitmap(secondBitmap, 0, fh, null); } else if(direction == LEFT) { bitmap = Bitmap.createBitmap(fw + sw, sh > fh ? sh : fh, Config.ARGB_8888); canvas = new Canvas(bitmap); canvas.drawBitmap(secondBitmap, 0, 0, null); canvas.drawBitmap(firstBitmap, sw, 0, null); } else if(direction == RIGHT) { bitmap = Bitmap.createBitmap(fw + sw, fh > sh ? fh : sh, Config.ARGB_8888); canvas = new Canvas(bitmap); canvas.drawBitmap(firstBitmap, 0, 0, null); canvas.drawBitmap(secondBitmap, fw, 0, null); } return bitmap; } }
關于如何正確的使用Android中的圖片工具類問題的解答就分享到這里了,希望以上內容可以對大家有一定的幫助,如果你還有很多疑惑沒有解開,可以關注億速云行業資訊頻道了解更多相關知識。
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。