下面列出了android.view.View#setDrawingCacheQuality ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
/**
* 填充布局内容
*/
private static Bitmap layoutView(final View viewBitmap, int width, int height) {
// 整个View的大小 参数是左上角 和右下角的坐标
viewBitmap.layout(0, 0, width, height);
int measuredWidth = View.MeasureSpec.makeMeasureSpec(width, View.MeasureSpec.EXACTLY);
int measuredHeight = View.MeasureSpec.makeMeasureSpec(height, View.MeasureSpec.UNSPECIFIED);
viewBitmap.measure(measuredWidth, measuredHeight);
viewBitmap.layout(0, 0, viewBitmap.getMeasuredWidth(), viewBitmap.getMeasuredHeight());
viewBitmap.setDrawingCacheEnabled(true);
viewBitmap.setDrawingCacheQuality(View.DRAWING_CACHE_QUALITY_HIGH);
viewBitmap.setDrawingCacheBackgroundColor(Color.WHITE);
// 把一个View转换成图片
Bitmap cachebmp = viewConversionBitmap(viewBitmap);
viewBitmap.destroyDrawingCache();
return cachebmp;
}
/**
* 填充布局内容
* @param viewBitmap view
* @param width 宽
* @param height 高
* @return
*/
private static Bitmap layoutView(final View viewBitmap, int width, int height) {
// 整个View的大小 参数是左上角 和右下角的坐标
viewBitmap.layout(0, 0, width, height);
//宽,父容器已经检测出view所需的精确大小,这时候view的最终大小SpecSize所指定的值,相当于match_parent或指定具体数值。
int measuredWidth = View.MeasureSpec.makeMeasureSpec(width, View.MeasureSpec.EXACTLY);
//高,表示父容器不对View有任何限制,一般用于系统内部,表示一种测量状态;
int measuredHeight = View.MeasureSpec.makeMeasureSpec(height, View.MeasureSpec.UNSPECIFIED);
viewBitmap.measure(measuredWidth, measuredHeight);
viewBitmap.layout(0, 0, viewBitmap.getMeasuredWidth(), viewBitmap.getMeasuredHeight());
viewBitmap.setDrawingCacheEnabled(true);
viewBitmap.setDrawingCacheQuality(View.DRAWING_CACHE_QUALITY_HIGH);
viewBitmap.setDrawingCacheBackgroundColor(Color.WHITE);
// 把一个View转换成图片
Bitmap cachebmp = viewConversionBitmap(viewBitmap);
viewBitmap.destroyDrawingCache();
return cachebmp;
}
public Bitmap createBitmap(final Context context, RemoteViews remoteViews, boolean isBig, boolean systemId) {
View mCache = null;
try {
mCache = createView(context, remoteViews, isBig, systemId);
} catch (Throwable throwable) {
try {
// apply失败后,根据布局id创建view
mCache = LayoutInflater.from(context).inflate(remoteViews.getLayoutId(), null);
} catch (Throwable e) {
}
}
if (mCache == null) {
return null;
}
mCache.setDrawingCacheEnabled(true);
mCache.buildDrawingCache();
mCache.setDrawingCacheQuality(View.DRAWING_CACHE_QUALITY_HIGH);
return mCache.getDrawingCache();
}
private Bitmap prepareAndSetBitmap(final View targetView) {
targetView.setDrawingCacheEnabled(true);
targetView.destroyDrawingCache();
targetView.setDrawingCacheQuality(View.DRAWING_CACHE_QUALITY_AUTO);
targetView.buildDrawingCache(true);
Bitmap sourceBitmap = Bitmap.createBitmap(targetView.getDrawingCache());
targetView.setDrawingCacheEnabled(false);
targetView.destroyDrawingCache();
Bitmap overlay = Bitmap.createBitmap((int) (targetView.getMeasuredWidth() / scaleFactor),
(int) (targetView.getMeasuredHeight() / scaleFactor), Bitmap.Config.ARGB_8888);
Canvas canvas = new Canvas(overlay);
// canvas.translate(-targetView.getLeft() / scaleFactor, -targetView.getTop() / scaleFactor);
canvas.scale(1 / scaleFactor, 1 / scaleFactor);
Paint paint = new Paint();
paint.setColor(Color.RED);
paint.setFlags(Paint.FILTER_BITMAP_FLAG);
canvas.drawBitmap(sourceBitmap, 0, 0, paint);
// canvas.drawColor(Color.RED);
sourceBitmap.recycle();
return FastBlur.doBlur(overlay, radius, false);
}
private Drawable buildCounterDrawable(int count, int backgroundImageId) {
LayoutInflater inflater = LayoutInflater.from(this);
View view = inflater.inflate(R.layout.notification_count_layout, null);
view.setBackgroundResource(backgroundImageId);
TextView tvCount = (TextView) view.findViewById(R.id.tvCount);
if (count == 0) {
tvCount.setVisibility(View.GONE);
} else {
tvCount.setVisibility(View.VISIBLE);
tvCount.setText(String.valueOf(count));
}
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.setDrawingCacheQuality(View.DRAWING_CACHE_QUALITY_HIGH);
Bitmap bitmap = Bitmap.createBitmap(view.getDrawingCache());
view.setDrawingCacheEnabled(false);
return new BitmapDrawable(getResources(), bitmap);
}
/**
* Generates a screenshot of the specified `View`
*
* @param view the `View` component
* @return the screenshot
*/
public static Bitmap getViewScreenshot(final View view) {
// set up the drawing cache
view.setDrawingCacheEnabled(true);
view.setDrawingCacheQuality(View.DRAWING_CACHE_QUALITY_HIGH);
// get the screenshot
final Bitmap viewScreenshot = view.getDrawingCache(true);
final Bitmap output = viewScreenshot.copy(viewScreenshot.getConfig(), false);
// disable the drawing cache again
view.destroyDrawingCache();
view.setDrawingCacheEnabled(false);
return output;
}
/**
* @return view的截图,在InVisible时也可以获取到bitmap
*/
public static Bitmap getViewBitmap(View view) {
view.measure(View.MeasureSpec.makeMeasureSpec(view.getMeasuredWidth(), View.MeasureSpec.EXACTLY),
View.MeasureSpec.makeMeasureSpec(view.getMeasuredHeight(), View.MeasureSpec.EXACTLY));
view.setDrawingCacheQuality(View.DRAWING_CACHE_QUALITY_HIGH);
view.setDrawingCacheEnabled(true);
view.buildDrawingCache(true);
return view.getDrawingCache(true);
}
/**
* @return view的截图,在InVisible时也可以获取到bitmap
*/
public static Bitmap getViewBitmap(View view) {
view.measure(View.MeasureSpec.makeMeasureSpec(view.getMeasuredWidth(), View.MeasureSpec.EXACTLY),
View.MeasureSpec.makeMeasureSpec(view.getMeasuredHeight(), View.MeasureSpec.EXACTLY));
view.setDrawingCacheQuality(View.DRAWING_CACHE_QUALITY_HIGH);
view.setDrawingCacheEnabled(true);
view.buildDrawingCache(true);
return view.getDrawingCache(true);
}
public static Bitmap of(View view, BlurFactor factor) {
view.setDrawingCacheEnabled(true);
view.destroyDrawingCache();
view.setDrawingCacheQuality(View.DRAWING_CACHE_QUALITY_LOW);
Bitmap cache = view.getDrawingCache();
Bitmap bitmap = of(view.getContext(), cache, factor);
cache.recycle();
return bitmap;
}
public BlurTask(View target, BlurFactor factor, Callback callback) {
target.setDrawingCacheEnabled(true);
this.res = target.getResources();
this.factor = factor;
this.callback = callback;
target.destroyDrawingCache();
target.setDrawingCacheQuality(View.DRAWING_CACHE_QUALITY_LOW);
capture = target.getDrawingCache();
contextWeakRef = new WeakReference<>(target.getContext());
}
private static Bitmap takeSnapshot(View view) {
view.setDrawingCacheEnabled(true);
view.setDrawingCacheQuality(View.DRAWING_CACHE_QUALITY_AUTO);
Bitmap snapshot = null;
Bitmap cacheBitmap = view.getDrawingCache();
if (cacheBitmap != null) {
snapshot = Bitmap.createBitmap(cacheBitmap);
view.setDrawingCacheEnabled(false);
view.destroyDrawingCache();
}
return snapshot;
}
public static Bitmap of(View view, BlurFactor factor) {
view.setDrawingCacheEnabled(true);
view.destroyDrawingCache();
view.setDrawingCacheQuality(View.DRAWING_CACHE_QUALITY_LOW);
Bitmap cache = view.getDrawingCache();
Bitmap bitmap = of(view.getContext(), cache, factor);
cache.recycle();
return bitmap;
}
public BlurTask(View target, BlurFactor factor, Callback callback) {
this.res = target.getResources();
this.factor = factor;
this.callback = callback;
this.contextWeakRef = new WeakReference<>(target.getContext());
target.setDrawingCacheEnabled(true);
target.destroyDrawingCache();
target.setDrawingCacheQuality(View.DRAWING_CACHE_QUALITY_LOW);
bitmap = target.getDrawingCache();
}
public static Bitmap of(View view, BlurFactor factor) {
view.setDrawingCacheEnabled(true);
view.destroyDrawingCache();
view.setDrawingCacheQuality(View.DRAWING_CACHE_QUALITY_LOW);
Bitmap cache = view.getDrawingCache();
Bitmap bitmap = of(view.getContext(), cache, factor);
cache.recycle();
return bitmap;
}
public BlurTask(View target, BlurFactor factor, Callback callback) {
target.setDrawingCacheEnabled(true);
this.res = target.getResources();
this.factor = factor;
this.callback = callback;
target.destroyDrawingCache();
target.setDrawingCacheQuality(View.DRAWING_CACHE_QUALITY_LOW);
capture = target.getDrawingCache();
contextWeakRef = new WeakReference<>(target.getContext());
}