Android仿简历生成图片功能,Android 根据View生成图片简易参考

一、分类

开发中,我们有时候需要根据View生成图片。

本文根据不同情况的View生成图片进行了一些示例,分类如下

第一种,普通View生成图片(view已经渲染加载到界面上)

第二种,无中生有,通过java代码创建的或者inflate创建

第三种,WebView 生成图片

第四种,ScrollView 生成图片

第五种,ListView 生成图片

第六种,RecyclerView 生成图片

还是图来的直接

44cc5f3f8de0

shot.gif

核心代码

public class SimpleUtils {

/**

* 将 Bitmap 保存到SD卡

* @param context

* @param mybitmap

* @param name

* @return

*/

public static boolean saveBitmapToSdCard(Context context, Bitmap mybitmap, String name){

boolean result = false;

//创建位图保存目录

String path = Environment.getExternalStorageDirectory() + "/1000ttt/";

File sd = new File(path);

if (!sd.exists()){

sd.mkdir();

}

File file = new File(path+name+".jpg");

FileOutputStream fileOutputStream = null;

if (!file.exists()){

try {

// 判断SD卡是否存在,并且是否具有读写权限

if(Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED)){

fileOutputStream = new FileOutputStream(file);

mybitmap.compress(Bitmap.CompressFormat.JPEG,100,fileOutputStream);

fileOutputStream.flush();

fileOutputStream.close();

//update gallery

Intent intent = new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE);

Uri uri = Uri.fromFile(file);

intent.setData(uri);

context.sendBroadcast(intent);

Toast.makeText(context, "保存成功", Toast.LENGTH_SHORT).show();

result = true;

}

else{

Toast.makeText(context, "不能读取到SD卡", Toast.LENGTH_SHORT).show();

}

} catch (FileNotFoundException e) {

e.printStackTrace();

} catch (IOException e) {

e.printStackTrace();

}

}

return result;

}

/**

* 手动测量摆放View

* 对于手动 inflate 或者其他方式代码生成加载的View进行测量,避免该View无尺寸

* @param v

* @param width

* @param height

*/

public static void layoutView(View v, int width, int height) {

// validate view.width and view.height

v.layout(0, 0, width, height);

int measuredWidth = View.MeasureSpec.makeMeasureSpec(width, View.MeasureSpec.EXACTLY);

int measuredHeight = View.MeasureSpec.makeMeasureSpec(height, View.MeasureSpec.EXACTLY);

// validate view.measurewidth and view.measureheight

v.measure(measuredWidth, measuredHeight);

v.layout(0, 0, v.getMeasuredWidth(), v.getMeasuredHeight());

}

public static int px2dip(Context context, float pxValue) {

final float scale = context.getResources().getDisplayMetrics().density;

return (int) (pxValue / scale + 0.5f);

}

/**

* 获取一个 View 的缓存视图

* (前提是这个View已经渲染完成显示在页面上)

* @param view

* @return

*/

public static Bitmap getCacheBitmapFromView(View view) {

final boolean drawingCacheEnabled = true;

view.setDrawingCacheEnabled(drawingCacheEnabled);

view.buildDrawingCache(drawingCacheEnabled);

final Bitmap drawingCache = view.getDrawingCache();

Bitmap bitmap;

if (drawingCache != null) {

bitmap = Bitmap.createBitmap(drawingCache);

view.setDrawingCacheEnabled(false);

} else {

bitmap = null;

}

return bitmap;

}

/**

* 对ScrollView进行截图

* @param scrollView

* @return

*/

public static Bitmap shotScrollView(ScrollView scrollView) {

int h = 0;

Bitmap bitmap = null;

for (int i = 0; i < scrollView.getChildCount(); i++) {

h += scrollView.getChildAt(i).getHeight();

scrollView.getChildAt(i).setBackgroundColor(Color.parseColor("#ffffff"));

}

bitmap = Bitmap.createBitmap(scrollView.getWidth(), h, Bitmap.Config.RGB_565);

final Canvas canvas = new Canvas(bitmap);

scrollView.draw(canvas);

return bitmap;

}

/**

* 对ListVie

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值