如何创建由圆形进度条舍入的imageView

时间:2016-08-04 03:32:25

标签: android imageview android-progressbar custom-view

我正在尝试在圆形图像周围绘制圆形进度条。我试图创建自己的“CircleImageView”类,它扩展了“ImageView”

我的onDraw()方法

    protected void onDraw(Canvas canvas) {
        canvas.drawCircle(mCenter[0], mCenter[1], mRadius, ringPaint);

        progress = 30; //for testing 
        float angle = 360 * progress / maxProgress;
        canvas.drawArc(mOval, mStartAngle, angle, false, progressPaint);

        super.onDraw(canvas);
    }

onMeasure方法()

protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
    super.onMeasure(widthMeasureSpec, heightMeasureSpec);

    final int height = getDefaultSize(getSuggestedMinimumHeight(), heightMeasureSpec);
    final int width = getDefaultSize(getSuggestedMinimumWidth(), widthMeasureSpec);
    final int min = Math.min(width, height);  
    setMeasuredDimension(width, height);

    // Get image matrix values and place them in an array
    float[] f = new float[9];
    getImageMatrix().getValues(f);

    // Extract the scale values using the constants (if aspect ratio maintained, scaleX == scaleY)
    final float scaleX = f[Matrix.MSCALE_X];
    final float scaleY = f[Matrix.MSCALE_Y];

    // Get the drawable (could also get the bitmap behind the drawable and getWidth/getHeight)
    final Drawable d = getDrawable();
    final int origW = d.getIntrinsicWidth();
    final int origH = d.getIntrinsicHeight();

    // Calculate the actual dimensions
    final int actW = Math.round(origW * scaleX);
    final int actH = Math.round(origH * scaleY);

    Log.e("DBG", "[" + origW + "," + origH + "] -> [" + actW + "," + actH + "] & scales: x=" + scaleX + " y=" +
            scaleY);

    mCenter = new int[]{width / 2, height / 2}; 

    mRadius = (int) mCenter[0] - actW - progressStrokeWidth / 2;

    mOval.set(mCenter[0] - mRadius, mCenter[1] - mRadius, mCenter[0] + mRadius, mCenter[1] + mRadius);  //mOval is an instance of RectF
}

init()方法

private void init(Context context, AttributeSet attrs) {
    mContext = context;

    TypedArray typedArray = mContext.obtainStyledAttributes(attrs, R.styleable.CircleProgressAttr);
    progressStrokeWidth = typedArray.getInteger(R.styleable.CircleProgressAttr_progressRingSize, 20);
    mRingColor = typedArray.getInt(R.styleable.CircleProgressAttr_progressRingColor, R.color.ringColor);
    mProgressColor = typedArray.getInt(R.styleable.CircleProgressAttr_progressColor, R.color.tab_underline_color);

    mStartAngle = -90;
    maxProgress = 100;

    mOval = new RectF();
    ringPaint = new Paint();
    progressPaint = new Paint();

    ringPaint.setColor(mRingColor); 
    ringPaint.setStyle(Paint.Style.STROKE); 
    ringPaint.setStrokeWidth(progressStrokeWidth); 
    ringPaint.setAntiAlias(true);

    progressPaint.setColor(mProgressColor);
    progressPaint.setStyle(Paint.Style.STROKE);
    progressPaint.setStrokeWidth(progressStrokeWidth);
    progressPaint.setAntiAlias(true);
}

我已使用以下XML代码

将CircleView插入RelativeLayout
   <com.kevinwang.simpleplayer.CircleImageView
        android:id="@+id/cd_img"
        android:src="@drawable/cd01"
        android:layout_above="@id/play_layout"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:scaleType="center"/>

我运行了应用,但进度条没有显示,我的代码有什么问题吗?

0 个答案:

没有答案