1 在 res/value 目录下创建 attrs.xml 文件 定义类似:
<?xml version="1.0" encoding="utf-8"?>
<resources>
<declare-styleable name="GameView">
<attr name="text" format="string" />
</declare-styleable>
</resources>
<declare-styleable name="GameView">
<attr name="text" format="string" />
</declare-styleable>
</resources>
2 定义
代码
public class GameView extends View {
super(context);
// TODO Auto-generated constructor stub
}
super(context, attrs);
TypedArray ta = context.obtainStyledAttributes(attrs,
R.styleable.GameView);
int attr = ta.getIndex(i);
case R.styleable.GameView_text:
ss = ta.getString(R.styleable.GameView_text);
break;
default:
break;
}
}
int y = 0;
// TODO Auto-generated method stub
if (miCount < 100) {
miCount++;
} else {
miCount = 0;
}
switch (miCount % 4) {
case 0:
mPaint.setColor(Color.BLUE);
break;
case 1:
mPaint.setColor(Color.GREEN);
break;
case 2:
mPaint.setColor(Color.RED);
break;
case 3:
mPaint.setColor(Color.YELLOW);
break;
default:
mPaint.setColor(Color.WHITE);
break;
}
canvas.drawRect((320 - 80) / 2, y, (320 - 80) / 2 + 80, y + 40, mPaint);
canvas.drawText(ss, 150, 150, mPaint);
private String ss;
public GameView(Context context) {
super(context);
// TODO Auto-generated constructor stub
}
public GameView(Context context, AttributeSet attrs) {
super(context, attrs);
TypedArray ta = context.obtainStyledAttributes(attrs,
R.styleable.GameView);
int n = ta.getIndexCount();
for (int i = 0; i < n; i++) {
int attr = ta.getIndex(i);
switch (attr) {
case R.styleable.GameView_text:
ss = ta.getString(R.styleable.GameView_text);
break;
default:
break;
}
}
}
@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
setMeasuredDimension(measureWidth(widthMeasureSpec),
measureHeight(heightMeasureSpec));
}
private int measureWidth(int measureSpec) {
int result = 300;
int specMode = MeasureSpec.getMode(measureSpec);
int specSize = MeasureSpec.getSize(measureSpec);
if (specMode == MeasureSpec.EXACTLY) {
result = specSize;
} else {
result = 300;
if (specMode == MeasureSpec.AT_MOST) {
result = Math.min(result, specSize);
}
}
return result;
}
/**
* Determines the height of this view
* @param measureSpec A measureSpec packed into an int
* @return The height of the view, honoring constraints from measureSpec
*/
private int measureHeight(int measureSpec) {
int result = 300;
int specMode = MeasureSpec.getMode(measureSpec);
int specSize = MeasureSpec.getSize(measureSpec);
if (specMode == MeasureSpec.EXACTLY) {
result = specSize;
} else {
result = 300;
if (specMode == MeasureSpec.AT_MOST) {
result = Math.min(result, specSize);
}
}
return result;
}
int miCount = 0;
int y = 0;
@Override
// TODO Auto-generated method stub
if (miCount < 100) {
miCount++;
} else {
miCount = 0;
}
Paint mPaint
= new Paint();switch (miCount % 4) {
case 0:
mPaint.setColor(Color.BLUE);
break;
case 1:
mPaint.setColor(Color.GREEN);
break;
case 2:
mPaint.setColor(Color.RED);
break;
case 3:
mPaint.setColor(Color.YELLOW);
break;
default:
mPaint.setColor(Color.WHITE);
break;
}
canvas.drawRect((320 - 80) / 2, y, (320 - 80) / 2 + 80, y + 40, mPaint);
canvas.drawText(ss, 150, 150, mPaint);
}
}
3 使用GameView
代码
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res/eas.org"
android:layout_width="wrap_content" android:layout_height="wrap_content">
<eas.org.GameView android:layout_width="wrap_content" app:text="wangtian"
android:layout_height="wrap_content" />
</LinearLayout>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res/eas.org"
android:layout_width="wrap_content" android:layout_height="wrap_content">
<eas.org.GameView android:layout_width="wrap_content" app:text="wangtian"
android:layout_height="wrap_content" />
</LinearLayout>