哎! 百忙中还在加班写简书真是醉了,我竟然在加班写简书
算了先给个demo吧,不想听我啰嗦的或者是看不懂的直接去下载吧(https://github.com/1037438704/SharedLongGraph)献给各位android新手们
虽然我也是新手后序有可能去更新毕竟分享有一堆
首先是一个工具类上面是我也看不懂的东西
反正就是往上复制就对了
public class BitmapViewUtils {
private BitmapgetViewBitmap(View v) {
v.clearFocus();
v.setPressed(false);
boolean willNotCache = v.willNotCacheDrawing();
v.setWillNotCacheDrawing(false);
int color = v.getDrawingCacheBackgroundColor();
v.setDrawingCacheBackgroundColor(0);
if (color !=0) {
v.destroyDrawingCache();
}
v.buildDrawingCache();
Bitmap cacheBitmap = v.getDrawingCache();
if (cacheBitmap ==null) {
return null;
}
Bitmap bitmap = Bitmap.createBitmap(cacheBitmap);
v.destroyDrawingCache();
v.setWillNotCacheDrawing(willNotCache);
v.setDrawingCacheBackgroundColor(color);
return bitmap;
}
public static BitmapgetBitmapByView(ScrollView scrollView) {
int h =0;
Bitmap bitmap =null;
for (int i =0; i < scrollView.getChildCount(); i++) {
h += scrollView.getChildAt(i).getHeight();
}
bitmap = Bitmap.createBitmap(scrollView.getWidth(), h,
Bitmap.Config.RGB_565);
final Canvas canvas =new Canvas(bitmap);
scrollView.draw(canvas);
return bitmap;
}
public static void savePhotoToSDCard(Bitmap photoBitmap, String path, String photoName) {
if (checkSDCardAvailable()) {
File dir =new File(path);
if (!dir.exists()) {
dir.mkdirs();
}
File photoFile =new File(path, photoName +".png");
FileOutputStream fileOutputStream =null;
try {
fileOutputStream =new FileOutputStream(photoFile);
if (photoBitmap !=null) {
if (photoBitmap.compress(Bitmap.CompressFormat.PNG, 100, fileOutputStream)) {
fileOutputStream.flush();
}
}
}catch (FileNotFoundException e) {
photoFile.delete();
e.printStackTrace();
}catch (IOException e) {
photoFile.delete();
e.printStackTrace();
}finally {
try {
fileOutputStream.close();
}catch (IOException e) {
e.printStackTrace();
}
}
}
}
public static boolean checkSDCardAvailable() {
return android.os.Environment.getExternalStorageState().equals(android.os.Environment.MEDIA_MOUNTED);
}
private static final StringSD_PATH ="/sdcard/dskqxt/pic/";
private static final StringIN_PATH ="/dskqxt/pic/";
/**
* 随机生产文件名
*
* @return
*/
private static StringgenerateFileName() {
return UUID.randomUUID().toString();
}
/**
* 保存bitmap到本地
*
* @param context
* @param mBitmap
* @return
*/
public static StringsaveBitmap(Context context, Bitmap mBitmap) {
String savePath;
File filePic;
if (Environment.getExternalStorageState().equals(
Environment.MEDIA_MOUNTED)) {
savePath =SD_PATH;
}else {
savePath = context.getApplicationContext().getFilesDir()
.getAbsolutePath()
+IN_PATH;
}
try {
filePic =new File(savePath +generateFileName() +".jpg");
if (!filePic.exists()) {
filePic.getParentFile().mkdirs();
filePic.createNewFile();
}
FileOutputStream fos =new FileOutputStream(filePic);
mBitmap.compress(Bitmap.CompressFormat.JPEG, 100, fos);
fos.flush();
fos.close();
}catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
return null;
}
return filePic.getAbsolutePath();
}
}
好的这个就是工具类
调用方法是 只要不傻应该能看懂(好吧除了我哈哈)
ScrollViewscro = findViewById(R.id.scro);
Bitmap bitmap = BitmapViewUtils.getBitmapByView(scro);//contentLly是布局文件
String s = BitmapViewUtils.saveBitmap(MainActivity.this, bitmap);
s是保存路径 你手机上的 不要忘了给权限不然会出不来
网友评论