안드로이드

[안드로이드 강좌 예제 따라하기 6] 커스텀 뷰 및 Bitmap 표시

App.SHIN 2014. 11. 4. 22:51

오늘은 커스텀 뷰를 만들고 Bitmap을 리소스로 부터 불러와서 표시합니다.

activity를 생성하고 layout에 커스텀 뷰를 설정하고 클래스를 만듭니다.

resource xml 설정

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
 		android:orientation="vertical"
 		android:layout_width="fill_parent"
 		android:layout_height="fill_parent"
 	>
<com.shin.view.CustomView
    android:id="@+id/custom_view"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    />
</LinearLayout>


CustomView를 Xml 파일에 입력을 했다면 변수가 필요합니다.

public CustomView(Context context,AttributeSet attrs)


bitmap = BitmapFactory.decodeResource(getResources(), R.drawable.ex1 );

이렇게 설정을하고 클래스 파일에서 리소스로 부터 Bitmap을 불러옵니다.


public CustomView(Context context,AttributeSet attrs) {
        super(context, attrs);
        orign = new Rect();
        dest = new Rect();
        
        bitmap = BitmapFactory.decodeResource(getResources(), R.drawable.ex1 );
        orign.left = 0;
        orign.right = bitmap.getWidth();
        orign.top = 0;
        orign.bottom = bitmap.getHeight();
        
        paint = new Paint();
        
	}

리소스파일

결과 화면

결과파일


ShinExample_06.zip