프로그래밍/안드로이드

안드로이드 - 윈도우 관리자 활용하기

가카리 2015. 9. 5. 15:31
반응형

윈도우 관리자는 안드로이드 프레임워크를 구성하는 주요 모듈로 윈도우를 관리한다.

 

다음의 호출문으로 구할 수 있다.

 

getSystemService(Context.WINDOW_SERVICE)

 

윈도우 관리는 대부분 시스템 내부에서 알아서 수행되므로 공개된 기능은 많지 않다.

 

다음 메소드는 윈도우가 실행되는 화면에 대한 정보를 구한다.

 

    Display getDefaultDisplay()

 

Display 클래스는 장비의 화면 폭이나 높이, 방향, 갱신 주기 등의 정보를 제공한다.

 

다음 메소드는 ViewManager 인터페이스로부터 상속받은 것이며 윈도우에 개별 뷰를 추가하거나 삭제한다.

 

    void addView(View view, ViewGroup.LayoutParams params)

    void removeView(View view)

    void updateViewLayout(View view, ViewGroup.LayoutParams params)

 

 

다음은 레이아웃을 벗어난 위치에 임의의 뷰를 배치하는 예제이다.

 

activity_test.xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"

xmlns:tools="http://schemas.android.com/tools"

android:layout_width="match_parent"

android:layout_height="match_parent"

android:orientation="vertical"

>

 

<TextView

android:id="@+id/result"

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:textSize="20sp" />

 

</LinearLayout>

 

WindowManagerTest.java

 

package com.example.ch18_windowmanager;

 

import android.app.Activity;

import android.content.Context;

import android.graphics.PixelFormat;

import android.graphics.Point;

import android.os.Bundle;

import android.view.Display;

import android.view.Gravity;

import android.view.WindowManager;

import android.widget.ImageView;

import android.widget.TextView;

 

public class WindowManagerTest extends Activity {

 

    @Override

    protected void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);

        setContentView(R.layout.activity_test);

      

        //윈도우 관리자 가져오기

        WindowManager wm = (WindowManager)getSystemService(Context.WINDOW_SERVICE);

        Display dis = wm.getDefaultDisplay();

        TextView result = (TextView)findViewById(R.id.result);

        Point pt = new Point();

        dis.getSize(pt);

        result.setText("width = " + pt.x + "height = " + pt.y +

                "\nrotate = " + dis.getRotation()/*dis.getOrientation()*/);

        

        //이미지뷰 하나 가져오고 파라미터 세팅

        ImageView img = new ImageView(this);

        img.setImageResource(R.drawable.ic_launcher);

        WindowManager.LayoutParams param = new WindowManager.LayoutParams();

        param.gravity = Gravity.LEFT | Gravity.TOP;

        param.x = 100;

        param.y = 20;

        param.width = WindowManager.LayoutParams.WRAP_CONTENT;

        param.height = WindowManager.LayoutParams.WRAP_CONTENT;

        

        //FLAG_LAYOUT_IN_SCREEN 이므로 윈도우의 좌상단을 기준으로 좌표에 배치됨

        param.flags = WindowManager.LayoutParams.FLAG_LAYOUT_IN_SCREEN

                | WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE;

        //이미지가 아닌 버튼이나 스피너가 투명색을 제대로 표현하게 .

        param.format = PixelFormat.TRANSLUCENT;

        

        //만든 뷰를 레이아웃에 붙인다.

        wm.addView(img, param);

        

    }

 

      

    

}

 

 

출력 화면

 

 

반응형