1、DrawView类
public class DrawView extends View {
/**
* 构造方法
*/
public DrawView(Context context, AttributeSet attrs) {
super(context, attrs);
}
/**
* 重写onDraw()方法
*/
@Override
protected void onDraw(Canvas canvas) {
Paint paint = new Paint();//定义一个采集默认设置的画笔
paint.setColor(Color.RED);//设置颜色为红色
paint.setShadowLayer(2, 3, 3, Color.rgb(180, 180, 180));//设置阴影
canvas.drawRect(40, 40,200,100,paint);//绘制矩形
super.onDraw(canvas);
}
}
2、布局文件
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<com.example.android9_2.DrawView
android:id="@+id/drawView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
</FrameLayout>