[CustomView] 1. 그림 정가운데 출력

|
#RobotView 이미지 가운체 드로우
[RobotView.java]
package net.itisn.test;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.drawable.Drawable;
import android.util.AttributeSet;
import android.view.View;

public class RobotView extends View {

    private Drawable image;

    public RobotView(Context context, AttributeSet attrs) {
    	super(context, attrs);
    	// TODO Auto-generated constructor stub
    	image = this.getResources().getDrawable(R.drawable.su);
    }

    @Override
    protected void onDraw(Canvas canvas) {
    	// TODO Auto-generated method stub
    	int viewWidth = this.getWidth();
    	int viewHeight = this.getHeight();

    	int imageWidth = image.getIntrinsicWidth();
    	int imageHeight = image.getIntrinsicHeight();

    	int x = viewWidth / 2 - imageWidth / 2;
    	int y = viewHeight / 2 - imageHeight / 2;

    	image.setBounds(x, y, x + imageWidth, y + imageHeight);
    	image.draw(canvas);

    	super.onDraw(canvas);
    }
}

[main.xml]
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    >

<TextView
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:text="@string/hello"
    />

<net.itisn.test.RobotView
    android:id="@+id/su"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    />

</LinearLayout>

- END -

And