close tube projcet를 위해 chat head를 사용하기로했다.
chat head는 service에서 window manager를 이용해 view를 추가하는 방식으로 구현한다.
Service를 생성하는 방법은 생략.
내가 작성한 코드가 어렵지는 않으므로 코드 전문을 보고 직접 해석하는게 좋을듯하다.
public class StreamService extends Service { private WindowManager pmWManager = null; private WindowManager.LayoutParams pmWParams = null; private ImageView pmImageView = null; private float pmDownInitX = 0; private float pmDownInitY = 0; public void onCreate() { super.onCreate(); // Window Manager pmWManager = (WindowManager) getSystemService(WINDOW_SERVICE); // Layout Params pmWParams = new WindowManager.LayoutParams( WindowManager.LayoutParams.WRAP_CONTENT, WindowManager.LayoutParams.WRAP_CONTENT, WindowManager.LayoutParams.TYPE_PHONE, WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE, PixelFormat.TRANSLUCENT ); pmWParams.gravity = Gravity.TOP | Gravity.LEFT; pmWParams.x = 100; pmWParams.y = 100; // pmImageView = new ImageView(this); pmImageView.setImageResource(R.drawable.ic_launcher); pmImageView.setOnTouchListener(new View.OnTouchListener() { @Override public boolean onTouch(View v, MotionEvent event) { if(event.getAction() == MotionEvent.ACTION_DOWN) { pmDownInitX = event.getX(); pmDownInitY = event.getY(); } else if(event.getAction() == MotionEvent.ACTION_MOVE) { pmWParams.x = pmWParams.x + (int)(event.getX() - pmDownInitX); pmWParams.y = pmWParams.y + (int)(event.getY() - pmDownInitY); pmWManager.updateViewLayout(pmImageView, pmWParams); } return true; } }); pmWManager.addView(pmImageView, pmWParams); } public void onDestroy() { super.onDestroy(); if(pmImageView != null) { pmWManager.removeView(pmImageView); pmImageView = null; } } @Override public IBinder onBind(Intent intent) { return null; } public int onStartCommand(Intent intent, int flags, int startId) { super.onStartCommand(intent,flags,startId); return START_STICKY; } }
'소프트웨어 > Android/Java/Ndk' 카테고리의 다른 글
Android] BroadcastReceiver 등록 (0) | 2016.06.14 |
---|---|
android] WebView 안멈춤 현상, WebView 계속 소리남 (0) | 2016.02.18 |
android] WindowManager UpdateViewLayout이 벌벌벌 떨릴때 (0) | 2016.02.18 |
android] YouTubePlayer 객체가 파괴되는듯하다.. (0) | 2016.02.11 |
android] YouTube developer (0) | 2016.02.10 |
TcpServer] Java Shutdown Hook (0) | 2015.11.02 |
JavaEffective] 변경 가능 데이터의 동기화(Lock, Synchronized, Volatile) (0) | 2015.08.21 |