public
class
VerticalTextView
extends
View
{
/**
* 系统范例:LabelView
* 1.新建attrs文件,增加属性:
<declare-styleable name="LabelView">
<attr name="text" format="string" />
<attr name="textColor" format="color" />
<attr name="textSize" format="dimension" />
</declare-styleable>
//////////////////////////////////////////////
<declare-styleable name="DraggableDot">
<attr name="radius" format="dimension" />
<attr name="legend" format="string" />
<attr name="anr">
<enum name="none" value="0" />
<enum name="thumbnail" value="1" />
<enum name="drop" value="2" />
</attr>
</declare-styleable>
////////////////////////////////////////////////
2.增加自己的命名空间,将android修改为自己的包名
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:MyView="http://schemas.android.com/apk/res/com.example.aexh23_paint_canvas"
3.在layout中创建自定义view,增加属性
<com.example.aexh23_paint_canvas.VerticalTextView
android:id="@+id/verticalTextView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
MyView:mytext="HelloWorld"
MyView:mytextColor="#654321"
MyView:mytextSize="35sp"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true" />
4.在自定义view类中增加方法
setTextColor(a.getColor(R.styleable.LabelView_textColor, 0xFF000000));
int textSize = a.getDimensionPixelOffset(R.styleable.LabelView_textSize, 0);
if (textSize > 0) {
setTextSize(textSize);
}
*/
private
Paint mPaint;
private
CharSequence s;
private
int
c;
private
int
d;
public
VerticalTextView(Context context, AttributeSet attrs)
{
super
(context, attrs);
TypedArray a = context.obtainStyledAttributes(attrs,
R.styleable.MyView);
s = a.getString(R.styleable.MyView_mytext);
c = a.getColor(R.styleable.MyView_mytextColor,
0xFF000000
);
d = a.getDimensionPixelOffset(R.styleable.MyView_mytextSize,
0
);
init();
a.recycle();
}
private
void
init()
{
mPaint =
new
Paint();
mPaint.setColor(c );
mPaint.setTextSize(d);
mPaint.setAntiAlias(
true
);
}
@Override
protected
void
onDraw(Canvas canvas)
{
super
.onDraw(canvas);
Bitmap bitmap = BitmapFactory.decodeResource(getResources(), R.drawable.bg);
canvas.rotate(
30
);
canvas.drawText(s.toString(),
0
,
0
, mPaint);
canvas.drawBitmap(bitmap,
0
,
0
, mPaint);
}
}