canvas教程

Android studio 继承view 画笔画圆、画方形、画三角形、画扇形、画椭圆

字号+ 作者:H5之家 来源:H5之家 2017-10-08 08:02 我要评论( )

Android studio 继承view 画笔画圆、画方形、画三角形、画扇形、画椭圆

MainActivitypublic class MainActivity extends AppCompatActivity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); }}

自定义代码

Zidingypublic class Zidingy extends View { private Paint mPaint; Context mContext; public Zidingy(Context context) { super(context); } public Zidingy(Context context, AttributeSet attrs) { super(context, attrs); } public Zidingy(Context context, AttributeSet attrs, int defStyleAttr) { super(context, attrs, defStyleAttr); this.mContext=context; } /*测量*/ @Override protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) { super.onMeasure(widthMeasureSpec, heightMeasureSpec); } /*绘制*/ @Override protected void onDraw(Canvas canvas) { /*画图*/ /*初始化画笔 文字*/ super.onDraw(canvas); Paint pa = new Paint(); /*圆*/ int we=canvas.getWidth()/2; int he=canvas.getHeight()/2; canvas.drawColor(Color.WHITE); pa.setColor(Color.RED); canvas.drawCircle(we,he,200,pa); pa.setColor(Color.WHITE); canvas.drawCircle(we,he,150,pa); pa.setColor(Color.YELLOW); canvas.drawCircle(we,he,100,pa); pa.setColor(Color.BLUE); canvas.drawText("武晓瑞",we-17,he,pa); //------------------------------------------------------------------- // 创建画笔(正方形) Paint p = new Paint(); //设置实心 p.setStyle(Paint.Style.FILL); // 设置红色 p.setColor(Color.BLACK); // 设置画笔的锯齿效果 p.setAntiAlias(true); //绘制// canvas.drawRect(50, 100, 300, 300, p);//(左、上、右、下) canvas.drawRect(10,150,300,300,p);// canvas.drawText("我是你的小可爱",wi-40,hi,pa); //------------------------------------------------------------------- /*三角形*/ Paint p1 = new Paint(); p1.setColor(Color.BLACK); //实例化路径 Path path = new Path(); path.moveTo(80, 200);// 此点为多边形的起点 path.lineTo(120, 250); path.lineTo(80, 250); path.close(); // 使这些点构成封闭的多边形 canvas.drawPath(path, p1); //------------------------------------------------------------------- /*扇形——SectorActivity*/ // 创建画笔 Paint p2 = new Paint(); p2.setColor(Color.RED); RectF rectF = new RectF(60, 100, 200, 240); canvas.drawArc(rectF, 200, 130, true, p2); //------------------------------------------------------------------- /*椭圆——OvalActivity*/ // 创建画笔 Paint p3 = new Paint(); p3.setColor(Color.GREEN); RectF rectF1 = new RectF(60, 100, 200, 240); rectF1.set(210,100,250,130); canvas.drawOval(rectF1, p3); } /*定位*/ @Override protected void onLayout(boolean changed, int left, int top, int right, int bottom) { super.onLayout(changed, left, top, right, bottom); } /*监听事件*/ @Override public boolean onTouchEvent(MotionEvent event) { return super.onTouchEvent(event); } public Zidingy(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) { super(context, attrs, defStyleAttr, defStyleRes); }}布局代码xml文件<?xml version="1.0" encoding="utf-8"?><RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:id="@+id/activity_main" android:layout_width="match_parent" android:layout_height="match_parent" android:paddingBottom="@dimen/activity_vertical_margin" android:paddingLeft="@dimen/activity_horizontal_margin" android:paddingRight="@dimen/activity_horizontal_margin" android:paddingTop="@dimen/activity_vertical_margin" tools:context="com.bwie.test.huayuan.MainActivity"> <com.bwie.test.huayuan.Zidingy android:layout_width="wrap_content" android:layout_height="wrap_content" /></RelativeLayout>

 

1.本站遵循行业规范,任何转载的稿件都会明确标注作者和来源;2.本站的原创文章,请转载时务必注明文章作者和来源,不尊重原创的行为我们将追究责任;3.作者投稿可能会经我们编辑修改或补充。

相关文章
  • android课程:canvas画图 切割画布(clipRect)

    android课程:canvas画图 切割画布(clipRect)

    2017-10-07 09:00

  • Android学习三、SurfaceView的学习

    Android学习三、SurfaceView的学习

    2017-10-03 15:53

  • android图像绘制(三)画布刷屏问题记录

    android图像绘制(三)画布刷屏问题记录

    2017-10-03 14:08

  • android - canvas 画线

    android - canvas 画线

    2017-09-21 10:01

网友点评
c