下面列出了android.view.View#setDrawingCacheEnabled ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
@Override
public void addView(View child, int index, ViewGroup.LayoutParams params) {
if (!checkLayoutParams(params)) {
params = generateLayoutParams(params);
}
final LayoutParams lp = (LayoutParams) params;
lp.isDecor |= child instanceof Decor;
if (mInLayout) {
if (lp != null && lp.isDecor) {
throw new IllegalStateException("Cannot add pager decor view during layout");
}
lp.needsMeasure = true;
addViewInLayout(child, index, params);
} else {
super.addView(child, index, params);
}
if (USE_CACHE) {
if (child.getVisibility() != GONE) {
child.setDrawingCacheEnabled(mScrollingCacheEnabled);
} else {
child.setDrawingCacheEnabled(false);
}
}
}
@Override
public void addView(View child, int index, ViewGroup.LayoutParams params) {
if (!checkLayoutParams(params)) {
params = generateLayoutParams(params);
}
final LayoutParams lp = (LayoutParams) params;
lp.isDecor |= child instanceof Decor;
if (mInLayout) {
if (lp != null && lp.isDecor) {
throw new IllegalStateException("Cannot add pager decor view during layout");
}
lp.needsMeasure = true;
addViewInLayout(child, index, params);
} else {
super.addView(child, index, params);
}
if (USE_CACHE) {
if (child.getVisibility() != GONE) {
child.setDrawingCacheEnabled(mScrollingCacheEnabled);
} else {
child.setDrawingCacheEnabled(false);
}
}
}
@Override
public void addView(View child, int index, ViewGroup.LayoutParams params) {
if (!checkLayoutParams(params)) {
params = generateLayoutParams(params);
}
final LayoutParams lp = (LayoutParams) params;
lp.isDecor |= child instanceof Decor;
if (mInLayout) {
if (lp != null && lp.isDecor) {
throw new IllegalStateException("Cannot add pager decor view during layout");
}
lp.needsMeasure = true;
addViewInLayout(child, index, params);
} else {
super.addView(child, index, params);
}
if (USE_CACHE) {
if (child.getVisibility() != GONE) {
child.setDrawingCacheEnabled(mScrollingCacheEnabled);
} else {
child.setDrawingCacheEnabled(false);
}
}
}
/**
* 获取当前屏幕截图,不包含状态栏
*
* @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;
}
@Override
public void addView(View child, int index, ViewGroup.LayoutParams params) {
if (!checkLayoutParams(params)) {
params = generateLayoutParams(params);
}
final LayoutParams lp = (LayoutParams) params;
lp.isDecor |= child instanceof Decor;
if (mInLayout) {
if (lp != null && lp.isDecor) {
throw new IllegalStateException("Cannot add pager decor view during layout");
}
lp.needsMeasure = true;
addViewInLayout(child, index, params);
} else {
super.addView(child, index, params);
}
if (USE_CACHE) {
if (child.getVisibility() != GONE) {
child.setDrawingCacheEnabled(mScrollingCacheEnabled);
} else {
child.setDrawingCacheEnabled(false);
}
}
}
@Override
public void addView(View child, int index, ViewGroup.LayoutParams params) {
if (!checkLayoutParams(params)) {
params = generateLayoutParams(params);
}
final LayoutParams lp = (LayoutParams) params;
lp.isDecor |= child instanceof Decor;
if (mInLayout) {
if (lp != null && lp.isDecor) {
throw new IllegalStateException("Cannot add pager decor view during layout");
}
lp.needsMeasure = true;
addViewInLayout(child, index, params);
} else {
super.addView(child, index, params);
}
if (USE_CACHE) {
if (child.getVisibility() != GONE) {
child.setDrawingCacheEnabled(mScrollingCacheEnabled);
} else {
child.setDrawingCacheEnabled(false);
}
}
}
/**
* 获取当前屏幕截图,不包含状态栏
* <p>需要用到上面获取状态栏高度getStatusBarHeight的方法</p>
*
* @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 = RxBarTool.getStatusBarHeight(activity);
int width = getScreenWidth(activity);
int height = getScreenHeight(activity);
Bitmap ret = Bitmap.createBitmap(bmp, 0, statusBarHeight, width, height - statusBarHeight);
view.destroyDrawingCache();
return ret;
}
@Override
protected boolean drawChild(Canvas canvas, View child, long drawingTime) {
final LayoutParams lp = (LayoutParams) child.getLayoutParams();
boolean result;
final int save = canvas.save(Canvas.CLIP_SAVE_FLAG);
if (mCanSlide && !lp.slideable && mSlideableView != null) {
// Clip against the slider; no sense drawing what will immediately be covered.
canvas.getClipBounds(mTmpRect);
mTmpRect.right = Math.min(mTmpRect.right, mSlideableView.getLeft());
canvas.clipRect(mTmpRect);
}
if (Build.VERSION.SDK_INT >= 11) { // HC
result = super.drawChild(canvas, child, drawingTime);
} else {
if (lp.dimWhenOffset && mSlideOffset > 0) {
if (!child.isDrawingCacheEnabled()) {
child.setDrawingCacheEnabled(true);
}
final Bitmap cache = child.getDrawingCache();
if (cache != null) {
canvas.drawBitmap(cache, child.getLeft(), child.getTop(), lp.dimPaint);
result = false;
} else {
Log.e(TAG, "drawChild: child view " + child + " returned null drawing cache");
result = super.drawChild(canvas, child, drawingTime);
}
} else {
if (child.isDrawingCacheEnabled()) {
child.setDrawingCacheEnabled(false);
}
result = super.drawChild(canvas, child, drawingTime);
}
}
canvas.restoreToCount(save);
return result;
}
/**
* Takes screenshot of current screen
*
* @param view to be taken screenshot of
* @return bitmap of the screenshot
*/
private static Bitmap getScreenShot(View view) {
View screenView = view.getRootView();
screenView.setDrawingCacheEnabled(true);
Bitmap bitmap = Bitmap.createBitmap(screenView.getDrawingCache());
screenView.setDrawingCacheEnabled(false);
return bitmap;
}
/**
* 获取当前屏幕截图,包含状态栏
*
* @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;
}
/**
* 抓起
*
* @param columnView 抓起的 View
* @param position 抓起的 View 在 横向RecyclerView 的 position
*/
public void dragCol(View columnView, int position) {
columnView.destroyDrawingCache();
columnView.setDrawingCacheEnabled(true);
Bitmap bitmap = columnView.getDrawingCache();
if (bitmap != null && !bitmap.isRecycled()) {
mDragImageView.setImageBitmap(bitmap);
mDragImageView.setRotation(1.5f);
mDragImageView.setAlpha(0.8f);
isDraggingColumn = true;
if (columnView.getTag() instanceof DragColumn) {
dragColumn = (DragColumn) columnView.getTag();
}
int dragPage = mHorizontalRecyclerView.getCurrentPosition();
RecyclerView.ViewHolder holder = (RecyclerView.ViewHolder) mHorizontalRecyclerView.findViewHolderForAdapterPosition(dragPage);
if (holder != null && holder.itemView != null && holder.getItemViewType() == TYPE_CONTENT) {
mCurrentVerticalRecycleView = ((DragHorizontalViewHolder) holder).getRecyclerView();
mPagerPosition = dragPage;
}
getTargetHorizontalRecyclerViewScrollBoundaries();
getTargetVerticalRecyclerViewScrollBoundaries();
int[] location = new int[2];
columnView.getLocationOnScreen(location);
mWindowParams.x = location[0];
mWindowParams.y = location[1];
mBornLocationX = location[0];
mBornLocationY = location[1];
confirmOffset = false;
mPosition = position;
mWindowManager.addView(mDragImageView, mWindowParams);
getHorizontalAdapter().onDrag(position);
}
}
private void setScrollingCacheEnabled(boolean enabled) {
if (mScrollingCacheEnabled != enabled) {
mScrollingCacheEnabled = enabled;
if (USE_CACHE) {
final int size = getChildCount();
for (int i = 0; i < size; ++i) {
final View child = getChildAt(i);
if (child.getVisibility() != GONE) {
child.setDrawingCacheEnabled(enabled);
}
}
}
}
}
/**
* 通知重新绘制
* 使用场景:网络图片加载后调用
* 建议:配合{@link #setStrongReference(boolean)}方法使用,体验更佳
* @param recyclerView recyclerView
* @param position position
*/
public void notifyRedraw(RecyclerView recyclerView, View viewGroup, int position) {
viewGroup.setDrawingCacheEnabled(false);
mBitmapCache.remove(position);
mHeadViewCache.remove(position);
int left = recyclerView.getPaddingLeft();
int right = recyclerView.getWidth() - recyclerView.getPaddingRight();
measureAndLayoutView(viewGroup, left, right);
mHeadViewCache.put(position, viewGroup);
recyclerView.invalidate();
}
/**
* 从View 中生成 BitMap
*
* @param view
* @return
*/
public static Bitmap convertFromView(View view) {
Bitmap bitmap=null;
Rect rect = new Rect();
view.getDrawingRect(rect);
view.destroyDrawingCache();
view.setDrawingCacheEnabled(true);
view.buildDrawingCache(true);
bitmap = view.getDrawingCache(true);
/**
* After rotation, the DecorView has no height and no width. Therefore
* .getDrawingCache() return null. That's why we have to force measure and layout.
*/
if (bitmap == null) {
view.measure(
View.MeasureSpec.makeMeasureSpec(rect.width(), View.MeasureSpec.EXACTLY),
View.MeasureSpec.makeMeasureSpec(rect.height(), View.MeasureSpec.EXACTLY)
);
view.layout(0, 0, view.getMeasuredWidth(),
view.getMeasuredHeight());
view.destroyDrawingCache();
view.setDrawingCacheEnabled(true);
view.buildDrawingCache(true);
bitmap = view.getDrawingCache(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();
// Bitmap bitmap = view.getDrawingCache();
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
if (!view.isHardwareAccelerated() && enabled) {
view.buildDrawingCache(true);
}
}
}
@Override
public void onAttachedToWindow() {
super.onAttachedToWindow();
final View view = this.getWindow().getDecorView();
if (view != null)
view.setDrawingCacheEnabled(true);
}
/**
* 获取一个 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;
}
/**
* 把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.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;
}
protected boolean drawChild(Canvas canvas, View view, long l1)
{
LayoutParams layoutparams = (LayoutParams)view.getLayoutParams();
int i1 = canvas.save(2);
if (j && !layoutparams.a && k != null)
{
canvas.getClipBounds(w);
w.right = Math.min(w.right, k.getLeft());
canvas.clipRect(w);
}
boolean flag;
if (android.os.Build.VERSION.SDK_INT >= 11)
{
flag = super.drawChild(canvas, view, l1);
} else
if (layoutparams.b && l > 0.0F)
{
if (!view.isDrawingCacheEnabled())
{
view.setDrawingCacheEnabled(true);
}
android.graphics.Bitmap bitmap = view.getDrawingCache();
if (bitmap != null)
{
canvas.drawBitmap(bitmap, view.getLeft(), view.getTop(), layoutparams.c);
flag = false;
} else
{
Log.e("SlidingPaneLayout", (new StringBuilder()).append("drawChild: child view ").append(view).append(" returned null drawing cache").toString());
flag = super.drawChild(canvas, view, l1);
}
} else
{
if (view.isDrawingCacheEnabled())
{
view.setDrawingCacheEnabled(false);
}
flag = super.drawChild(canvas, view, l1);
}
canvas.restoreToCount(i1);
return flag;
}
@Override
public boolean onTouchEvent(MotionEvent event) {
LinePath linePath;
int index;
int id;
int eventMasked = event.getActionMasked();
switch (eventMasked) {
case MotionEvent.ACTION_DOWN:
case MotionEvent.ACTION_POINTER_DOWN: {
index = event.getActionIndex();
id = event.getPointerId(index);
if (extractingColor) { //If the user chose the 'extract color' menu option, the touch event indicates where they want to extract the color from.
extractingColor = false;
View v = findViewById(R.id.CanvasId);
v.setDrawingCacheEnabled(true);
Bitmap cachedBitmap = v.getDrawingCache();
int newColor = cachedBitmap.getPixel(Math.round(event.getX(index)), Math.round(event.getY(index)));
v.destroyDrawingCache();
colorChanged(newColor);
Toast.makeText(getApplicationContext(),
R.string.color_extracted,
Toast.LENGTH_SHORT).show();
} else {
linePath = multiLinePathManager.addLinePathWithPointer(id);
if (linePath != null) {
linePath.touchStart(event.getX(index), event.getY(index));
} else {
Log.e("anupam", "Too many fingers!");
}
}
break;
}
case MotionEvent.ACTION_MOVE:
for (int i = 0; i < event.getPointerCount(); i++) {
id = event.getPointerId(i);
index = event.findPointerIndex(id);
linePath = multiLinePathManager.findLinePathFromPointer(id);
if (linePath != null) {
linePath.touchMove(event.getX(index), event.getY(index));
}
wasMadeDirty = true;
}
break;
case MotionEvent.ACTION_UP:
case MotionEvent.ACTION_POINTER_UP:
case MotionEvent.ACTION_CANCEL:
index = event.getActionIndex();
id = event.getPointerId(index);
linePath = multiLinePathManager.findLinePathFromPointer(id);
if (linePath != null) {
linePath.lineTo(linePath.getLastX(), linePath.getLastY());
// Commit the path to our offscreen
mCanvas.drawPath(linePath, mPaint);
// Kill this so we don't double draw
linePath.reset();
// Allow this LinePath to be associated to another idPointer
linePath.disassociateFromPointer();
}
wasMadeDirty = true;
break;
}
invalidate();
return true;
}