清华大佬耗费三个月吐血整理的几百G的资源,免费分享!....>>>
将Drawable转为Bitmap
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); // canvas.setBitmap(bitmap); drawable.setBounds(0, 0, drawable.getIntrinsicWidth(), drawable.getIntrinsicHeight()); drawable.draw(canvas); return bitmap; }
将Bitmap转为btye[]
public static byte[] bmpToByteArray(final Bitmap bmp, final boolean needRecycle) { ByteArrayOutputStream output = new ByteArrayOutputStream(); bmp.compress(CompressFormat.JPEG, 80, output); if (needRecycle) { bmp.recycle(); } byte[] result = output.toByteArray(); try { output.close(); } catch (Exception e) { e.printStackTrace(); } return result; }
将图片存储到sdcard中
public static void storeImageToSDCARD(Bitmap colorImage, String ImageName, String path) { File file = new File(path); if (!file.exists()) { file.mkdir(); } File imagefile = new File(file, ImageName + ".jpg"); try { imagefile.createNewFile(); FileOutputStream fos = new FileOutputStream(imagefile); colorImage.compress(Bitmap.CompressFormat.JPEG, 100, fos); fos.flush(); fos.close(); } catch (FileNotFoundException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } }
sdcard取图片
public static Bitmap getImg(String path,String picName) { if (!Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED)) { return null; } try { File file = new File(path, picName + ".png"); FileInputStream inputStream = new FileInputStream(file); byte[] b = new byte[inputStream.available()]; inputStream.read(b); Bitmap bitmap = BitmapFactory.decodeByteArray(b, 0, b.length); return bitmap; } catch (Exception e) { return null; } }
网络获取图片
public static Bitmap getImageByNet(String urlpath) throws Exception { URL url = new URL(urlpath); HttpURLConnection conn = (HttpURLConnection) url.openConnection(); conn.setRequestMethod("GET"); conn.setConnectTimeout(5 * 1000); Bitmap bitmap = null; if (conn.getResponseCode() == 200) { InputStream inputStream = conn.getInputStream(); bitmap = BitmapFactory.decodeStream(inputStream); } return bitmap; }
将两个bitmap对象整合并保存为一张图片
public Bitmap combineBitmap(Bitmap background, Bitmap foreground) { //第一张图片的宽高 int bgWidth = background.getWidth(); int bgHeight = background.getHeight(); //第二章图片的宽高 int fgWidth = foreground.getWidth(); int fgHeight = foreground.getHeight(); //创建一个新的bitmao 高度等于两张高度的总和 用来竖列拼接 Bitmap newmap = Bitmap.createBitmap(bgWidth, bgHeight + fgHeight, android.graphics.Bitmap.Config.ARGB_8888); Canvas canvas = new Canvas(newmap); //画上第一张图片 canvas.drawBitmap(background, 0, 0, null); //从第一张图片的下边开始画入第二张图片 canvas.drawBitmap(foreground, 0, bgHeight, null); return newmap; }
Bitmap旋转一定角度
public static Bitmap rotate(Bitmap b, int degrees) { if (degrees != 0 && b != null) { Matrix m = new Matrix(); m.setRotate(degrees, (float) b.getWidth() / 2, (float) b.getHeight() / 2); try { Bitmap b2 = Bitmap.createBitmap(b, 0, 0, b.getWidth(), b.getHeight(), m, true); return b2;// 正常情况下返回旋转角度的图 } catch (OutOfMemoryError ex) { return b;// 内存溢出返回原图 } finally { b.recycle();// 释放资源 } } return b; }
Bitmap画一个圆角图
public static Bitmap getRoundedCornerBitmap(Bitmap bitmap, float roundPx) { if (bitmap == null) { return bitmap; } try { 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); 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); return output; } catch (OutOfMemoryError e) { // TODO: handle exception System.gc(); return null; } }
从view 得到图片
public static Bitmap getBitmapFromView(View view) { view.destroyDrawingCache(); view.measure(View.MeasureSpec.makeMeasureSpec(0, View.MeasureSpec.UNSPECIFIED), View.MeasureSpec .makeMeasureSpec(0, View.MeasureSpec.UNSPECIFIED)); view.layout(0, 0, view.getMeasuredWidth(), view.getMeasuredHeight()); view.setDrawingCacheEnabled(true); Bitmap bitmap = view.getDrawingCache(true); return bitmap; }
添加水印
public static Bitmap watermarkBitmap(Bitmap src, Bitmap watermark, String title) { if (src == null) { return null; } int w = src.getWidth(); int h = src.getHeight(); // 需要处理图片太大造成的内存超过的问题,这里我的图片很小所以不写相应代码了 Bitmap newb = Bitmap.createBitmap(w, h, Config.ARGB_8888);// 创建一个新的和SRC长度宽度一样的位图 Canvas cv = new Canvas(newb); cv.drawBitmap(src, 0, 0, null);// 在 0,0坐标开始画入src Paint paint = new Paint(); // 加入图片 if (watermark != null) { int ww = watermark.getWidth(); int wh = watermark.getHeight(); paint.setAlpha(50); cv.drawBitmap(watermark, w - ww + 5, h - wh + 5, paint);// 在src的右下角画入水印 } // 加入文字 if (title != null) { String familyName = "宋体"; Typeface font = Typeface.create(familyName, Typeface.BOLD); TextPaint textPaint = new TextPaint(); textPaint.setColor(Color.RED); textPaint.setTypeface(font); textPaint.setTextSize(22); // 这里是自动换行的 StaticLayout layout = new StaticLayout(title, textPaint, w, Alignment.ALIGN_NORMAL, 1.0F, 0.0F, true); layout.draw(cv); // 文字就加左上角算了 // cv.drawText(title,0,40,paint); } cv.save(Canvas.ALL_SAVE_FLAG);// 保存 cv.restore();// 存储 return newb; }