package com.activity.liangweihang_yizhou; import android.content.Context; import android.graphics.Canvas; import android.graphics.Color; import android.graphics.Paint; import android.graphics.Path; import android.util.AttributeSet; import android.widget.RelativeLayout; public class MyView extends RelativeLayout { public MyView(Context context) { this(context,null); } public MyView(Context context, AttributeSet attrs, int defStyleAttr) { super(context, attrs, defStyleAttr); } public MyView(Context context, AttributeSet attrs) { this(context, attrs,0); } protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) { /* int widthMode = MeasureSpec.getMode(widthMeasureSpec); int widthSize = MeasureSpec.getSize(widthMeasureSpec); int hightMode = MeasureSpec.getMode(heightMeasureSpec); int hightSize = MeasureSpec.getSize(heightMeasureSpec); int width,hight; if (widthMode==MeasureSpec.EXACTLY){ if (widthSize<DensityUtil.dip2px(getContext(),50 )) widthSize=DensityUtil.dip2px(getContext(),50 ); } if (hightMode==MeasureSpec.EXACTLY){ if (hightSize<DensityUtil.dip2px(getContext(),50 )) hightSize=DensityUtil.dip2px(getContext(),50 ); } width=MeasureSpec.makeMeasureSpec(widthSize,widthMode); hight=MeasureSpec.makeMeasureSpec(hightSize,hightMode);*/ super.onMeasure(widthMeasureSpec, heightMeasureSpec); } protected void onDraw(Canvas canvas) { super.onDraw(canvas); Paint mpaint=new Paint(); mpaint.setColor(Color.YELLOW); mpaint.setStyle(Paint.Style.FILL); canvas.drawCircle(getWidth()/2,getHeight()/2,155,mpaint); Paint zpaint=new Paint(); // 绘制这个三角形,你可以绘制任意多边形 Path path = new Path(); path.moveTo(getWidth()/2, (getHeight()/2)-155);// 此点为多边形的起点 path.lineTo((getWidth()/2)-137, ((getHeight()/2)+(155/2))); path.lineTo((getWidth()/2)+137, ((getHeight()/2)+(155/2))); path.close(); // 使这些点构成封闭的多边形 canvas.drawPath(path, zpaint); } } //布局文件<com.activity.liangweihang_yizhou.MyView android:background="#ff55" android:layout_width="300dp" android:layout_height="300dp" android:layout_centerVertical="true" android:layout_centerHorizontal="true" />
自定义圆内套三角形
最新推荐文章于 2022-09-24 23:02:06 发布