[ImageView]

|
1. ImageView.java 생성
2. View 클래스를 상속받은 클래스는 생성자가 반드시 있어야 하므로 ImageView(Context context) {...} 생성
3. 콘텍스트에서 리소스를 가져온다.
4. 가져온 리소스를 디코드한다.
5. onDraw()를 오버라이드하여 drawBitmap()으로 그림을 그린다.


ImageTest.java
C++pasted just now: 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
package net.itisn.test;

import android.app.Activity;
import android.os.Bundle;

public class ImageTest extends Activity {
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        //setContentView(R.layout.main);
        setContentView(new ImageView(this));
    }
}

ImageView.java
package net.itisn.test;

import android.content.Context;
import android.content.res.Resources;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Rect;

public class ImageView extends android.widget.ImageView {

	private Bitmap image;
	public ImageView(Context context) {
		super(context);
		// TODO Auto-generated constructor stub
		this.setBackgroundColor(Color.WHITE);
		
		//그림 읽기
		Resources r = context.getResources();
		image = BitmapFactory.decodeResource(r, R.drawable.icon);
	}
	
	@Override
	protected void onDraw(Canvas canvas) {
		// TODO Auto-generated method stub
		super.onDraw(canvas);
		canvas.drawBitmap(image, 0, 0, null);
		
		int w = image.getWidth();
		int h = image.getHeight();
		Rect src = new Rect(0, 0, w, h);
		Rect dst = new Rect(0, 70, w*3, h*3+70);
		canvas.drawBitmap(image, src, dst, null);
	}
}



-end

'Android' 카테고리의 다른 글

[AlertDialog]  (0) 2010.08.18
[MenuTest]  (0) 2010.08.18
[LifeCycle]  (0) 2010.08.17
[ShapeEx]  (0) 2010.08.16
[.java로 View Control]  (0) 2010.08.16
And