问题描述
我创建了一个在屏幕上应用彩色滤镜的应用.我希望能够通过按下按钮来更改过滤器的颜色.更改 xml 文件中的背景颜色有效,但是当我尝试在服务中更改它时,它不会更改颜色.我没有收到任何错误,但颜色没有改变.我认为因为我使用 windowmanager 添加视图而不是使用 setContentView
设置它,所以它可能不会应用颜色更改.这是我的服务的代码,我知道正在调用该服务是因为 Log.d(TAG,"onCreated");
出现在 logcat 中.顺便说一下,我曾尝试更改 root
而不是 bView
的颜色,但我最终得到相同的结果.
I have created an app that applies a coloured filter over the screen. I would like to be able to change the colour of the filter by pressing buttons. Changing the background colour in the xml file works, but when I try changing it in a service it does not change colour. I am getting no errors but the colour does not change. I think that because I am adding the view with windowmanager and not setting it with setContentView
, it might not apply the colour changes. Here is the code of my service, I know the service is being called because the Log.d(TAG,"onCreated");
is showing up in the logcat. By the way, I have tried changing the colour of root
instead of bView
, but I end up with the same result.
public class ChangeColor extends Service {
public static final String TAG = "ChangeColour";
View mOverlayView;
@Override
public IBinder onBind(Intent intent) {
return null;
}
@Override
public void onCreate() {
super.onCreate();
}
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
LayoutInflater inflater = (LayoutInflater) this.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
mOverlayView = inflater.inflate(R.layout.overlay_view, null);
LinearLayout bView = (LinearLayout) mOverlayView.findViewById(R.id.lin);
if (bView != null) {
View root = bView.getRootView();
bView.setBackgroundColor(Color.parseColor("#000000"));
Log.d(TAG, "onCreated");
}
return START_NOT_STICKY;
}
}
见 我的旧帖子 如果您想查看添加视图的服务和活动.如果有人有任何想法,请告诉我!谢谢.
See my old post if you would like to see the service and activity that is adding the view. If anybody has any ideas let me know! Thanks.
推荐答案
请尝试以下操作.我没有看到您将视图添加到窗口的位置,尽管我知道您以前在那里有过.我还更改了颜色和一些值,以便我可以注意到更改.确保在您的清单中定义了该服务.
Try the following. I didn't see where you added the view to the window, although I know you had it there before. I also changed the color and some of the values so I could notice the change. Make sure the service is defined in your manifest.
public class ChangeColor extends Service {
public static final String TAG = "ChangeColour";
View mOverlayView;
@Override
public IBinder onBind(Intent intent) {
return null;
}
@Override
public void onCreate() {
super.onCreate();
final WindowManager.LayoutParams params = new WindowManager.LayoutParams(
WindowManager.LayoutParams.MATCH_PARENT,
WindowManager.LayoutParams.MATCH_PARENT,
WindowManager.LayoutParams.TYPE_SYSTEM_OVERLAY,
WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE |
WindowManager.LayoutParams.FLAG_NOT_TOUCH_MODAL |
WindowManager.LayoutParams.FLAG_LAYOUT_IN_SCREEN,
PixelFormat.TRANSLUCENT);
params.alpha = 0.5F;
params.dimAmount = 0.9F;
WindowManager wm = (WindowManager) getSystemService(Context.WINDOW_SERVICE);
LayoutInflater inflater = (LayoutInflater) this.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
mOverlayView = inflater.inflate(R.layout.overlay_view, null);
wm.addView(mOverlayView, params);
}
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
if (mOverlayView != null) {
mOverlayView.setBackgroundColor(Color.parseColor("#FF0000"));
}
return START_NOT_STICKY;
}
}
这篇关于视图颜色不会以编程方式改变的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!