android] chat head

소프트웨어/Android/Java/Ndk 2016.02.13 16:33 |


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;
    }

}



Posted by cs만두

댓글을 달아 주세요