Java Code Examples for android.view.View.buildDrawingCache()

The following are Jave code examples for showing how to use buildDrawingCache() of the android.view.View class. You can vote up the examples you like. Your votes will be used in our system to get more good examples.
Example 1
Project: AndroidDevSamples   File: ScreenUtil.java   Source Code and License Vote up 6 votes
/**
 * 获取当前屏幕截图,不包含状态栏
 */
public static Bitmap snapShotWithoutStatusBar(Activity activity) {
    View view = activity.getWindow().getDecorView();
    view.setDrawingCacheEnabled(true);
    view.buildDrawingCache();
    Bitmap bmp = view.getDrawingCache();
    Rect frame = new Rect();
    activity.getWindow().getDecorView().getWindowVisibleDisplayFrame(frame);
    int statusBarHeight = frame.top;

    int width = getScreenWidth(activity);
    int height = getScreenHeight(activity);
    Bitmap bp = null;
    bp = Bitmap.createBitmap(bmp, 0, statusBarHeight, width, height
            - statusBarHeight);
    view.destroyDrawingCache();
    return bp;
}
 
Example 2
Project: baselibrary-master   File: ScreenUtils.java   Source Code and License Vote up 6 votes
/**
 * 获取当前屏幕截图,不包含状态栏
 * 
 * @param activity
 * @return
 */
public static Bitmap snapShotWithoutStatusBar(Activity activity)
{
	View view = activity.getWindow().getDecorView();
	view.setDrawingCacheEnabled(true);
	view.buildDrawingCache();
	Bitmap bmp = view.getDrawingCache();
	Rect frame = new Rect();
	activity.getWindow().getDecorView().getWindowVisibleDisplayFrame(frame);
	int statusBarHeight = frame.top;

	int width = getScreenWidth(activity);
	int height = getScreenHeight(activity);
	Bitmap bp = null;
	bp = Bitmap.createBitmap(bmp, 0, statusBarHeight, width, height
			- statusBarHeight);
	view.destroyDrawingCache();
	return bp;

}
 
Example 3
Project: cniao5   File: ScreenUtils.java   Source Code and License Vote up 6 votes
/**
 * 获取当前屏幕截图,不包含状态栏
 *
 * @param activity
 * @return
 */
public static Bitmap snapShotWithoutStatusBar(Activity activity) {
    View view = activity.getWindow().getDecorView();
    view.setDrawingCacheEnabled(true);
    view.buildDrawingCache();
    Bitmap bmp = view.getDrawingCache();
    Rect frame = new Rect();
    activity.getWindow().getDecorView().getWindowVisibleDisplayFrame(frame);
    int statusBarHeight = frame.top;

    int width = getScreenWidth(activity);
    int height = getScreenHeight(activity);
    Bitmap bp = null;
    bp = Bitmap.createBitmap(bmp, 0, statusBarHeight, width, height
            - statusBarHeight);
    view.destroyDrawingCache();
    return bp;
}
 
Example 4
Project: TextEmoji   File: ShareActivity.java   Source Code and License Vote up 6 votes
private Bitmap getShareBitmap(String text, int textSize) {
    View view = getLayoutInflater().inflate(R.layout.layout_text_card, (ViewGroup) findViewById(android.R.id.content), false);
    TextView textView = (TextView) view.findViewById(R.id.text);
    textView.setText(text);
    textView.setTextSize(textSize);

    view.setDrawingCacheEnabled(true);

    view.measure(View.MeasureSpec.makeMeasureSpec(0, View.MeasureSpec.UNSPECIFIED),
            View.MeasureSpec.makeMeasureSpec(0, View.MeasureSpec.UNSPECIFIED));
    view.layout(0, 0, view.getMeasuredWidth(), view.getMeasuredHeight());

    view.buildDrawingCache(true);
    Bitmap b = Bitmap.createBitmap(view.getDrawingCache());
    view.setDrawingCacheEnabled(false); // clear drawing cache

    return b;
}
 
Example 5
Project: boohee_v5.6   File: BitmapUtil.java   Source Code and License Vote up 6 votes
@SuppressLint({"NewApi"})
public static Bitmap takeScreenShot(Activity pActivity) {
    View view = pActivity.getWindow().getDecorView();
    view.setDrawingCacheEnabled(true);
    view.buildDrawingCache();
    Bitmap bitmap = view.getDrawingCache();
    Rect frame = new Rect();
    view.getWindowVisibleDisplayFrame(frame);
    int stautsHeight = frame.top;
    Point size = new Point();
    Display display = pActivity.getWindowManager().getDefaultDisplay();
    if (VERSION.SDK_INT < 13) {
        size.set(display.getWidth(), display.getHeight());
    } else {
        pActivity.getWindowManager().getDefaultDisplay().getSize(size);
    }
    return Bitmap.createBitmap(bitmap, 0, stautsHeight, size.x, size.y - stautsHeight);
}
 
Example 6
Project: AndroidUtilCode-master   File: ScreenUtils.java   Source Code and License Vote up 5 votes
/**
 * 获取当前屏幕截图,包含状态栏
 *
 * @param activity activity
 * @return Bitmap
 */
public static Bitmap captureWithStatusBar(Activity activity) {
    View view = activity.getWindow().getDecorView();
    view.setDrawingCacheEnabled(true);
    view.buildDrawingCache();
    Bitmap bmp = view.getDrawingCache();
    DisplayMetrics dm = new DisplayMetrics();
    activity.getWindowManager().getDefaultDisplay().getMetrics(dm);
    Bitmap ret = Bitmap.createBitmap(bmp, 0, 0, dm.widthPixels, dm.heightPixels);
    view.destroyDrawingCache();
    return ret;
}
 
Example 7
Project: GitHub   File: ConvertUtils.java   Source Code and License Vote up 5 votes
/**
 * 把view转化为bitmap(截图)
 * 参见:http://www.cnblogs.com/lee0oo0/p/3355468.html
 */
public static Bitmap toBitmap(View view) {
    int width = view.getWidth();
    int height = view.getHeight();
    if (view instanceof ListView) {
        height = 0;
        // 获取listView实际高度
        ListView listView = (ListView) view;
        for (int i = 0; i < listView.getChildCount(); i++) {
            height += listView.getChildAt(i).getHeight();
        }
    } else if (view instanceof ScrollView) {
        height = 0;
        // 获取scrollView实际高度
        ScrollView scrollView = (ScrollView) view;
        for (int i = 0; i < scrollView.getChildCount(); i++) {
            height += scrollView.getChildAt(i).getHeight();
        }
    }
    view.setDrawingCacheEnabled(true);
    view.clearFocus();
    view.setPressed(false);
    boolean willNotCache = view.willNotCacheDrawing();
    view.setWillNotCacheDrawing(false);
    // Reset the drawing cache background color to fully transparent for the duration of this operation
    int color = view.getDrawingCacheBackgroundColor();
    view.setDrawingCacheBackgroundColor(Color.WHITE);//截图去黑色背景(透明像素)
    if (color != Color.WHITE) {
        view.destroyDrawingCache();
    }
    view.buildDrawingCache();
    Bitmap cacheBitmap = view.getDrawingCache();
    if (cacheBitmap == null) {
        return null;
    }
    Bitmap bitmap = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888);
    Canvas canvas = new Canvas(bitmap);
    canvas.drawBitmap(cacheBitmap, 0, 0, null);
    canvas.save(Canvas.ALL_SAVE_FLAG);
    canvas.restore();
    if (!bitmap.isRecycled()) {
        LogUtils.verbose("recycle bitmap: " + bitmap.toString());
        bitmap.recycle();
    }
    // Restore the view
    view.destroyDrawingCache();
    view.setWillNotCacheDrawing(willNotCache);
    view.setDrawingCacheBackgroundColor(color);
    return bitmap;
}
 
Example 8
Project: EmotionApp   File: ScreenUtils.java   Source Code and License Vote up 5 votes
/**
 * 获取当前屏幕截图,包含状态栏
 *
 * @param activity
 * @return
 */
public static Bitmap snapShotWithStatusBar(Activity activity)
{
    View view = activity.getWindow().getDecorView();
    view.setDrawingCacheEnabled(true);
    view.buildDrawingCache();
    Bitmap bmp = view.getDrawingCache();
    int width = getScreenWidth(activity);
    int height = getScreenHeight(activity);
    Bitmap bp = null;
    bp = Bitmap.createBitmap(bmp, 0, 0, width, height);
    view.destroyDrawingCache();
    return bp;

}
 
Example 9
Project: cniao5   File: ScreenUtils.java   Source Code and License Vote up 5 votes
/**
 * 获取当前屏幕截图,包含状态栏
 *
 * @param activity
 * @return
 */
public static Bitmap snapShotWithStatusBar(Activity activity) {
    View view = activity.getWindow().getDecorView();
    view.setDrawingCacheEnabled(true);
    view.buildDrawingCache();
    Bitmap bmp = view.getDrawingCache();
    int width = getScreenWidth(activity);
    int height = getScreenHeight(activity);
    Bitmap bp = null;
    bp = Bitmap.createBitmap(bmp, 0, 0, width, height);
    view.destroyDrawingCache();
    return bp;

}
 
Example 10
Project: baselibrary-master   File: ScreenUtils.java   Source Code and License Vote up 5 votes
/**
 * 获取当前屏幕截图,包含状态栏
 * 
 * @param activity
 * @return
 */
public static Bitmap snapShotWithStatusBar(Activity activity)
{
	View view = activity.getWindow().getDecorView();
	view.setDrawingCacheEnabled(true);
	view.buildDrawingCache();
	Bitmap bmp = view.getDrawingCache();
	int width = getScreenWidth(activity);
	int height = getScreenHeight(activity);
	Bitmap bp = null;
	bp = Bitmap.createBitmap(bmp, 0, 0, width, height);
	view.destroyDrawingCache();
	return bp;

}
 
Example 11
Project: newIPlay   File: ScreenUtils.java   Source Code and License Vote up 5 votes
/**
 * 获取当前屏幕截图,包含状态栏
 *
 * @param activity activity
 * @return Bitmap
 */
public static Bitmap captureWithStatusBar(Activity activity) {
    View view = activity.getWindow().getDecorView();
    view.setDrawingCacheEnabled(true);
    view.buildDrawingCache();
    Bitmap bmp = view.getDrawingCache();
    DisplayMetrics dm = new DisplayMetrics();
    activity.getWindowManager().getDefaultDisplay().getMetrics(dm);
    Bitmap ret = Bitmap.createBitmap(bmp, 0, 0, dm.widthPixels, dm.heightPixels);
    view.destroyDrawingCache();
    return ret;
}
 
Example 12
Project: DailyZhiHu   File: ScreenUtils.java   Source Code and License Vote up 5 votes
/**
 * 获取当前屏幕截图,包含状态栏
 * 
 * @param activity
 * @return
 */
public static Bitmap snapShotWithStatusBar(Activity activity)
{
	View view = activity.getWindow().getDecorView();
	view.setDrawingCacheEnabled(true);
	view.buildDrawingCache();
	Bitmap bmp = view.getDrawingCache();
	int width = getScreenWidth(activity);
	int height = getScreenHeight(activity);
	Bitmap bp = null;
	bp = Bitmap.createBitmap(bmp, 0, 0, width, height);
	view.destroyDrawingCache();
	return bp;

}
 
Example 13
Project: LJFramework   File: ScreenUtils.java   Source Code and License Vote up 5 votes
/**
 * 获取当前屏幕截图,不包含状态栏
 *
 * @param activity activity
 * @return Bitmap
 */
public static Bitmap captureWithoutStatusBar(Activity activity) {
    View view = activity.getWindow().getDecorView();
    view.setDrawingCacheEnabled(true);
    view.buildDrawingCache();
    Bitmap bmp = view.getDrawingCache();
    int statusBarHeight = BarUtils.getStatusBarHeight(activity);
    DisplayMetrics dm = new DisplayMetrics();
    activity.getWindowManager().getDefaultDisplay().getMetrics(dm);
    Bitmap ret = Bitmap
            .createBitmap(bmp, 0, statusBarHeight, dm.widthPixels,
                    dm.heightPixels - statusBarHeight);
    view.destroyDrawingCache();
    return ret;
}
 
Example 14
Project: Huochexing12306   File: ShareUtil.java   Source Code and License Vote up 5 votes
/**
 * 保存截图到sd卡 并返回截图路径
 * @param activity
 * @return
 */
public String getScreenShut(Activity activity){
	View view = activity.getWindow().getDecorView();
	view.setDrawingCacheEnabled(true);
	view.buildDrawingCache();
	Bitmap b1 = view.getDrawingCache();
	// 获取状态栏高度
	Rect frame = new Rect();
	activity.getWindow().getDecorView().getWindowVisibleDisplayFrame(frame);
	int statusBarHeight = frame.top;
	// 获取屏幕长和高
	int width = activity.getWindowManager().getDefaultDisplay().getWidth();
	int height = activity.getWindowManager().getDefaultDisplay()
			.getHeight();
	// 去掉标题栏
	Bitmap b = Bitmap.createBitmap(b1, 0, statusBarHeight, width, height
			- statusBarHeight);
	view.destroyDrawingCache();
	//保存到sd卡
	String  sdCardRoot = Environment.getExternalStorageDirectory().getAbsolutePath();
	String fileName = "shareIMG"+new Date().getTime()+".png";
	String path = sdCardRoot+"/HuoCheXing/shareImage/"+fileName;
	if(saveBitmapToSDCard(b,path)){
		return path;
	}
	return null;
}
 
Example 15
Project: WechatChatroomHelper   File: ScreenUtils.java   Source Code and License Vote up 5 votes
/**
 * 获取当前屏幕截图,包含状态栏
 *
 * @param activity
 * @return
 */
public static Bitmap snapShotWithStatusBar(Activity activity) {
    View view = activity.getWindow().getDecorView();
    view.setDrawingCacheEnabled(true);
    view.buildDrawingCache();
    Bitmap bmp = view.getDrawingCache();
    int width = getScreenWidth(activity);
    int height = getScreenHeight(activity);
    Bitmap bp = null;
    bp = Bitmap.createBitmap(bmp, 0, 0, width, height);
    view.destroyDrawingCache();
    return bp;
}
 
Example 16
Project: FilterBar   File: UIUtil.java   Source Code and License Vote up 5 votes
public static Bitmap convertViewToBitmap(View view) {
    if (view.getLayoutParams() == null) {
        view.setLayoutParams(new ViewGroup.LayoutParams(-2, -2));
    }

    measureView(view);
    view.layout(0, 0, view.getMeasuredWidth(), view.getMeasuredHeight());
    view.destroyDrawingCache();
    view.buildDrawingCache();
    return view.getDrawingCache();
}
 
Example 17
Project: letv   File: FileUtils.java   Source Code and License Vote up 5 votes
private Bitmap takeScreenShot(Activity context) {
    View view = context.getWindow().getDecorView();
    view.setDrawingCacheEnabled(true);
    view.buildDrawingCache();
    Bitmap screenBitmap = view.getDrawingCache();
    view.destroyDrawingCache();
    view.setDrawingCacheEnabled(false);
    return screenBitmap;
}
 
Example 18
Project: MyFire   File: ChangeModeController.java   Source Code and License Vote up 5 votes
/**
 * 获取一个 View 的缓存视图
 *
 * @param view
 * @return
 */
private 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;
}
 
Example 19
Project: TitleLayout   File: ScreenUtil.java   Source Code and License Vote up 5 votes
/**
 * 获取当前屏幕截图,不包含状态栏
 *
 * @param activity activity
 * @return Bitmap
 */
public static Bitmap captureWithoutStatusBar(Activity activity) {
    View view = activity.getWindow().getDecorView();
    view.setDrawingCacheEnabled(true);
    view.buildDrawingCache();
    Bitmap bmp = view.getDrawingCache();
    int statusBarHeight = getStatusBarHeight(activity);//获取状态栏高度
    DisplayMetrics dm = new DisplayMetrics();
    activity.getWindowManager().getDefaultDisplay().getMetrics(dm);
    Bitmap ret = Bitmap.createBitmap(bmp, 0, statusBarHeight, dm.widthPixels, dm.heightPixels - statusBarHeight);
    view.destroyDrawingCache();
    return ret;
}
 
Example 20
Project: AndroidBasicLibs   File: ViewUtils.java   Source Code and License Vote up 4 votes
public static Bitmap captureView(View v) {
    v.setDrawingCacheEnabled(true);
    v.buildDrawingCache();
    return v.getDrawingCache();
}