android 自定义控件--公司印章制作

不用图片,做一个简单的印章资源,用自定义控件来实现。
效果图:
android 自定义控件--公司印章制作_第1张图片


分析:

1、画两个同心圆,外圆用红色来画,内圆用父控件背景颜色来画。

2、drawTextOnPath来画文字。

3、在内圆里面用path.lineTo()来画五角星。


代码:

attrs.xml


    
    

    
        
        
        
    

自定义View CustomerSeal 的构造函数:

public CustomerSeal(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);

        //获取系统默认的背景颜色
        TypedArray array = context.getTheme().obtainStyledAttributes(new int[] {
                android.R.attr.colorBackground,
                android.R.attr.textColorPrimary,
        });
        mBackgroundColor =array.getColor(0, 0xFF00FF);


        TypedArray a = context.getTheme().obtainStyledAttributes(attrs, R.styleable.Customer_Seal, defStyleAttr, 0);
        int n = a.getIndexCount();
        for(int i=0;i

重写onDtraw()方法:

@Override
    protected void onDraw(Canvas canvas) {
//        super.onDraw(canvas);

        // 得到画布宽高
        mWidth = getWidth();
        mHeight = getHeight();

        mPaint.setStrokeWidth (5);//设置画笔宽度
        mPaint.setAntiAlias(true); //指定是否使用抗锯齿功能,如果使用,会使绘图速度变慢
        mPaint.setColor(Color.RED);
        mPaint.setStyle(Paint.Style.STROKE);//绘图样式,设置为填充

        //画圆
        Path circlePath = new Path();
        circlePath.addCircle(mWidth/2, mHeight/2, 180, Path.Direction.CW);  //圆心x坐标,y坐标,半径,方向

        canvas.drawPath(circlePath, mPaint);

        mPaint.setStrokeWidth((float) 0.0);//设置画笔宽度
        mPaint.setColor(mBackgroundColor);//设置画笔宽度

        //在画一个圆
        Path textCirclePath = new Path();
        textCirclePath.addCircle(mWidth / 2, mHeight / 2, 100, Path.Direction.CW);  //圆心x坐标,y坐标,半径,方向
        canvas.drawPath(textCirclePath, mPaint);

        mPaint.setStrokeWidth(0);//设置画笔宽度
        mPaint.setColor(mTextColor);
        mPaint.setStyle(Paint.Style.FILL);


        //写字
        canvas.drawTextOnPath(mTitle,textCirclePath,250,-35,mPaint);

        //(0,0),(mWidth / 2 - radius,mHeight/2-radius)

        int radius = 80;
        int startX = mWidth/2-radius;  //起点x位置
        int startY = mHeight/2-radius; //起点y位置
        //画五角星
        Path path = new Path();
        float radian = degree2Radian(DEGREE);
        float radius_in = (float) (radius * Math.sin(radian / 2) / Math.cos(radian)); //中间五边形的半径

        path.moveTo((float) (radius * Math.cos(radian / 2)+startX), startY);
        path.lineTo((float) (radius * Math.cos(radian / 2) + radius_in * Math.sin(radian) + startX), (float) (radius - radius * Math.sin(radian / 2)+startY));
        path.lineTo((float) (radius * Math.cos(radian / 2) * 2 + startX), (float) (radius - radius * Math.sin(radian / 2) + startY));
        path.lineTo((float) (radius * Math.cos(radian / 2) + radius_in * Math.cos( radian /2)+startX),(float) (radius + radius_in * Math.sin( radian /2)+startY));
        path.lineTo((float) (radius * Math.cos(radian / 2) + radius * Math.sin(radian)+startX), (float) (radius + radius * Math.cos(radian)+startY));
        path.lineTo((float) (radius * Math.cos(radian / 2)+startX), (float) (radius + radius_in+startY));
        path.lineTo((float) (radius * Math.cos(radian / 2) - radius * Math.sin(radian)+startX), (float) (radius + radius * Math.cos(radian)+startY));
        path.lineTo((float) (radius * Math.cos(radian / 2) - radius_in * Math.cos( radian /2)+startX), (float) (radius + radius_in * Math.sin(radian / 2)+startY));
        path.lineTo(startX, (float) (radius - radius * Math.sin(radian /2)+startY));
        path.lineTo((float) (radius * Math.cos(radian / 2) - radius_in * Math.sin(radian)+startX) , (float) (radius - radius * Math.sin(radian /2)+startY));
        path.close();
        canvas.drawPath(path, mPaint);
    }

角度转圆弧的方法

/**
     * 角度转弧度
     * @param degree
     * @return
     */
    private float degree2Radian(float degree){
        return (float) (Math.PI * degree / 180);
    }


源代码


你可能感兴趣的:(Android)