赞
踩
- /**
- * get Screen Px
- */
- private void getCurrentScreenPx() {
- DisplayMetrics metric = new DisplayMetrics();
- getWindowManager().getDefaultDisplay().getMetrics(metric);
- int mScreenWidth = metric.widthPixels; // 屏幕宽度(像素)
- int mScreenHeight = metric.heightPixels; // 屏幕高度(像素)
- //get print view
- View mPrintView = LayoutInflater.from(this).inflate(R.layout.print, null, false);
- initPrintView(mPrintView);
- Log.d("wc_test","mPrintView-getCurrentScreenPx-->"+mScreenWidth+"--->"+mScreenHeight);
- Log.d("wc_test","mPrintView--->"+mPrintView.getWidth()+"--->"+mPrintView.getHeight());
- layoutView(mPrintView, mScreenWidth, mScreenHeight);//指定打印view大小
- }
-
- private void layoutView(View mPrintView, int mPrintWidth, int mPrintHeight) {
- //指定整个View的大小 参数是左上角 和右下角的坐标
- mPrintView.layout(0, 0, mPrintWidth, mPrintHeight);
- int measuredWidth = View.MeasureSpec.makeMeasureSpec(mPrintWidth, View.MeasureSpec.EXACTLY);
- int measuredHeight = View.MeasureSpec.makeMeasureSpec(mPrintHeight, View.MeasureSpec.AT_MOST);
- //measure完后,并不会实际改变View的尺寸,需要调用View.layout方法去进行布局
- //调用layout函数后,View的大小将会变成你想要设置成的大小
- mPrintView.measure(measuredWidth, measuredHeight);
- mPrintView.layout(0, 0,mPrintView.getMeasuredWidth(), mPrintView.getMeasuredHeight());
- }
2.进行View绘制。如果UI界面需要显示,则直接设置此bitmap即可。
- private Bitmap loadBitmapFromView(ScrollView mPrintScrollView) {
- int w = mPrintScrollView.getWidth();
- int h = mPrintScrollView.getHeight();
- Bitmap bmp = Bitmap.createBitmap(w, h, Bitmap.Config.ARGB_8888);
- Canvas canvas = new Canvas(bmp);
- canvas.drawColor(Color.WHITE);
- /** 如果不设置canvas画布为白色,则生成透明 */
- mPrintScrollView.layout(0, 0, w, h);
- mPrintScrollView.draw(canvas);
- return bmp;
- }
2.保存照片至本地,设置完view之后,记得执行
mPrintScrollView.destroyDrawingCache();
- 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 {
- if(fileOutputStream != null){
- fileOutputStream.close();
- }
- } catch (IOException e) {
- e.printStackTrace();
- }
- }
- }
- }
-
- /**
- *check sdCard
- */
- public static boolean checkSDCardAvailable() {
- return android.os.Environment.getExternalStorageState().equals(android.os.Environment.MEDIA_MOUNTED);
- }
1.如果当前View没有超出界面
- private Bitmap getViewBitmap(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;
- }
2.当前view超出界面
- public static Bitmap getBitmapByView(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;
- }
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。