工具類 ViewToBitmapUtils.ktjava
import android.app.Activity
import android.graphics.*
import android.view.View
import android.graphics.Bitmap
import android.text.TextUtils
/**
* 把任意view保存到bitmap中
*/
class ViewToBitmapUtils {
companion object{
//從view 中獲取bitmap
fun getViewBitmap(view: View?):Bitmap?{
if(view==null){
return null
}
var dp20 = Utils.dp2px(20f)
var dp30 = Utils.dp2px(30f)
val cardBitmap = Bitmap.createBitmap(view.width,view.height,Bitmap.Config.ARGB_8888)
var cardCanvas = Canvas(cardBitmap)
// cardCanvas.drawFilter = PaintFlagsDrawFilter(0, Paint.ANTI_ALIAS_FLAG or Paint.FILTER_BITMAP_FLAG)
view.draw(cardCanvas)//繪製前景
val bgBitmap = Bitmap.createBitmap(view.width+dp20*2,view.height+dp30*2,Bitmap.Config.ARGB_8888)
val bgCanvas = Canvas(bgBitmap);
bgCanvas.drawFilter = PaintFlagsDrawFilter(0, Paint.ANTI_ALIAS_FLAG or Paint.FILTER_BITMAP_FLAG)
bgCanvas.drawColor(Color.parseColor("#F1F1F1"))
bgCanvas.drawBitmap(cardBitmap,dp20.toFloat(),dp30.toFloat(), Paint())
return bgBitmap
}
//合併兩個view到一個bitmap,一樣給這兩個view增長一個背景圖片
fun combineView(topView: View,bottomView: View): Bitmap {
val topWidth = topView.width.toFloat()
val topHeight = topView.height.toFloat()
var dp20 = Utils.dp2px(20f)
var dp30 = Utils.dp2px(30f)
bottomView.measure(View.MeasureSpec.makeMeasureSpec(topView.width,View.MeasureSpec.EXACTLY),View.MeasureSpec.makeMeasureSpec(0,View.MeasureSpec.UNSPECIFIED));
bottomView.layout(0,0,topView.width,bottomView.measuredHeight);
val bottomWidth = bottomView.width.toFloat()
val bottomHeight = bottomView.height.toFloat()
val width = Math.max(topWidth,bottomWidth)+dp20*2 //取最大的寬度做爲最終寬度 增長20dp*2
val height = topHeight + bottomHeight + dp30*2 //計算總高度 增長 30dp*2
val topBitmap = Bitmap.createBitmap(width.toInt(), topHeight.toInt(), Bitmap.Config.ARGB_8888)//頂部圖片
topView.draw(Canvas(topBitmap))
val bottomBitmap = Bitmap.createBitmap(width.toInt(), bottomHeight.toInt(), Bitmap.Config.ARGB_8888)//底部圖片
bottomView.draw(Canvas(bottomBitmap))
val bitmap = Bitmap.createBitmap(width.toInt(), height.toInt(), Bitmap.Config.ARGB_8888)//底圖
val canvas = Canvas(bitmap)//畫布
canvas.drawFilter = PaintFlagsDrawFilter(0, Paint.ANTI_ALIAS_FLAG or Paint.FILTER_BITMAP_FLAG)
canvas.drawColor(Color.parseColor("#D9D9D9"))
val paint = Paint()
canvas.drawBitmap(topBitmap, dp20.toFloat(), dp30.toFloat(), paint)//畫頂部
canvas.drawBitmap(bottomBitmap, dp20.toFloat(), topHeight+dp30.toFloat(), paint)//畫底部
return bitmap
}
//把bitmap 保存到文件
fun bitmapToFile(context: Activity, bitmap:Bitmap?, endName:String, isSave:Boolean): String? {
if(bitmap==null || TextUtils.isEmpty(endName)){
return null
}
if(isSave){
return ImageUtil.saveAnyImageToGallery(context,bitmap,"share_$endName.jpg")
}else {
return ImageUtil.saveImageToTempDir(context,bitmap,"share_$endName.jpg")
}
}
}
}
複製代碼
ImageUtils.java 方法android
/**保存任意圖片到相冊
*
* @param context
* @param bmp 圖片位圖
* @param fileName 圖片文件名字,不含路徑
*/
public static String saveAnyImageToGallery(Context context, Bitmap bmp,String fileName) {
// 首先保存圖片
String storePath = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES).getAbsolutePath() + File.separator;
File appDir = new File(storePath);
if (!appDir.exists()) {
appDir.mkdirs();
}
String filePath = storePath + File.separator + fileName;
createImgToFile(bmp, filePath);
FileUtil.fileScan(context, filePath);
return filePath;
}
public static void createImgToFile(Bitmap bmp, String filename) {
FileOutputStream b = null;
try {
b = new FileOutputStream(filename);
bmp.compress(Bitmap.CompressFormat.JPEG, 100, b);// 把數據寫入文件
} catch (FileNotFoundException e) {
e.printStackTrace();
} finally {
try {
if(b!=null){
b.flush();
b.close();
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
/**保存任意圖片到SDcard koolearn temp臨時文件夾下 用完就刪除的那種
*
* @param context
* @param bmp 圖片位圖
* @param fileName 圖片文件名字,不含路徑
*/
public static String saveImageToTempDir(Context context, Bitmap bmp,String fileName) {
// 首先保存圖片
File storePath = new File(FileUtil.createKoolearnDir()+File.separator+"temp","share_"+System.currentTimeMillis()+".jpg");
if (!storePath.getParentFile().exists()) {
storePath.mkdirs();
}else{
//有此文件夾則刪除此文件夾下面全部圖片,作到每次都清空
for (int i=0;i<storePath.getParentFile().listFiles().length;i++){
storePath.getParentFile().listFiles()[i].delete();
}
}
createImgToFile(bmp, storePath.getAbsolutePath());
return storePath.getAbsolutePath();
}
複製代碼