import android.content.Context; import android.graphics.Canvas; import android.graphics.Color; import android.graphics.Paint; import android.util.AttributeSet; import android.view.View; /** * Created by Liqf on 2017.09.19. */ public class LView1 extends View { //继承View private Paint paint; public LView1(Context context) { super(context); } public LView1(Context context, AttributeSet attrs) { super(context, attrs); } @Override protected void onDraw(Canvas canvas) { super.onDraw(canvas); paint = new Paint();// new一个画笔 paint.setColor(Color.YELLOW);// 设置画笔颜色 paint.setStyle(Paint.Style.FILL);// 设置画笔填充 canvas.drawCircle(100, 100, 100, paint);// 用画笔在画布上添加一个圆,不只可以添加圆,还可以添加矩形等! paint.setColor(Color.YELLOW);// 设置画笔颜色 canvas.drawText("第二个圆", 40, 50, paint);// 用画笔在画布上添加文字,中间两个参数对应的是坐标。 } } XML布局文件<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="match_parent" android:layout_height="match_parent" android:background="@mipmap/t01d22730de0fe9fc88" > <com.example.mydemo_18.LView android:layout_width="wrap_content" android:layout_height="wrap_content" ></com.example.mydemo_18.LView> </LinearLayout>
09-07
09-07
09-07