新建一个类,继承于view
核心代码:
@Override
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);
canvas.save();
canvas.drawRect(0, 0, 120, 120, paint);
canvas.restore();
invalidate();
}
 
private Paint paint;
public void init() {
paint = new Paint();
paint.setColor(0x000ffff);
}
<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" >
<com.example.test_an_3.MyView
android:id="@+id/myView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</RelativeLayout>