Android -自定义View之简单绘制验证码

自定义属性



    
    
    
    
        
        
        
    

自定义view部分

public class MyView extends View {
    private String mText;
    private int mColor;
    private int mTextSize;
    private Rect mBound;
    private Paint mPaint;

    public MyView(Context context) {
        this(context, null);
    }

    public MyView(Context context, @Nullable AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public MyView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, 0);
        TypedArray array = context.getTheme().obtainStyledAttributes(attrs, R.styleable.MyView, defStyleAttr, 0);
        int n = array.getIndexCount();
        for (int i = 0; i < n; i++) {
            int attr = array.getIndex(i);
            switch (attr) {
                case R.styleable.MyView_text:
                    mText = array.getString(attr);
                    break;
                case R.styleable.MyView_textColor:
                    mColor = array.getColor(attr, Color.BLACK);
                    break;
                case R.styleable.MyView_textSize:
                    mTextSize = array.getDimensionPixelSize(attr, (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_SP, 16, getResources().getDisplayMetrics()));
                    break;
            }
        }
        array.recycle();

        mPaint = new Paint();
        mPaint.setTextSize(mTextSize);

        mBound = new Rect();
        mPaint.getTextBounds(mText, 0, mText.length(), mBound);

        this.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                mText = reandomText();
                postInvalidate();
            }
        });

    }

    private String reandomText() {
        Random random = new Random();
        Set set = new HashSet<>();
        while (set.size() < 4) {
            int randomInt = random.nextInt(10);
            set.add(randomInt);
        }
        StringBuilder stringBuilder = new StringBuilder();
        for (Integer i : set) {
            stringBuilder.append(i);
        }
        return stringBuilder.toString();
    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        int widthMode = MeasureSpec.getMode(widthMeasureSpec);
        int widthSize = MeasureSpec.getSize(widthMeasureSpec);
        int heightMode = MeasureSpec.getMode(heightMeasureSpec);
        int heightSize = MeasureSpec.getSize(heightMeasureSpec);
        int width;
        int height;
        if (widthMode == MeasureSpec.EXACTLY) {
            width = widthSize;
        } else {
            mPaint.setTextSize(mTextSize);
            mPaint.getTextBounds(mText, 0, mText.length(), mBound);
            float textWidth = mBound.width() + 100;
            width = (int) (getPaddingLeft() + textWidth + getPaddingRight());
        }

        if (heightMode == MeasureSpec.EXACTLY) {
            height = heightSize;
        } else {
            mPaint.setTextSize(mTextSize);
            mPaint.getTextBounds(mText, 0, mText.length(), mBound);
            float textheight = mBound.height() + 100;
            height = (int) (getPaddingTop() + textheight + getPaddingBottom());
        }
        setMeasuredDimension(width, height);
    }

    @Override
    protected void onDraw(Canvas canvas) {
        mPaint.setColor(Color.YELLOW);
        canvas.drawRect(0, 0, getMeasuredWidth(), getMeasuredHeight(), mPaint);
        mPaint.setColor(mColor);
        canvas.drawText(mText, (float) (getWidth() / 2 - mBound.width() / 2), (float) (getHeight() / 2 + mBound.height() / 2), mPaint);
        for (int a = 0; a < 5; a++) {
            drawLine(canvas, mPaint);
        }

        for (int a = 0; a < 100; a++) {
            drawPoint(canvas, mPaint);
        }
    }

    private void drawLine(Canvas canvas, Paint paint) {
        Random mRandom = new Random();
        int color = randomColor();
        int startX = mRandom.nextInt(getMeasuredWidth());
        int startY = mRandom.nextInt(getMeasuredHeight());
        int stopX = mRandom.nextInt(getMeasuredWidth());
        int stopY = mRandom.nextInt(getMeasuredHeight());
        paint.setStrokeWidth(4);
        paint.setColor(colo![图片.png](https://upload-images.jianshu.io/upload_images/12573796-ba160eb1c5db57b5.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)
r);
        canvas.drawLine(startX, startY, stopX, stopY, paint);
    }

    private void drawPoint(Canvas canvas, Paint paint){
        Random random =new Random();
        int color =randomColor();
        float x =random.nextInt(getMeasuredWidth());
        float y =random.nextInt(getMeasuredHeight());
        paint.setColor(color);
        paint.setStrokeWidth(4);
        canvas.drawPoint(x,y,paint);
    }

    private int randomColor() {
        Random mRandom = new Random();
        StringBuilder stringBuilder = new StringBuilder();
        for (int i = 0; i < 8; i++) {
            int a = mRandom.nextInt(0xf);
            stringBuilder.append(Integer.toHexString(a));
        }
        Log.e(TAG, "randomColor: "+stringBuilder );
        return Color.parseColor("#" + stringBuilder);
    }

}

布局




    

在MainActivity中调用

findViewById(R.id.myview);

最后的效果


image

你可能感兴趣的:(Android -自定义View之简单绘制验证码)