0
在我的应用我使用programmatically
取得relativeLayout
其连接到WindowManager
。我正在试图模糊relativeLayout
使relativeLayout
背后,一切都会看起来blur.I曾经尝试这样做下面的代码:错误,而试图以模糊的RelativeLayout在android系统
/** *由Adarsh于2016年12月12日创建。 */
public class Exa extends Service {
RelativeLayout relativeLayout;
WindowManager windowManager;
RenderScript mRS;
ScriptIntrinsicBlur script;
Allocation allocOriginalScreenshot,allocBlurred;
TextureView textureViewBlurred;
private static final String TAG="MainActivity";
@Override
public void onCreate(){
example();
relativeLayout=new RelativeLayout(this);
RelativeLayout.LayoutParams layoutParams=new RelativeLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT);
relativeLayout.setLayoutParams(layoutParams);
windowManager=(WindowManager)getSystemService(WINDOW_SERVICE);
WindowManager.LayoutParams layoutParamsw= new WindowManager.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT, WindowManager.LayoutParams.TYPE_PHONE, WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE,PixelFormat.TRANSPARENT);
windowManager.addView(relativeLayout,layoutParamsw);
blurView(relativeLayout); //adding layout for blurring effect
}
////all these code helps to give blurring effect
private void example() {
mRS=RenderScript.create(this);
script=ScriptIntrinsicBlur.create(mRS, Element.RGBA_8888(mRS));
script.setRadius(5);
}
Bitmap getViewScreenshot(View v){
v.setDrawingCacheEnabled(true);
Bitmap b=Bitmap.createBitmap(v.getDrawingCache());
v.setDrawingCacheEnabled(false);
return b;
}
void replaceView(View originalView,View newView){
originalView.setTag(newView);
newView.setLayoutParams(new FrameLayout.LayoutParams(originalView.getLayoutParams()));
ViewGroup parent=(ViewGroup)originalView.getParent();
int index=parent.indexOfChild(originalView);
parent.removeView(originalView);
parent.addView(newView,index);
}
void restoreView(View v){
Log.i("hii","unblurring view");
View otherView=(View)v.getTag();
if (otherView!=null&&otherView.getParent()!=null){
replaceView(otherView,v);
}
else if (v!=null&&v.getParent()!=null){
replaceView(v,otherView);
}
}
void blurView(View v){
Bitmap viewScreenshot=getViewScreenshot(v);
if(allocOriginalScreenshot!=null&&(allocOriginalScreenshot.getType().getX()!=viewScreenshot.getWidth()||allocOriginalScreenshot.getType().getY()!=viewScreenshot.getHeight())){
allocOriginalScreenshot.destroy();
allocBlurred.destroy();
textureViewBlurred=null;
allocOriginalScreenshot=null;
allocBlurred=null;
}
if(allocOriginalScreenshot==null){
allocOriginalScreenshot=Allocation.createFromBitmap(mRS,viewScreenshot);
allocBlurred=Allocation.createTyped(mRS,allocOriginalScreenshot.getType(),Allocation.USAGE_SCRIPT|Allocation.USAGE_IO_OUTPUT);
textureViewBlurred=new TextureView(this);
textureViewBlurred.setOpaque(false);
textureViewBlurred.setSurfaceTextureListener(surfaceTextureListener);
}
else {
allocOriginalScreenshot.copyFrom(viewScreenshot);
}
replaceView(v,textureViewBlurred);
}
void unblur(View v){
restoreView(v);
}
void executeBlur(){
Log.d(TAG,"Executing blur");
script.setInput(allocOriginalScreenshot);
script.forEach(allocBlurred);
allocBlurred.ioSend();
}
TextureView.SurfaceTextureListener surfaceTextureListener=new TextureView.SurfaceTextureListener() {
@Override
public void onSurfaceTextureAvailable(SurfaceTexture surface, int width, int height) {
allocBlurred.setSurface(new Surface(surface));
executeBlur();
}
@Override
public void onSurfaceTextureSizeChanged(SurfaceTexture surface, int width, int height) {
}
@Override
public boolean onSurfaceTextureDestroyed(SurfaceTexture surface) {
return false;
}
@Override
public void onSurfaceTextureUpdated(SurfaceTexture surface) {
}
};
@Nullable
@Override
public IBinder onBind(Intent intent) {
return null;
}
}
当我尝试运行它,它给我的错误是这样
java.lang.NullPointerException: Attempt to invoke virtual method 'int android.graphics.Bitmap.getWidth()' on a null object reference
当我尝试在我的MainActivity
它完美的作品和模糊的所有内部的relativeLayout
内容相同的节目。 请帮帮我。
好吧那么你有什么想法我该如何模糊布局 – Adarsh
服务不能有布局。如果您正在尝试录制外部应用程序,则应查看API 21中添加的[MediaProjection](https://developer.android.com/reference/android/media/projection/package-summary.html)。您应该如果你想扩展对早期API的支持,请检查[this](http://stackoverflow.com/a/32623147/5980145)。 – Bhav