标签:
SelfView.class
package com.bwie.view; import android.content.Context; import android.graphics.Canvas; import android.graphics.Color; import android.graphics.Paint; import android.util.AttributeSet; import android.widget.TextView; public class SelfView extends TextView { public SelfView(Context context) { super(context); // TODO Auto-generated constructor stub } public SelfView(Context context, AttributeSet attrs) { super(context, attrs); // TODO Auto-generated constructor stub } public SelfView(Context context, AttributeSet attrs, int defStyle) { super(context, attrs, defStyle); // TODO Auto-generated constructor stub } @Override public void onDraw(Canvas canvas) { // TODO Auto-generated method stub // 创建一个画笔 Paint paint = new Paint(); // 设置画笔的颜色为蓝绿色 paint.setColor(Color.CYAN); // 设置画笔的字体大小 paint.setTextSize(25); // 设置画笔的线条宽度 paint.setStrokeWidth(20); // 画板为灰色 canvas.drawColor(Color.GRAY); //画一条直线 canvas.drawLine(20, 280, 280, 280, paint);//1.开始X 2.开始Y 3.结束X 4.结束Y. 5.画笔 //画文字 canvas.drawText("我是被自定义出来的", 10, 250, paint);//1.显示文字 2.开始X 3.开始Y 4.画笔 //画圆 canvas.drawCircle(80, 80, 80, paint);//1.圆心X 2.圆心Y 3.半径 4.画笔 super.onDraw(canvas); } @Override protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) { // TODO Auto-generated method stub setMeasuredDimension(300, 300);//设置画板大小 } }
xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" tools:context=".MainActivity" > <com.bwie.view.SelfView android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_centerInParent="true" /> </RelativeLayout>
标签:
原文地址:http://www.cnblogs.com/ldou/p/5369812.html