码迷,mamicode.com
首页 > 移动开发 > 详细

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

时间:2016-05-12 19:32:02      阅读:754      评论:0      收藏:0      [点我收藏+]

标签:

不用图片,做一个简单的印章资源,用自定义控件来实现。
效果图:
技术分享


分析:

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

2、drawTextOnPath来画文字。

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


代码:

attrs.xml

<attr name="seal_text" format="string" />
    <attr name="seal_textSize" format="dimension" />
    <attr name="seal_textColor" format="color" />

    <declare-styleable name="Customer_Seal" >
        <attr name="seal_text" />
        <attr name="seal_textSize" />
        <attr name="seal_textColor" />
    </declare-styleable>

自定义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<n;i++){
            int attr = a.getIndex(i);
            switch (attr){
                case R.styleable.Customer_Seal_seal_text:
                    mTitle = a.getString(attr);
                    break;
                case R.styleable.Customer_Seal_seal_textColor:
                    //默认黑色
                    mTextColor = a.getColor(attr, Color.BLACK);
                    break;
                case R.styleable.Customer_Seal_seal_textSize:
                    mTextSize = a.getDimensionPixelSize(attr, (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_SP,
                            16, getResources().getDisplayMetrics()));
                    break;
            }
        }
        a.recycle();
        mPaint = new Paint();
        mPaint.setTextSize(mTextSize);
    }

重写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 自定义控件--公司印章制作

标签:

原文地址:http://blog.csdn.net/chmj1208/article/details/51354700

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!