├── .classpath
├── .gitignore
├── .project
├── .settings
├── org.eclipse.core.resources.prefs
└── org.eclipse.jdt.core.prefs
├── AndroidManifest.xml
├── README.md
├── ic_launcher-web.png
├── libs
└── android-support-v4.jar
├── proguard-project.txt
├── project.properties
├── res
├── drawable-hdpi
│ └── ic_launcher.png
├── drawable-mdpi
│ └── ic_launcher.png
├── drawable-xhdpi
│ ├── ic_launcher.png
│ └── pic.jpg
├── drawable-xxhdpi
│ └── ic_launcher.png
├── layout
│ ├── layout_main.xml
│ ├── layout_scrollview_test.xml
│ └── layout_webview_test.xml
├── menu
│ └── content_to_picture.xml
├── values-v11
│ └── styles.xml
├── values-v14
│ └── styles.xml
├── values-w820dp
│ └── dimens.xml
└── values
│ ├── dimens.xml
│ ├── strings.xml
│ └── styles.xml
└── src
└── com
└── jarlen
└── picture
├── ContentToPictureUtils.java
├── ScrollViewToPictureActivity.java
├── TestMainActivity.java
└── WebviewToPictureActivity.java
/.classpath:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
7 |
8 |
9 |
10 |
--------------------------------------------------------------------------------
/.gitignore:
--------------------------------------------------------------------------------
1 | # Built application files
2 | *.apk
3 | *.ap_
4 |
5 | # Files for the Dalvik VM
6 | *.dex
7 |
8 | # Java class files
9 | *.class
10 |
11 | # Generated files
12 | bin/
13 | gen/
14 |
15 | # Gradle files
16 | .gradle/
17 | build/
18 | /*/build/
19 |
20 | # Local configuration file (sdk path, etc)
21 | local.properties
22 |
23 | # Proguard folder generated by Eclipse
24 | proguard/
25 |
26 | # Log Files
27 | *.log
28 |
--------------------------------------------------------------------------------
/.project:
--------------------------------------------------------------------------------
1 |
2 |
3 | ContentToPicture
4 |
5 |
6 |
7 |
8 |
9 | com.android.ide.eclipse.adt.ResourceManagerBuilder
10 |
11 |
12 |
13 |
14 | com.android.ide.eclipse.adt.PreCompilerBuilder
15 |
16 |
17 |
18 |
19 | org.eclipse.jdt.core.javabuilder
20 |
21 |
22 |
23 |
24 | com.android.ide.eclipse.adt.ApkBuilder
25 |
26 |
27 |
28 |
29 |
30 | com.android.ide.eclipse.adt.AndroidNature
31 | org.eclipse.jdt.core.javanature
32 |
33 |
34 |
--------------------------------------------------------------------------------
/.settings/org.eclipse.core.resources.prefs:
--------------------------------------------------------------------------------
1 | eclipse.preferences.version=1
2 | encoding//src/com/jarlen/picture/ContentToPictureUtils.java=UTF-8
3 | encoding//src/com/jarlen/picture/ScrollViewToPictureActivity.java=UTF-8
4 | encoding//src/com/jarlen/picture/WebviewToPictureActivity.java=UTF-8
5 | encoding/=UTF-8
6 |
--------------------------------------------------------------------------------
/.settings/org.eclipse.jdt.core.prefs:
--------------------------------------------------------------------------------
1 | eclipse.preferences.version=1
2 | org.eclipse.jdt.core.compiler.codegen.targetPlatform=1.6
3 | org.eclipse.jdt.core.compiler.compliance=1.6
4 | org.eclipse.jdt.core.compiler.source=1.6
5 |
--------------------------------------------------------------------------------
/AndroidManifest.xml:
--------------------------------------------------------------------------------
1 |
2 |
6 |
7 |
10 |
11 |
16 |
19 |
20 |
21 |
22 |
23 |
24 |
25 |
28 |
31 |
32 |
33 |
34 |
35 |
36 |
37 |
38 |
39 |
40 |
41 |
42 |
43 |
44 |
45 |
--------------------------------------------------------------------------------
/README.md:
--------------------------------------------------------------------------------
1 | # content2picture
2 | 先看需求: 当内容已经超出了手机可显示的范围时,要截取这些所有的内容,从而生成所谓的”长截图”. 没什么难点,利用了webview的特点,和scrollview 的view的绘制,生成bitmap。
3 |
4 | 主要代码:
5 |
6 | //这是scrollview的
7 |
8 | public static Bitmap getBitmapByView(ScrollView scrollView) {
9 | int h = 0;
10 | Bitmap bitmap = null;
11 |
12 | for (int i = 0; i < scrollView.getChildCount(); i++) {
13 | h += scrollView.getChildAt(i).getHeight();
14 | scrollView.getChildAt(i).setBackgroundColor(
15 | Color.parseColor("#ffffff"));
16 | }
17 |
18 | bitmap = Bitmap.createBitmap(scrollView.getWidth(), h,
19 | Bitmap.Config.RGB_565);
20 | final Canvas canvas = new Canvas(bitmap);
21 | scrollView.draw(canvas);
22 | return bitmap;
23 | }
24 |
25 |
26 | /**
27 | * mScrollView
28 | *
29 | * @param context
30 | * @param scrollView
31 | */
32 | public static void scrollviewContent2Png(Context context,
33 | ScrollView scrollView) {
34 | Bitmap bmp = null;
35 | bmp = getBitmapByView(scrollView);
36 | saveBitmapToCamera(context, bmp, null);
37 | }
38 |
39 | //这是webview的,利用了webview的api
40 |
41 | private static Bitmap captureWebView(WebView webView) {
42 | Picture snapShot = webView.capturePicture();
43 | Bitmap bmp = Bitmap.createBitmap(snapShot.getWidth(),
44 | snapShot.getHeight(), Bitmap.Config.ARGB_8888);
45 | Canvas canvas = new Canvas(bmp);
46 | snapShot.draw(canvas);
47 | return bmp;
48 | }
49 |
50 | 代码粗略,只实现了功能部分,在图片生成的时候,未使用线程,如果还有其他比较好的方案,可以相互交流下
51 |
--------------------------------------------------------------------------------
/ic_launcher-web.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/jarlen/content2picture/adc43e7a62d0aedc6a4da3581fdeeb07f1448d6a/ic_launcher-web.png
--------------------------------------------------------------------------------
/libs/android-support-v4.jar:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/jarlen/content2picture/adc43e7a62d0aedc6a4da3581fdeeb07f1448d6a/libs/android-support-v4.jar
--------------------------------------------------------------------------------
/proguard-project.txt:
--------------------------------------------------------------------------------
1 | # To enable ProGuard in your project, edit project.properties
2 | # to define the proguard.config property as described in that file.
3 | #
4 | # Add project specific ProGuard rules here.
5 | # By default, the flags in this file are appended to flags specified
6 | # in ${sdk.dir}/tools/proguard/proguard-android.txt
7 | # You can edit the include path and order by changing the ProGuard
8 | # include property in project.properties.
9 | #
10 | # For more details, see
11 | # http://developer.android.com/guide/developing/tools/proguard.html
12 |
13 | # Add any project specific keep options here:
14 |
15 | # If your project uses WebView with JS, uncomment the following
16 | # and specify the fully qualified class name to the JavaScript interface
17 | # class:
18 | #-keepclassmembers class fqcn.of.javascript.interface.for.webview {
19 | # public *;
20 | #}
21 |
--------------------------------------------------------------------------------
/project.properties:
--------------------------------------------------------------------------------
1 | # This file is automatically generated by Android Tools.
2 | # Do not modify this file -- YOUR CHANGES WILL BE ERASED!
3 | #
4 | # This file must be checked in Version Control Systems.
5 | #
6 | # To customize properties used by the Ant build system edit
7 | # "ant.properties", and override values to adapt the script to your
8 | # project structure.
9 | #
10 | # To enable ProGuard to shrink and obfuscate your code, uncomment this (available properties: sdk.dir, user.home):
11 | #proguard.config=${sdk.dir}/tools/proguard/proguard-android.txt:proguard-project.txt
12 |
13 | # Project target.
14 | target=android-15
15 |
--------------------------------------------------------------------------------
/res/drawable-hdpi/ic_launcher.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/jarlen/content2picture/adc43e7a62d0aedc6a4da3581fdeeb07f1448d6a/res/drawable-hdpi/ic_launcher.png
--------------------------------------------------------------------------------
/res/drawable-mdpi/ic_launcher.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/jarlen/content2picture/adc43e7a62d0aedc6a4da3581fdeeb07f1448d6a/res/drawable-mdpi/ic_launcher.png
--------------------------------------------------------------------------------
/res/drawable-xhdpi/ic_launcher.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/jarlen/content2picture/adc43e7a62d0aedc6a4da3581fdeeb07f1448d6a/res/drawable-xhdpi/ic_launcher.png
--------------------------------------------------------------------------------
/res/drawable-xhdpi/pic.jpg:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/jarlen/content2picture/adc43e7a62d0aedc6a4da3581fdeeb07f1448d6a/res/drawable-xhdpi/pic.jpg
--------------------------------------------------------------------------------
/res/drawable-xxhdpi/ic_launcher.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/jarlen/content2picture/adc43e7a62d0aedc6a4da3581fdeeb07f1448d6a/res/drawable-xxhdpi/ic_launcher.png
--------------------------------------------------------------------------------
/res/layout/layout_main.xml:
--------------------------------------------------------------------------------
1 |
2 |
6 |
7 |
12 |
13 |
18 |
19 |
24 |
25 |
26 |
--------------------------------------------------------------------------------
/res/layout/layout_scrollview_test.xml:
--------------------------------------------------------------------------------
1 |
2 |
5 |
6 |
11 |
12 |
16 |
17 |
21 |
22 |
27 |
28 |
33 |
34 |
38 |
39 |
40 |
41 |
49 |
50 |
--------------------------------------------------------------------------------
/res/layout/layout_webview_test.xml:
--------------------------------------------------------------------------------
1 |
5 |
6 |
10 |
11 |
12 |
20 |
--------------------------------------------------------------------------------
/res/menu/content_to_picture.xml:
--------------------------------------------------------------------------------
1 |
12 |
--------------------------------------------------------------------------------
/res/values-v11/styles.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
7 |
10 |
11 |
12 |
--------------------------------------------------------------------------------
/res/values-v14/styles.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
8 |
11 |
12 |
13 |
--------------------------------------------------------------------------------
/res/values-w820dp/dimens.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
8 | 64dp
9 |
10 |
11 |
--------------------------------------------------------------------------------
/res/values/dimens.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 | 16dp
5 | 16dp
6 |
7 |
8 |
--------------------------------------------------------------------------------
/res/values/strings.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 | ContentToPicture
5 | Hello world!
6 | Settings
7 |
8 |
9 |
--------------------------------------------------------------------------------
/res/values/styles.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
7 |
14 |
15 |
16 |
19 |
20 |
21 |
--------------------------------------------------------------------------------
/src/com/jarlen/picture/ContentToPictureUtils.java:
--------------------------------------------------------------------------------
1 | package com.jarlen.picture;
2 |
3 | import java.io.BufferedReader;
4 | import java.io.File;
5 | import java.io.FileNotFoundException;
6 | import java.io.FileOutputStream;
7 | import java.io.IOException;
8 | import java.io.InputStream;
9 | import java.io.InputStreamReader;
10 | import java.io.UnsupportedEncodingException;
11 | import java.text.SimpleDateFormat;
12 | import java.util.Date;
13 |
14 | import android.annotation.SuppressLint;
15 | import android.content.Context;
16 | import android.content.Intent;
17 | import android.graphics.Bitmap;
18 | import android.graphics.Canvas;
19 | import android.graphics.Color;
20 | import android.graphics.Picture;
21 | import android.net.Uri;
22 | import android.os.Environment;
23 | import android.view.View;
24 | import android.webkit.WebSettings.LayoutAlgorithm;
25 | import android.webkit.WebView;
26 | import android.widget.ScrollView;
27 |
28 | @SuppressWarnings("deprecation")
29 | public class ContentToPictureUtils {
30 |
31 | public static String DCIMCamera_PATH = Environment
32 | .getExternalStorageDirectory() + "/DCIM/Camera/";
33 |
34 | /**
35 | * webView
36 | *
37 | * @param context
38 | * @param webView
39 | */
40 | public static void webviewContent2Png(Context context, WebView webView) {
41 | Bitmap bmp = null;
42 | bmp = captureWebView(webView);
43 | // new Thread(new WorkThread(bmp)).start();
44 | saveBitmapToCamera(context, bmp, null);
45 | }
46 |
47 | /**
48 | * mScrollView
49 | *
50 | * @param context
51 | * @param scrollView
52 | */
53 | public static void scrollviewContent2Png(Context context,
54 | ScrollView scrollView) {
55 | Bitmap bmp = null;
56 | bmp = getBitmapByView(scrollView);
57 | // new Thread(new WorkThread(bmp)).start();
58 | saveBitmapToCamera(context, bmp, null);
59 | }
60 |
61 | private static Bitmap captureWebView(WebView webView) {
62 | Picture snapShot = webView.capturePicture();
63 | Bitmap bmp = Bitmap.createBitmap(snapShot.getWidth(),
64 | snapShot.getHeight(), Bitmap.Config.ARGB_8888);
65 | Canvas canvas = new Canvas(bmp);
66 | snapShot.draw(canvas);
67 | return bmp;
68 | }
69 |
70 | public static Boolean saveBitmapToCamera(Context context, Bitmap bm,
71 | String name) {
72 |
73 | File file = null;
74 |
75 | if (name == null || name.equals("")) {
76 | SimpleDateFormat formatter = new SimpleDateFormat("yyyyMMddHHmmss");
77 | Date curDate = new Date(System.currentTimeMillis());
78 | name = formatter.format(curDate) + ".jpg";
79 | }
80 |
81 | file = new File(DCIMCamera_PATH, name);
82 | if (file.exists()) {
83 | file.delete();
84 | }
85 |
86 | try {
87 | FileOutputStream out = new FileOutputStream(file);
88 | bm.compress(Bitmap.CompressFormat.PNG, 100, out);
89 | out.flush();
90 | out.close();
91 | } catch (FileNotFoundException e) {
92 | e.printStackTrace();
93 | return false;
94 |
95 | } catch (IOException e) {
96 |
97 | e.printStackTrace();
98 | return false;
99 | }
100 |
101 | Intent intent = new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE);
102 | Uri uri = Uri.fromFile(file);
103 | intent.setData(uri);
104 | context.sendBroadcast(intent);
105 |
106 | return true;
107 | }
108 |
109 | public static Bitmap getBitmapByView(ScrollView scrollView) {
110 | int h = 0;
111 | Bitmap bitmap = null;
112 |
113 | for (int i = 0; i < scrollView.getChildCount(); i++) {
114 | h += scrollView.getChildAt(i).getHeight();
115 | scrollView.getChildAt(i).setBackgroundColor(
116 | Color.parseColor("#ffffff"));
117 | }
118 |
119 | bitmap = Bitmap.createBitmap(scrollView.getWidth(), h,
120 | Bitmap.Config.RGB_565);
121 | final Canvas canvas = new Canvas(bitmap);
122 | scrollView.draw(canvas);
123 | return bitmap;
124 | }
125 |
126 | }
127 |
--------------------------------------------------------------------------------
/src/com/jarlen/picture/ScrollViewToPictureActivity.java:
--------------------------------------------------------------------------------
1 | package com.jarlen.picture;
2 |
3 | import android.app.Activity;
4 | import android.os.Bundle;
5 | import android.view.View;
6 | import android.view.View.OnClickListener;
7 | import android.widget.Button;
8 | import android.widget.ScrollView;
9 | import android.widget.Toast;
10 |
11 | /**
12 | * 从ScrollView 和webview中截取图片
13 | *
14 | * @author jarlen
15 | *
16 | */
17 | public class ScrollViewToPictureActivity extends Activity implements
18 | OnClickListener {
19 |
20 | private Button testBtn;
21 |
22 | private ScrollView mScrollView;
23 |
24 | @Override
25 | protected void onCreate(Bundle savedInstanceState) {
26 | super.onCreate(savedInstanceState);
27 |
28 | setContentView(R.layout.layout_scrollview_test);
29 | mScrollView = (ScrollView) findViewById(R.id.scrollview_sv);
30 |
31 | testBtn = (Button) findViewById(R.id.testBtn);
32 | testBtn.setOnClickListener(this);
33 |
34 | }
35 |
36 | @Override
37 | public void onClick(View view) {
38 |
39 | if (view.getId() == R.id.testBtn) {
40 |
41 | ContentToPictureUtils.scrollviewContent2Png(this, mScrollView);
42 | Toast.makeText(this, "已保存至相册", Toast.LENGTH_SHORT).show();
43 | }
44 |
45 | }
46 |
47 | }
48 |
--------------------------------------------------------------------------------
/src/com/jarlen/picture/TestMainActivity.java:
--------------------------------------------------------------------------------
1 | package com.jarlen.picture;
2 |
3 | import android.app.Activity;
4 | import android.content.Intent;
5 | import android.os.Bundle;
6 | import android.view.View;
7 | import android.view.View.OnClickListener;
8 |
9 | public class TestMainActivity extends Activity implements OnClickListener {
10 |
11 | @Override
12 | protected void onCreate(Bundle savedInstanceState) {
13 | // TODO Auto-generated method stub
14 | super.onCreate(savedInstanceState);
15 |
16 | setContentView(R.layout.layout_main);
17 |
18 | findViewById(R.id.scroll_test).setOnClickListener(this);
19 | findViewById(R.id.webview_test).setOnClickListener(this);
20 | }
21 |
22 | @Override
23 | public void onClick(View view) {
24 | switch (view.getId()) {
25 | case R.id.scroll_test:
26 |
27 | Intent ScrollViewIntent = new Intent(this,
28 | ScrollViewToPictureActivity.class);
29 |
30 | this.startActivity(ScrollViewIntent);
31 |
32 | break;
33 | case R.id.webview_test:
34 |
35 | Intent WebviewIntent = new Intent(this,
36 | WebviewToPictureActivity.class);
37 |
38 | this.startActivity(WebviewIntent);
39 |
40 | break;
41 |
42 | default:
43 | break;
44 | }
45 |
46 | }
47 |
48 | }
49 |
--------------------------------------------------------------------------------
/src/com/jarlen/picture/WebviewToPictureActivity.java:
--------------------------------------------------------------------------------
1 | package com.jarlen.picture;
2 |
3 | import android.app.Activity;
4 | import android.app.ProgressDialog;
5 | import android.os.Bundle;
6 | import android.view.View;
7 | import android.view.View.OnClickListener;
8 | import android.webkit.WebView;
9 | import android.webkit.WebViewClient;
10 | import android.widget.Button;
11 | import android.widget.Toast;
12 |
13 | /**
14 | * 从ScrollView 和webview中截取图片
15 | *
16 | * @author jarlen
17 | *
18 | */
19 | public class WebviewToPictureActivity extends Activity implements
20 | OnClickListener {
21 |
22 | private Button testBtn;
23 |
24 | private WebView mWebView;
25 |
26 | private ProgressDialog mProgressDialog;
27 |
28 | @Override
29 | protected void onCreate(Bundle savedInstanceState) {
30 | super.onCreate(savedInstanceState);
31 |
32 | setContentView(R.layout.layout_webview_test);
33 |
34 | mWebView = (WebView) findViewById(R.id.webView);
35 | mWebView.setDrawingCacheEnabled(true);//
36 |
37 | mProgressDialog = new ProgressDialog(this);
38 |
39 | mWebView.loadUrl("http://blog.csdn.net/jarlen/article/details/46931093");
40 | mWebView.setVisibility(View.GONE);
41 |
42 | mProgressDialog.show();
43 |
44 | mWebView.setWebViewClient(new WebViewClient() {
45 |
46 | @Override
47 | public boolean shouldOverrideUrlLoading(WebView view, String url) {
48 | // TODO Auto-generated method stub
49 | mWebView.loadUrl(url);
50 | return true;
51 | }
52 |
53 | @Override
54 | public void onPageFinished(WebView view, String url) {
55 | // TODO Auto-generated method stub
56 | mWebView.setVisibility(View.VISIBLE);
57 | mProgressDialog.dismiss();
58 | super.onPageFinished(view, url);
59 | }
60 |
61 | });
62 |
63 | testBtn = (Button) findViewById(R.id.testBtn);
64 | testBtn.setOnClickListener(this);
65 |
66 | }
67 |
68 | @Override
69 | public void onClick(View view) {
70 |
71 | if (view.getId() == R.id.testBtn) {
72 | ContentToPictureUtils.webviewContent2Png(this, mWebView);
73 | Toast.makeText(this, "已保存至相册", Toast.LENGTH_SHORT).show();
74 | }
75 |
76 | }
77 |
78 | }
79 |
--------------------------------------------------------------------------------