public static Bitmap takeScreenShot(Activity activity, View viewFragment) { // View是你需要截图的View View view = activity.getWindow().getDecorView(); if (view == null) { System.out.println("view is null"); } else { System.out.println("view:" + view.toString()); } // View view = viewFragment; 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(); return b; }
public static Bitmap screenshot(View view, boolean hasstatubar) { if (view == null) { return null; } view.setDrawingCacheEnabled(true); view.buildDrawingCache(); Bitmap cachebmp = view.getDrawingCache(); Rect rect = new Rect(); view.getWindowVisibleDisplayFrame(rect); int statusBarHeights = rect.top; DisplayMetrics m = view.getResources().getDisplayMetrics(); // 获取屏幕宽和高 int widths = m.widthPixels; int heights = m.heightPixels; Bitmap bmp = null; if (cachebmp != null) { if (hasstatubar) { bmp = Bitmap.createBitmap(cachebmp, 0, 0, widths, heights); } else { bmp = Bitmap.createBitmap(cachebmp, 0, statusBarHeights, widths, heights - statusBarHeights); } } view.destroyDrawingCache(); return bmp; }
/** * 截屏 * * @param activity * @return */ public static Bitmap getScreenViewBitmap(Activity activity) { View view = activity.getWindow().getDecorView(); view.setDrawingCacheEnabled(true); view.buildDrawingCache(); Bitmap bitmap = view.getDrawingCache(); return bitmap; }
/** * This simple implementation creates a Bitmap copy of the list item currently shown at ListView * <code>position</code>. */ @Override public View onCreateFloatView(int position) { // Guaranteed that this will not be null? I think so. Nope, got // a NullPointerException once... View v = mListView.getChildAt( position + mListView.getHeaderViewsCount() - mListView.getFirstVisiblePosition()); if (v == null) { return null; } v.setPressed(false); // Create a copy of the drawing cache so that it does not get // recycled by the framework when the list tries to clean up memory // v.setDrawingCacheQuality(View.DRAWING_CACHE_QUALITY_HIGH); v.setDrawingCacheEnabled(true); mFloatBitmap = Bitmap.createBitmap(v.getDrawingCache()); v.setDrawingCacheEnabled(false); if (mImageView == null) { mImageView = new ImageView(mListView.getContext()); } mImageView.setBackgroundColor(mFloatBGColor); mImageView.setPadding(0, 0, 0, 0); mImageView.setImageBitmap(mFloatBitmap); mImageView.setLayoutParams(new ViewGroup.LayoutParams(v.getWidth(), v.getHeight())); return mImageView; }
private String takeScreenshot(String imageName) { // image naming and path to include sd card appending name you choose // for file String mParentFolder = Environment.getExternalStorageDirectory().toString() + "/" + SHARE_PATH; File mParentDir = new File(mParentFolder); mParentDir.mkdirs(); String mPath = mParentFolder + "/" + imageName + ".jpg"; // create bitmap screen capture Bitmap bitmap; View v1 = findViewById(R.id.awesomepager); v1.setDrawingCacheEnabled(true); bitmap = Bitmap.createBitmap(v1.getDrawingCache()); v1.setDrawingCacheEnabled(false); OutputStream fout = null; File imageFile = new File(mPath); try { fout = new FileOutputStream(imageFile); bitmap.compress(Bitmap.CompressFormat.JPEG, 97, fout); fout.flush(); fout.close(); } catch (Exception e) { mPath = null; } return mPath; }
public void captureScreen() { int topHeight = 300, bottomHeight = 250; View v = getWindow().getDecorView().getRootView(); v.setDrawingCacheEnabled(true); Bitmap b = Bitmap.createBitmap(v.getDrawingCache()); screenShot = Bitmap.createBitmap( b, 0, bottomHeight, b.getWidth(), b.getHeight() - (bottomHeight + topHeight)); v.setDrawingCacheEnabled(false); try { FileOutputStream fos = this.openFileOutput("screenshot", Context.MODE_PRIVATE); screenShot.compress(Bitmap.CompressFormat.PNG, 100, fos); fos.flush(); fos.close(); Intent intent = new Intent(this, EditFinish.class); intent.putExtra("image", "screenshot"); startActivity(intent); } catch (FileNotFoundException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } }
private Uri getViewImage(View view) { try { File outputDir = getActivity().getCacheDir(); // context being the Activity pointer File outputFile = File.createTempFile("screen", ".png", outputDir); view.setDrawingCacheEnabled(true); Bitmap bitmap = Bitmap.createBitmap(view.getDrawingCache()); try { view.setDrawingCacheEnabled(false); FileOutputStream out = new FileOutputStream(outputFile); try { bitmap.compress(Bitmap.CompressFormat.JPEG, 90, out); out.flush(); return Uri.fromFile(outputFile); } finally { IOUtils.closeQuietly(out); } } finally { bitmap.recycle(); } } catch (IOException e) { Services.Log.error(e); return null; } }
public static Bitmap getDrawingCache(View view) { view.setDrawingCacheEnabled(true); Bitmap bmap = view.getDrawingCache(); Bitmap snapshot = Bitmap.createBitmap(bmap, 0, 0, bmap.getWidth(), bmap.getHeight(), null, true); view.setDrawingCacheEnabled(false); return snapshot; }
/** * 获取点击的Item的对应View, * * @param view * @return */ private ImageView getView(View view) { view.destroyDrawingCache(); view.setDrawingCacheEnabled(true); Bitmap cache = Bitmap.createBitmap(view.getDrawingCache()); view.setDrawingCacheEnabled(false); ImageView iv = new ImageView(this); iv.setImageBitmap(cache); return iv; }
public void thumbNailScaleAnimation(View view) { view.setDrawingCacheEnabled(true); view.setPressed(false); view.refreshDrawableState(); Bitmap bitmap = view.getDrawingCache(); ActivityOptions opts = ActivityOptions.makeThumbnailScaleUpAnimation(view, bitmap, 0, 0); // Request the activity be started, using the custom animation options. startActivity(new Intent(MainActivity.this, AnimationActivity.class), opts.toBundle()); view.setDrawingCacheEnabled(false); }
/** * 截屏 * * @author [email protected] 2013-10-26 下午2:39:01 * @param activity * @return Bitmap */ public static Bitmap shot(Activity activity) { View view = activity.getWindow().getDecorView(); Display display = activity.getWindowManager().getDefaultDisplay(); view.layout(0, 0, display.getWidth(), display.getHeight()); // 允许当前窗口保存缓存信息,这样getDrawingCache()方法才会返回一个Bitmap view.setDrawingCacheEnabled(true); Bitmap bmp = Bitmap.createBitmap(view.getDrawingCache()); view.setDrawingCacheEnabled(false); return bmp; }
/** * 将定义的view装换成 bitmap格式 * * @param view * @return */ public static Bitmap convertView2Bitmap(View view) { view.setDrawingCacheEnabled(true); Bitmap bitmap = Bitmap.createBitmap(view.getDrawingCache()); view.setDrawingCacheEnabled(false); return bitmap; /* * view.measure(MeasureSpec.makeMeasureSpec(0, MeasureSpec.UNSPECIFIED), * MeasureSpec.makeMeasureSpec(0, MeasureSpec.UNSPECIFIED)); * view.layout(0, 0, view.getMeasuredWidth(), view.getMeasuredHeight()); * view.buildDrawingCache(); Bitmap bitmap = view.getDrawingCache(); * //return bitmap; */ // return BitmapUtils.getTransparentBitmap(bitmap,80); }
public static Bitmap capture( View view, float width, float height, boolean scroll, Bitmap.Config config) { if (!view.isDrawingCacheEnabled()) view.setDrawingCacheEnabled(true); Bitmap bitmap = Bitmap.createBitmap((int) width, (int) height, config); bitmap.eraseColor(Color.WHITE); Canvas canvas = new Canvas(bitmap); int left = view.getLeft(); int top = view.getTop(); if (scroll) { left = view.getScrollX(); top = view.getScrollY(); } int status = canvas.save(); canvas.translate(-left, -top); float scale = width / view.getWidth(); canvas.scale(scale, scale, left, top); view.draw(canvas); canvas.restoreToCount(status); Paint alphaPaint = new Paint(); alphaPaint.setColor(Color.TRANSPARENT); canvas.drawRect(0f, 0f, 1f, height, alphaPaint); canvas.drawRect(width - 1f, 0f, width, height, alphaPaint); canvas.drawRect(0f, 0f, width, 1f, alphaPaint); canvas.drawRect(0f, height - 1f, width, height, alphaPaint); canvas.setBitmap(null); return bitmap; }
public CustomDragShadowBuilder(View v) { super(v); v.setDrawingCacheEnabled(true); mBitmapDrawable = new BitmapDrawable(v.getResources(), v.getDrawingCache()); }
public void printscreen_share(View v, Activity context) { View view1 = context.getWindow().getDecorView(); Display display = context.getWindowManager().getDefaultDisplay(); view1.layout(0, 0, display.getWidth(), display.getHeight()); view1.setDrawingCacheEnabled(true); Bitmap bitmap = Bitmap.createBitmap(view1.getDrawingCache()); }
@Override public boolean onInterceptTouchEvent(MotionEvent event) { /**/ if (!waitMoveDrag || (dragListener == null && dropListener == null)) { return super.onInterceptTouchEvent(event); } fileManager.clearClickTime(); int act = event.getAction(); waitMoveDrag = false; switch (act) { case MotionEvent.ACTION_DOWN: int x = (int) event.getX(); int y = (int) event.getY(); int itemNum = pointToPosition(x, y); if (itemNum == INVALID_POSITION) break; dragItemFrom = dragCurPos = itemNum; View item = (View) getChildAt(itemNum - getFirstVisiblePosition()); if (item == null) { break; } dragging = true; // itemHeight = item.getHeight(); // item.setBackgroundColor(Color.BLUE); item.setDrawingCacheEnabled(true); Bitmap bm = Bitmap.createBitmap(item.getDrawingCache()); startDrag(bm, (int) event.getRawX(), (int) event.getRawY()); startDragListener.startDrag(itemNum); return false; } /** */ return super.onInterceptTouchEvent(event); }
private MemberChip constructChipSpan(User user, int offset, boolean pressed) { LayoutInflater lf = (LayoutInflater) getContext().getSystemService(Activity.LAYOUT_INFLATER_SERVICE); View clipView = lf.inflate(R.layout.member_clip, null); ((TextView) clipView.findViewById(R.id.member_clip)).setText(user.name); int spec = MeasureSpec.makeMeasureSpec(0, MeasureSpec.UNSPECIFIED); clipView.measure(spec, spec); clipView.layout(0, 0, clipView.getMeasuredWidth(), clipView.getMeasuredHeight()); Bitmap b = Bitmap.createBitmap(clipView.getWidth(), clipView.getHeight(), Bitmap.Config.ARGB_8888); Canvas canvas = new Canvas(b); canvas.translate(-clipView.getScrollX(), -clipView.getScrollY()); clipView.draw(canvas); clipView.setDrawingCacheEnabled(true); Bitmap cacheBmp = clipView.getDrawingCache(); Bitmap viewBmp = cacheBmp.copy(Bitmap.Config.ARGB_8888, true); clipView.destroyDrawingCache(); Drawable result = new BitmapDrawable(getResources(), viewBmp); result.setBounds(0, 0, viewBmp.getWidth(), viewBmp.getHeight()); MemberChip chip = new MemberChip(result, user); return chip; }
/* * Restore size and visibility for all listitems */ private void unExpandViews(boolean deletion) { for (int i = 0; ; i++) { View v = getChildAt(i); if (v == null) { if (deletion) { // HACK force update of mItemCount int position = getFirstVisiblePosition(); int y = getChildAt(0).getTop(); setAdapter(getAdapter()); setSelectionFromTop(position, y); // end hack } layoutChildren(); // force children to be recreated where needed v = getChildAt(i); if (v == null) { break; } } ViewGroup.LayoutParams params = v.getLayoutParams(); params.height = mItemHeightNormal; v.setLayoutParams(params); v.setVisibility(View.VISIBLE); v.setDrawingCacheEnabled( false); // Resets the drawing cache, the positions might have changed. We don't want the // cache to be wrong. } }
void clearChildrenCache() { final int count = getChildCount(); for (int i = 0; i < count; i++) { final View layout = (View) getChildAt(i); layout.setDrawingCacheEnabled(false); } }
@Override public boolean onLongClick(View view) { if (((BrickAdapter) getAdapter()).getActionMode() != BrickAdapter.ActionModeEnum.NO_ACTION) { return true; } ((BrickAdapter) getAdapter()).isDragging = true; ((BrickAdapter) getAdapter()).setSpinnersEnabled(false); int itemPosition = calculateItemPositionAndTouchPointY(view); boolean drawingCacheEnabled = view.isDrawingCacheEnabled(); view.setDrawingCacheEnabled(true); view.measure( MeasureSpec.makeMeasureSpec(ScreenValues.SCREEN_WIDTH, MeasureSpec.EXACTLY), MeasureSpec.makeMeasureSpec( Utils.getPhysicalPixels(WIDTH_OF_BRICK_PREVIEW_IMAGE, getContext()), MeasureSpec.AT_MOST)); view.layout(0, 0, ScreenValues.SCREEN_WIDTH, view.getMeasuredHeight()); view.setDrawingCacheBackgroundColor(Color.TRANSPARENT); view.buildDrawingCache(true); Bitmap bitmap; if (view.getDrawingCache() == null) { view.setDrawingCacheEnabled(drawingCacheEnabled); bitmap = getBitmapFromView(view, getMeasuredWidth(), view.getHeight()); } else { bitmap = Bitmap.createBitmap(view.getDrawingCache()); } view.setDrawingCacheEnabled(drawingCacheEnabled); startDragging(bitmap, touchPointY); if (!dragNewBrick) { setDragViewAnimation(0); dragNewBrick = false; } dragAndDropListener.drag(itemPosition, itemPosition); dimBackground = true; previousItemPosition = itemPosition; return true; }
/** * 屏幕截图 * * @param activity * @return */ public static Bitmap viewToBitmap(Activity activity) { DisplayMetrics dm = new DisplayMetrics(); activity.getWindowManager().getDefaultDisplay().getMetrics(dm); View view = activity.getWindow().getDecorView(); view.layout(0, 0, dm.widthPixels, dm.heightPixels); view.setDrawingCacheEnabled(true); return Bitmap.createBitmap(view.getDrawingCache()); }
/** * 功能简述:创建一张当前的view的bitmap截图 功能详细描述:根据指定的缩放比例,对当前view进行截图,并返回截图bitmap 注意: * * @param view:待画的view * @param scale:缩放比例 * @return:view的截图,若当前view为null或宽高<=0,则返回null。 */ public static final Bitmap createBitmap(View view, float scale) { Bitmap pRet = null; if (null == view) { Log.i(TAG, "create bitmap function param view is null"); return pRet; } int scaleWidth = (int) (view.getWidth() * scale); int scaleHeight = (int) (view.getHeight() * scale); if (scaleWidth <= 0 || scaleHeight <= 0) { Log.i(TAG, "create bitmap function param view is not layout"); return pRet; } boolean bViewDrawingCacheEnable = view.isDrawingCacheEnabled(); if (!bViewDrawingCacheEnable) { view.setDrawingCacheEnabled(true); } try { Bitmap viewBmp = view.getDrawingCache(true); // 如果拿到的缓存为空 if (viewBmp == null) { pRet = Bitmap.createBitmap( scaleWidth, scaleHeight, view.isOpaque() ? Config.RGB_565 : Config.ARGB_8888); Canvas canvas = new Canvas(pRet); canvas.scale(scale, scale); view.draw(canvas); canvas = null; } else { pRet = Bitmap.createScaledBitmap(viewBmp, scaleWidth, scaleHeight, true); } viewBmp = null; } catch (OutOfMemoryError e) { pRet = null; Log.i(TAG, "create bitmap out of memory"); } catch (Exception e) { pRet = null; Log.i(TAG, "create bitmap exception"); } if (!bViewDrawingCacheEnable) { view.setDrawingCacheEnabled(false); } return pRet; }
/** * @param view * @return 文字转bitmap */ private Bitmap textTobimap(View view) { view.setDrawingCacheEnabled(true); view.measure( MeasureSpec.makeMeasureSpec(0, MeasureSpec.UNSPECIFIED), MeasureSpec.makeMeasureSpec(0, MeasureSpec.UNSPECIFIED)); view.layout(0, 0, view.getMeasuredWidth(), view.getMeasuredHeight()); Bitmap bitmap = view.getDrawingCache(); return bitmap; }
private Bitmap getChildDrawingCache(final View child) { Bitmap bitmap = child.getDrawingCache(); if (bitmap == null) { child.setDrawingCacheEnabled(true); child.buildDrawingCache(); bitmap = child.getDrawingCache(); } return bitmap; }
private static Bitmap convertViewToBitmap(View view) { // 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.setDrawingCacheEnabled(true); // view.buildDrawingCache(); return view.getDrawingCache(); }
/** * 从view 得到图片 * * @param view * @return */ public static Bitmap getBitmapFromView(View view) { view.destroyDrawingCache(); 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.setDrawingCacheEnabled(true); Bitmap bitmap = view.getDrawingCache(true); return bitmap; }
@Override protected void setChildrenDrawingCacheEnabled(boolean enabled) { final int count = getChildCount(); for (int i = 0; i < count; i++) { final View view = getChildAt(i); view.setDrawingCacheEnabled(enabled); // Update the drawing caches view.buildDrawingCache(true); } }
/** * 获取当前屏幕截图,包含状态栏 * * @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(); int width = getScreenWidth(activity); int height = getScreenHeight(activity); Bitmap bp = Bitmap.createBitmap(bmp, 0, 0, width, height); view.destroyDrawingCache(); return bp; }
public static void setActivity(Activity act) { host = act; ViewTools.setActivity(act); View rootView = host.getWindow().getDecorView().getRootView(); rootView.setDrawingCacheEnabled(true); rootView.buildDrawingCache(); // host.getWindow().getDecorView().getViewTreeObserver().addOnGlobalLayoutListener(new // LayoutObserver()); }
public static com.amap.api.maps2d.model.BitmapDescriptor getBitmapFromViewGaode(View view) { view.destroyDrawingCache(); 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.setDrawingCacheEnabled(true); Bitmap bitmap = view.getDrawingCache(true); com.amap.api.maps2d.model.BitmapDescriptor bd = com.amap.api.maps2d.model.BitmapDescriptorFactory.fromBitmap(bitmap); return bd; }