0

我正在編寫具有圖像疊加的服務類 imageView疊加是可見的,但在添加imageview調整大小代碼後它不可見。 另外,MotionEvent for resize imageview效果不佳 爲什麼imageview疊加層不可見並且MotionEvent運行不正常? 你能幫我解決這個問題嗎?ImageView不可見,運動事件效果不佳

這裏是我的代碼

未調整此代碼顯示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); 
    } 
} 
+0

你會從未來使用xml真正受益:) – Warpzit 2013-03-09 06:18:52

回答

0

您所抱怨的代碼不會將您的佈局添加到ImageView - mTouchDetector。如果您使用普通Java而不是XML,則必須添加它才能看到它。看看你的第一個例子(工作的):

mWindowManager.addView(mTouchDetector, mParams); 

這就是這裏的想法。