ImageView不可见,运动事件不能很好地运行

时间:2013-03-09 05:30:39

标签: android overlay android-imageview image-resizing motionevent

我正在编写具有图像叠加功能的服务类 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);
    }
}

1 个答案:

答案 0 :(得分:0)

您抱怨的代码不是将ImageView - mTouchDetector 添加到您的布局中。如果使用普通Java而不是XML,则必须添加它才能使其可见。看看你的第一个例子(工作的例子):

mWindowManager.addView(mTouchDetector, mParams); 

这就是这个想法。