모바일 개발자 LasseLindh

[안드로이드/Android] 앱 화면캡쳐 소스 본문

Android

[안드로이드/Android] 앱 화면캡쳐 소스

사용자 라쎄린드 2017.07.25 10:17

안드로이드 앱을 개발할때 가끔 앱내화면을 캡쳐하는 기능이 필요할 때가 있습니다.


일반적으로 앱내의 특정뷰를 캡쳐하는 소스는 많이 널려있는것 같네요


카카오톡의 경우 특정 인덱스를 지정해서 리사이클러뷰를 캡쳐하는 기능이 있어서 


공부할 겸 여러가지 캡쳐가 가능한 유틸을 만들어보았습니다.


// 특정 뷰만 캡쳐시

CaptureUtil.captureView(View View);


// 액티비티 전체화면을 캡쳐

CaptureUtil.captureActivity(Activity context);


// 리사이클러뷰의 전체 스크롤을 캡쳐

CaptureUtil.captureRecyclerView(RecyclerView view, int bgColor);


// 리사이클러뷰의 특정 인덱스 범위를 캡쳐

CaptureUtil.captureMyRecyclerView(RecyclerView view, int bgColor, int startPosition, int endPosition);




import android.app.Activity;
import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.os.Environment;
import android.support.v7.widget.RecyclerView;
import android.util.LruCache;
import android.view.View;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;

/**
* Created by lasselindh on 2017. 6. 22..
*/

public class CaptureUtil {
// 캡쳐가 저장될 외부 저장소
private static final String CAPTURE_PATH = "/CAPTURE_TEST";

/**
* 특정 뷰만 캡쳐
* @param View
*/
public static void captureView(View View) {
View.buildDrawingCache();
Bitmap captureView = View.getDrawingCache();
FileOutputStream fos;

String strFolderPath = Environment.getExternalStorageDirectory().getAbsolutePath() + CAPTURE_PATH;
File folder = new File(strFolderPath);
if(!folder.exists()) {
folder.mkdirs();
}

String strFilePath = strFolderPath + "/" + System.currentTimeMillis() + ".png";
File fileCacheItem = new File(strFilePath);

try {
fos = new FileOutputStream(fileCacheItem);
captureView.compress(Bitmap.CompressFormat.PNG, 100, fos);
} catch (FileNotFoundException e) {
e.printStackTrace();
}
}

/**
* 액티비티 전체 캡쳐
* @param context
*/
public static void captureActivity(Activity context) {
if(context == null) return;
View root = context.getWindow().getDecorView().getRootView();
root.setDrawingCacheEnabled(true);
root.buildDrawingCache();
// 루트뷰의 캐시를 가져옴
Bitmap screenshot = root.getDrawingCache();

// get view coordinates
int[] location = new int[2];
root.getLocationInWindow(location);

// 이미지를 자를 수 있으나 전체 화면을 캡쳐 하도록 함
Bitmap bmp = Bitmap.createBitmap(screenshot, location[0], location[1], root.getWidth(), root.getHeight(), null, false);
String strFolderPath = Environment.getExternalStorageDirectory().getAbsolutePath() + CAPTURE_PATH;
File folder = new File(strFolderPath);
if(!folder.exists()) {
folder.mkdirs();
}

String strFilePath = strFolderPath + "/" + System.currentTimeMillis() + ".png";
File fileCacheItem = new File(strFilePath);
OutputStream out = null;
try {
fileCacheItem.createNewFile();
out = new FileOutputStream(fileCacheItem);
bmp.compress(Bitmap.CompressFormat.PNG, 100, out);
}
catch (Exception e) {
e.printStackTrace();
}
finally {
try {
out.close();
}
catch (IOException e) {
e.printStackTrace();
}
}
}

/**
* 리사이클러뷰 전체스크롤 캡쳐
* @param view
* @param bgColor
*/
public static void captureRecyclerView(RecyclerView view, int bgColor) {
if(view == null) return;
captureMyRecyclerView(view, bgColor, 0, view.getAdapter().getItemCount() - 1);
}

/**
* 리사이클러뷰 범위 캡쳐
* @param view
* @param bgColor
* @param startPosition
* @param endPosition
*/
public static void captureRecyclerView(RecyclerView view, int bgColor, int startPosition, int endPosition) {
if(view == null) return;
captureMyRecyclerView(view, bgColor, startPosition, endPosition);
}


private static void captureMyRecyclerView(RecyclerView view, int bgColor, int startPosition, int endPosition) {
RecyclerView.Adapter adapter = view.getAdapter();
Bitmap bigBitmap = null;
if (adapter != null) {

if ( startPosition > endPosition ){
int tmp = endPosition;
endPosition = startPosition;
startPosition = tmp;
}

int size = endPosition - startPosition;
int height = 0;
Paint paint = new Paint();
int iHeight = 0;
final int maxMemory = (int) (Runtime.getRuntime().maxMemory() / 1024);

final int cacheSize = maxMemory / 8;
LruCache<String, Bitmap> bitmaCache = new LruCache<>(cacheSize);
for (int i = startPosition; i < endPosition + 1; i++) {
RecyclerView.ViewHolder holder = adapter.createViewHolder(view, adapter.getItemViewType(i));
adapter.onBindViewHolder(holder, i);
holder.itemView.measure(View.MeasureSpec.makeMeasureSpec(view.getWidth(), View.MeasureSpec.EXACTLY),
View.MeasureSpec.makeMeasureSpec(0, View.MeasureSpec.UNSPECIFIED));
holder.itemView.layout(0, 0, holder.itemView.getMeasuredWidth(), holder.itemView.getMeasuredHeight());
holder.itemView.setDrawingCacheEnabled(true);
holder.itemView.buildDrawingCache();
if(bgColor != 0)
holder.itemView.setBackgroundColor(bgColor);
Bitmap drawingCache = holder.itemView.getDrawingCache();
if (drawingCache != null) {

bitmaCache.put(String.valueOf(i), drawingCache);
}

height += holder.itemView.getMeasuredHeight();
}

bigBitmap = Bitmap.createBitmap(view.getMeasuredWidth(), height, Bitmap.Config.ARGB_8888);
Canvas bigCanvas = new Canvas(bigBitmap);
bigCanvas.drawColor(Color.WHITE);

for (int i = 0; i < size + 1; i++) {
Bitmap bitmap = bitmaCache.get(String.valueOf(i));
bigCanvas.drawBitmap(bitmap, 0f, iHeight, paint);
iHeight += bitmap.getHeight();
bitmap.recycle();
}

}

String strFolderPath = Environment.getExternalStorageDirectory().getAbsolutePath() + CAPTURE_PATH;
File folder = new File(strFolderPath);
if(!folder.exists()) {
folder.mkdirs();
}

String strFilePath = strFolderPath + "/" + System.currentTimeMillis() + ".png";
File fileCacheItem = new File(strFilePath);
OutputStream out = null;
try {
fileCacheItem.createNewFile();
out = new FileOutputStream(fileCacheItem);
bigBitmap.compress(Bitmap.CompressFormat.PNG, 100, out);
}
catch (Exception e) {
e.printStackTrace();
}
finally {
try {
out.close();
}
catch (IOException e) {
e.printStackTrace();
}
}
}
}


0 Comments
댓글쓰기 폼