首页 > 系统 > Android > 正文

Android图形绘制之——简单的几何图形

2019-11-06 09:53:00
字体:
来源:转载
供稿:网友

1.自定义view

2.重写onDraw()方法

3.xml中引用:

自定义view代码:

public class MyView extends View{    public MyView(Context context, AttributeSet attrs) {        super(context, attrs);    }    /**     * 重写绘制方法     */    @Override    PRotected void onDraw(Canvas canvas) {        super.onDraw(canvas);        canvas.drawColor(Color.BLACK);//绘制黑色背景        Paint paint = new Paint();//创建画笔        paint.setColor(Color.RED);//设置画笔颜色为红色        canvas.drawRect(10, 10, 110, 110, paint);//绘制矩形        canvas.drawText("这是字符串", 10, 130, paint);//字符串,以字符串下面为基准        RectF rf1 = new RectF(10,130,110,230);//定义一个矩形        canvas.drawArc(rf1, 0, 45, true, paint);//画弧,顺时针        canvas.drawLine(150, 10, 250, 110, paint);//画线        RectF rf2 = new RectF(150,130,250,230);//定义一个矩形        canvas.drawOval(rf2,paint);//根据矩形画一个圆    }}XML中引用:

<?xml version="1.0" encoding="utf-8"?><LinearLayout    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"    android:orientation="vertical"    tools:context=".ui.activity.MyViewActivity">    <testku.mygame.ui.myview.MyView        android:layout_width="match_parent"        android:layout_height="match_parent"/>    </LinearLayout>

效果图:


发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表