我已成功设法将布局转换为位图。我当时想要做的是使用我设置的Renderscript模糊方法的Bitmap模糊,但在尝试模糊位图时我一直得到NullPointerException。
当我将Renderscripy blurr方法传递给其他位图时,它可以正常工作,但是当我从布局中传递创建的位图时,它无效。
有人知道我在做错了什么吗?这让我发疯了。
RelativeLayout blurlayout;
blurlayout = (RelativeLayout) view.findViewById(R.id.blurcontainer);
blurlayout.setDrawingCacheEnabled(true);
blurlayout.buildDrawingCache();
Bitmap bluredlayout = blurlayout.getDrawingCache();
//This returns null
Bitmap finalBlur = Bitmap.createBitmap(fastblur16(bluredlayout, 25, getActivity()));
Drawable d = new BitmapDrawable(getResources(), finalBlur);
private static Bitmap fastblur16(Bitmap source, int radius, Context ctx) {
Bitmap bitmap = source.copy(source.getConfig(), true);
RenderScript rs = RenderScript.create(ctx);
Allocation input = Allocation.createFromBitmap(rs, source,
Allocation.MipmapControl.MIPMAP_NONE, Allocation.USAGE_SCRIPT);
Allocation output = Allocation.createTyped(rs, input.getType());
ScriptIntrinsicBlur script = ScriptIntrinsicBlur.create(rs,
Element.U8_4(rs));
script.setRadius(radius);
script.setInput(input);
script.forEach(output);
output.copyTo(bitmap);
return bitmap;
}
堆栈追踪:
FATAL EXCEPTION: main
java.lang.NullPointerException
at com.example.stackoverflowquestion.FragmentView.fastblur16(FragmentView.java:221)
at com.example.stackoverflowquestion.FragmentView.onCreateView(FragmentView.java:178)
at android.support.v4.app.Fragment.performCreateView(Fragment.java:1504)
at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:942)
at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1121)
at android.support.v4.app.BackStackRecord.run(BackStackRecord.java:682)
at android.support.v4.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1484)
at android.support.v4.app.FragmentManagerImpl.executePendingTransactions(FragmentManager.java:482)
at android.support.v4.app.FragmentStatePagerAdapter.finishUpdate(FragmentStatePagerAdapter.java:163)
at android.support.v4.view.ViewPager.populate(ViewPager.java:1073)
at android.support.v4.view.ViewPager.populate(ViewPager.java:919)
at android.support.v4.view.ViewPager.onMeasure(ViewPager.java:1441)
at android.view.View.measure(View.java:16030)
at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5012)
at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1404)
at android.widget.LinearLayout.measureVertical(LinearLayout.java:695)
at android.widget.LinearLayout.onMeasure(LinearLayout.java:588)
at android.view.View.measure(View.java:16030)
at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5012)
at android.widget.FrameLayout.onMeasure(FrameLayout.java:314)
at android.view.View.measure(View.java:16030)
at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5012)
at com.android.internal.widget.ActionBarOverlayLayout.onMeasure(ActionBarOverlayLayout.java:302)
at android.view.View.measure(View.java:16030)
at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5012)
at android.widget.FrameLayout.onMeasure(FrameLayout.java:314)
at com.android.internal.policy.impl.PhoneWindow$DecorView.onMeasure(PhoneWindow.java:2196)
at android.view.View.measure(View.java:16030)
at android.view.ViewRootImpl.performMeasure(ViewRootImpl.java:1915)
at android.view.ViewRootImpl.measureHierarchy(ViewRootImpl.java:1109)
at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1290)
at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1009)
at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:5508)
at android.view.Choreographer$CallbackRecord.run(Choreographer.java:749)
at android.view.Choreographer.doCallbacks(Choreographer.java:562)
at android.view.Choreographer.doFrame(Choreographer.java:532)
at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:735)
at android.os.Handler.handleCallback(Handler.java:730)
at android.os.Handler.dispatchMessage(Handler.java:92)
at android.os.Looper.loop(Looper.java:213)
at android.app.ActivityThread.main(ActivityThread.java:5225)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:525)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:741)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:557)
at dalvik.system.NativeStart.main(Native Method)
使用@Katerina A.的解决方案:
我的片段中的onCreateView中有这段代码
blurlayout = (RelativeLayout) view.findViewById(R.id.blurcontainer);
blurlayout.setDrawingCacheEnabled(true);
blurlayout.buildDrawingCache(true);
drawnCacheBittmap = Bitmap.createBitmap(blurlayout.getDrawingCache());
blurlayout.setDrawingCacheEnabled(false);
然后我尝试使用onStart
@Override
public void onStart() {
// TODO Auto-generated method stub
Bitmap bluredlayout = Bitmap.createBitmap(fastblur16(drawnCacheBitmap, 25,
getActivity()));
Drawable d = new BitmapDrawable(getResources(), bluredlayout);
int sdk = android.os.Build.VERSION.SDK_INT;
if (sdk < android.os.Build.VERSION_CODES.JELLY_BEAN) {
blurlayout.setBackgroundDrawable(d);
} else {
blurlayout.setBackground(d);
}
super.onStart();
}
答案 0 :(得分:1)
我怀疑您的绘图缓存位图为空。 在获取drawingCache
之前,您必须执行以下操作v.setDrawingCacheEnabled(true);
v.buildDrawingCache(true);
Bitmap b = Bitmap.createBitmap(v.getDrawingCache());
v.setDrawingCacheEnabled(false); // clear drawing cache
我仍然认为这是一个时间问题。如果在onCreateView中调用getDrawingCache,您仍然不确定您的布局是否已经过测量。我个人会尝试在onStart中获取drawingCache。或者,您可以使用PreDrawListener在系统准备就绪后立即执行此操作。例如。
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_layout, container, false);
view.getViewTreeObserver().addOnPreDrawListener(new ViewTreeObserver.OnPreDrawListener() {
@Override
public boolean onPreDraw() {
image.getViewTreeObserver().removeOnPreDrawListener(this);
image.buildDrawingCache();
Bitmap bmp = image.getDrawingCache();
fastblur(bmp);
}
});
return view;
}