自定义圆内套三角形

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" />

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值