自定义App头像背景圆框

来源:互联网 发布:淘宝民族风女装品牌大全 编辑:程序博客网 时间:2024/04/30 07:49

import android.content.Context;
import android.graphics.Paint;
import android.graphics.RectF;
import android.util.AttributeSet;
import android.view.View;

/**
* Created by JiaHua on 2016/4/14.
* 自定义view 圆
*/
public class RoundProgressBar extends View {
private int mRatio;

/** * view的宽度 */private int mWidth;/** * view的高度 */private int mHeight;/** * 圆弧的中心位置 */private int mArcCenterX;/** * 圆弧的中心位置 */private int mArcCenterY;/** * 外接矩形 */private RectF mArcRect;/** * 对应画笔的宽度 */private float mArcWidth;/** * 对应画笔的宽度 */private float mBarWidth;/** * 画笔 */private Paint mArcPaint;public RoundProgressBar(Context context) {    super(context);}public RoundProgressBar(Context context, AttributeSet attrs) {    super(context, attrs);}public RoundProgressBar(Context context, AttributeSet attrs, int defStyleAttr) {    super(context, attrs, defStyleAttr);}@Overrideprotected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {    int defaultWidth = Integer.MAX_VALUE;    int width;    int height;    int widthMode = MeasureSpec.getMode(widthMeasureSpec);    int widthSize = MeasureSpec.getSize(widthMeasureSpec);    if (widthMode == MeasureSpec.EXACTLY || widthMode == MeasureSpec.AT_MOST) {        width = widthSize;    } else {        width = defaultWidth;    }    int defaultHeight = (int) (width * 1.f / mRatio);    height = defaultHeight;    setMeasuredDimension(width, height);}@Overrideprotected void onSizeChanged(int w, int h, int oldw, int oldh) {    super.onSizeChanged(w, h, oldw, oldh);    mWidth = w;    mHeight = h;    mArcCenterX = (int) (mWidth / 2.f);    mArcCenterY = (int) (160.f / 525.f * mHeight);    mArcRect = new RectF();    mArcRect.left = mArcCenterX - 125.f / 450.f * mWidth;    mArcRect.top = mArcCenterY - 125.f / 525.f * mHeight;    mArcRect.right = mArcCenterX + 125.f / 450.f * mWidth;    mArcRect.bottom = mArcCenterY + 125.f / 525.f * mHeight;    mArcWidth = 20.f / 450.f * mWidth;    mBarWidth = 16.f / 450.f * mWidth;

// 画笔的宽度设置自适应
mArcPaint.setStrokeWidth(mArcWidth);

}

}

0 0