#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 -
'Android' 카테고리의 다른 글
[RelativeLayout] 0. 레이아웃 만들기 (0) | 2010.08.13 |
---|---|
XML 문법 (0) | 2010.08.13 |
[CustomView] 4. Thread 이용하여 그림 움직이기 (0) | 2010.08.12 |
[CustomView] 3. RobotView 그림 위치 지정 - 성공 (0) | 2010.08.12 |
[CustomView] 0. RobotView 생성 (0) | 2010.08.11 |