我正在编写具有图像叠加层的服务类 imageView 叠加层是可见的,但是在我添加 imageview 调整大小代码后它不可见。此外,调整大小的 imageview 的 MotionEvent 效果不佳 为什么 imageview 覆盖不可见且 MotionEvent 效果不佳?你能帮我解决这个问题吗?
这是我的代码
此代码显示未调整大小的 ImageView 叠加层,它是可见的并且 MotionEvent 运行良好
package kr.hybdms.sidepanel;
import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.app.Service;
import android.content.Context;
import android.content.ContextWrapper;
import android.content.Intent;
import android.net.Uri;
import android.os.IBinder;
import android.util.Log;
import android.view.Gravity;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnTouchListener;
import android.view.WindowManager;
import android.widget.ImageView;
import android.graphics.PixelFormat;
public class TouchDetectService extends Service {
private ImageView mTouchDetector;
private WindowManager.LayoutParams mParams;
private WindowManager mWindowManager;
private static final int MY_NOTIFICATION_ID=1;
private NotificationManager notificationManager;
private Notification myNotification;
final static String ACTION = "NotifyServiceAction";
final static String STOP_SERVICE = "";
final static int RQS_STOP_SERVICE = 1;
private OnTouchListener mViewTouchListener = new OnTouchListener() {
@Override public boolean onTouch(View v, MotionEvent event) {
switch(event.getAction()) {
case MotionEvent.ACTION_MOVE:
Intent lsp = new Intent(getBaseContext(), SidePanel.class);
lsp.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
getApplication().startActivity(lsp);
break;
}
return true;
}
};
@Override
public IBinder onBind(Intent arg0) { return null; }
@Override
public void onCreate() {
super.onCreate();
boolean rightpanel = getSharedPreferences(getPackageName() + "_preferences", Context.MODE_PRIVATE).getBoolean("panelpos_right", true);
boolean notificationison = getSharedPreferences(getPackageName() + "_preferences", Context.MODE_PRIVATE).getBoolean("noti_toggle", true);
Log.i("BOOTSVC", "Service started at the BOOT_COMPLETED.");
if(rightpanel)
{
mTouchDetector = new ImageView(this);
mTouchDetector.setImageResource(R.drawable.detector);
mTouchDetector.setOnTouchListener(mViewTouchListener);
mParams = new WindowManager.LayoutParams(
WindowManager.LayoutParams.WRAP_CONTENT,
WindowManager.LayoutParams.WRAP_CONTENT,
WindowManager.LayoutParams.TYPE_PHONE,
WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE,
PixelFormat.TRANSLUCENT);
mParams.gravity = Gravity.RIGHT | Gravity.CENTER;
mWindowManager = (WindowManager) getSystemService(WINDOW_SERVICE);
mWindowManager.addView(mTouchDetector, mParams);
}
else
{
mTouchDetector = new ImageView(this);
mTouchDetector.setImageResource(R.drawable.detector);
mTouchDetector.setOnTouchListener(mViewTouchListener);
mParams = new WindowManager.LayoutParams(
WindowManager.LayoutParams.WRAP_CONTENT,
WindowManager.LayoutParams.WRAP_CONTENT,
WindowManager.LayoutParams.TYPE_PHONE,
WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE,
PixelFormat.TRANSLUCENT);
mParams.gravity = Gravity.LEFT | Gravity.CENTER;
mWindowManager = (WindowManager) getSystemService(WINDOW_SERVICE);
mWindowManager.addView(mTouchDetector, mParams);
}
if(notificationison){
notificationManager =
(NotificationManager)getSystemService(Context.NOTIFICATION_SERVICE);
myNotification = new Notification(R.drawable.ic_stat_sidepanel,
getText(R.string.service_notification),
System.currentTimeMillis());
Context context = getApplicationContext();
CharSequence notificationTitle = getText(R.string.service_running);
CharSequence notificationText = getText(R.string.service_running_desc);
Intent myIntent = new Intent(getBaseContext(), Settings.class);;
PendingIntent pendingIntent
= PendingIntent.getActivity(getBaseContext(),
0, myIntent,
Intent.FLAG_ACTIVITY_NEW_TASK);
myNotification.defaults |= Notification.DEFAULT_SOUND;
myNotification.flags |= Notification.FLAG_AUTO_CANCEL;
myNotification.flags = Notification.FLAG_ONGOING_EVENT;
myNotification.setLatestEventInfo(context,
notificationTitle,
notificationText,
pendingIntent);
notificationManager.notify(MY_NOTIFICATION_ID, myNotification);
}
else
{
}
}
@Override
public void onDestroy() {
if(mWindowManager != null) {
if(mTouchDetector != null) mWindowManager.removeView(mTouchDetector);
}
super.onDestroy();
notificationManager.cancel(MY_NOTIFICATION_ID);
}
}
此代码显示已调整大小的 ImageView 叠加层,它不可见且 MotionEvent 无法正常工作 imageview 是按首选项值调整大小
package kr.hybdms.sidepanel;
import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.app.Service;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.IBinder;
import android.os.Vibrator;
import android.preference.PreferenceManager;
import android.util.Log;
import android.view.Gravity;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnTouchListener;
import android.view.WindowManager;
import android.widget.ImageView;
import android.graphics.PixelFormat;
public class TouchDetectService extends Service {
private ImageView mTouchDetector;
private WindowManager.LayoutParams mParams;
private WindowManager mWindowManager;
private static final int MY_NOTIFICATION_ID=1;
private NotificationManager notificationManager;
private Notification myNotification;
final static String ACTION = "NotifyServiceAction";
final static String STOP_SERVICE = "";
final static int RQS_STOP_SERVICE = 1;
private OnTouchListener mViewTouchListener = new OnTouchListener() {
@Override public boolean onTouch(View v, MotionEvent event) {
switch(event.getAction()) {
case MotionEvent.ACTION_MOVE:
boolean vibeon = getSharedPreferences(getPackageName() + "_preferences", Context.MODE_PRIVATE).getBoolean("vibe_toggle", true);
if(vibeon){
Vibrator vibe = (Vibrator) getSystemService(Context.VIBRATOR_SERVICE);
vibe.vibrate(10);}
else{}
Intent lsp = new Intent(getBaseContext(), SidePanel.class);
lsp.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
getApplication().startActivity(lsp);
break;
}
return true;
}
};
@Override
public IBinder onBind(Intent arg0) { return null; }
@Override
public void onCreate() {
super.onCreate();
boolean rightpanel = getSharedPreferences(getPackageName() + "_preferences", Context.MODE_PRIVATE).getBoolean("panelpos_right", true);
boolean notificationison = getSharedPreferences(getPackageName() + "_preferences", Context.MODE_PRIVATE).getBoolean("noti_toggle", true);
Log.i("BOOTSVC", "Service started at the BOOT_COMPLETED.");
SharedPreferences myPreference = PreferenceManager.getDefaultSharedPreferences(this);
String dw = myPreference.getString("detector_width", "");
String dh = myPreference.getString("detector_height", "");
mTouchDetector = new ImageView(this);
mTouchDetector.setImageResource(R.drawable.detector);
mTouchDetector.setOnTouchListener(mViewTouchListener);
WindowManager.LayoutParams params = new WindowManager.LayoutParams();
if(dw.equals("025")){
params.height = (int) (mTouchDetector.getDrawable().getIntrinsicWidth()*0.25);
}
else if(dw.equals("050")){
params.height = (int) (mTouchDetector.getDrawable().getIntrinsicWidth()*0.5);
}
else if(dw.equals("075")){
params.height = (int) (mTouchDetector.getDrawable().getIntrinsicWidth()*0.75);
}
else if(dw.equals("100")){
params.height = mTouchDetector.getDrawable().getIntrinsicWidth();
}
else if(dw.equals("200")){
params.height = mTouchDetector.getDrawable().getIntrinsicWidth()*2;
}
else if(dw.equals("300")){
params.height = mTouchDetector.getDrawable().getIntrinsicWidth()*3;
}
else if(dw.equals("400")){
params.height = mTouchDetector.getDrawable().getIntrinsicWidth()*4;
}
if (dh.equals("025")){
params.width = (int) (mTouchDetector.getDrawable().getIntrinsicHeight()*0.25);
}
else if (dh.equals("050")){
params.width = (int) (mTouchDetector.getDrawable().getIntrinsicHeight()*0.5);
}
else if (dh.equals("075")){
params.width = (int) (mTouchDetector.getDrawable().getIntrinsicHeight()*0.75);
}
else if (dh.equals("100")){
params.width = mTouchDetector.getDrawable().getIntrinsicHeight();
}
else if (dh.equals("200")){
params.width = mTouchDetector.getDrawable().getIntrinsicHeight()*2;
}
else if (dh.equals("300")){
params.width = mTouchDetector.getDrawable().getIntrinsicHeight()*3;
}
else if (dh.equals("400")){
params.width = mTouchDetector.getDrawable().getIntrinsicHeight()*4;
}
params.flags = WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE;
params.format = PixelFormat.TRANSLUCENT;
params.type = WindowManager.LayoutParams.TYPE_PHONE;
if(rightpanel)
{
params.gravity = Gravity.RIGHT & Gravity.CENTER;
}
else
{
params.gravity = Gravity.LEFT & Gravity.CENTER;
}
mWindowManager = (WindowManager) getSystemService(WINDOW_SERVICE);
mWindowManager.addView(mTouchDetector, params);
if(notificationison){
notificationManager =
(NotificationManager)getSystemService(Context.NOTIFICATION_SERVICE);
myNotification = new Notification(R.drawable.ic_stat_sidepanel,
getText(R.string.service_notification),
System.currentTimeMillis());
Context context = getApplicationContext();
CharSequence notificationTitle = getText(R.string.service_running);
CharSequence notificationText = getText(R.string.service_running_desc);
Intent myIntent = new Intent(getBaseContext(), Settings.class);;
PendingIntent pendingIntent
= PendingIntent.getActivity(getBaseContext(),
0, myIntent,
Intent.FLAG_ACTIVITY_NEW_TASK);
myNotification.defaults |= Notification.DEFAULT_SOUND;
myNotification.flags |= Notification.FLAG_AUTO_CANCEL;
myNotification.flags = Notification.FLAG_ONGOING_EVENT;
myNotification.setLatestEventInfo(context,
notificationTitle,
notificationText,
pendingIntent);
notificationManager.notify(MY_NOTIFICATION_ID, myNotification);
}
else
{
}
}
@Override
public void onDestroy() {
if(mWindowManager != null) {
if(mTouchDetector != null) mWindowManager.removeView(mTouchDetector);
}
super.onDestroy();
notificationManager.cancel(MY_NOTIFICATION_ID);
}
}