2010年5月21日星期五

建立自定義視圖(Custom View)

在Android, 你可以擴展視圖類(View), 從而建立自定義的視圖類.

自定義視圖(Custom View)

- 首先, 擴展View定義新類:
public class MyView extends View

- 重寫構造函數:
public MyView(Context context)
public MyView(Context context, AttributeSet attrs)
public MyView(Context context, AttributeSet attrs, int defStyle)

- 重寫protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec)
必須重寫此方法, 調用setMeasuredDimension(int, int)方法設定measuredWith和measuredHeight.

簡單起見, 本例子只使用下面的代碼設定measuredWith和measuredHeight:
setMeasuredDimension(MeasureSpec.getSize(widthMeasureSpec), MeasureSpec.getSize(heightMeasureSpec));

- 重寫protected void onDraw(Canvas canvas)
這裡是你實際繪製你的視圖的代碼.

- 在public void onCreate(Bundle savedInstanceState)設置使用你的自定義視圖.
MyView myView = new MyView(this);
setContentView(myView);

MyView.java
package com.AndroidView;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.util.AttributeSet;
import android.view.View;

public class MyView extends View {

private Paint myPaint = new Paint(Paint.ANTI_ALIAS_FLAG);

public MyView(Context context) {
super(context);
// TODO Auto-generated constructor stub
}

public MyView(Context context, AttributeSet attrs) {
super(context, attrs);
// TODO Auto-generated constructor stub
}

public MyView(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
// TODO Auto-generated constructor stub
}

@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
// TODO Auto-generated method stub
//super.onMeasure(widthMeasureSpec, heightMeasureSpec);

setMeasuredDimension(MeasureSpec.getSize(widthMeasureSpec),
MeasureSpec.getSize(heightMeasureSpec));
}

@Override
protected void onDraw(Canvas canvas) {
// TODO Auto-generated method stub
//super.onDraw(canvas);

int radius;
int height = getMeasuredHeight();
int width = getMeasuredWidth();
int centerX = width/2;
int centerY = height/2;

if (height>width){
radius = width/2;
}
else{
radius = height/2;
}

myPaint.setStyle(Paint.Style.STROKE);
myPaint.setColor(Color.WHITE);

myPaint.setStrokeWidth(1);
canvas.drawLine(centerX, centerY-10, centerX, centerY+10, myPaint);
canvas.drawLine(centerX-10, centerY, centerX+10, centerY, myPaint);

myPaint.setStrokeWidth(10);
myPaint.setColor(Color.RED);
canvas.drawRect(0, 0, width, height, myPaint);

myPaint.setStrokeWidth(3);
myPaint.setColor(Color.GREEN);


canvas.drawCircle(centerX, centerY, radius, myPaint);
}

}


AndroidView.java
package com.AndroidView;

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

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

MyView myView = new MyView(this);
setContentView(myView);
}
}


next: 通過XML調用自定義視圖(Custom View)

1 則留言:

  1. 我想請問一下第12行
    private Paint myPaint = new Paint(Paint.ANTI_ALIAS_FLAG);
    的意思是什麼呢?!可以請板主解釋一下嘛謝謝

    回覆刪除