在命运之轮中旋转文本-Android

时间:2018-08-09 16:39:41

标签: android

我正在使用this library为我的应用发财。我已经编辑了一些东西以使其看起来像我想要的,但是有些我无法实现。

我有这个:看文字方向 enter image description here

我想要这个: 看文字方向

enter image description here

我无法像第二张图片一样旋转文本值。

这是我认为用于绘制文本的类:

public class PielView extends View {

private RectF mRange = new RectF();
private int mRadius;

private Paint mArcPaint;
private Paint mBackgroundPaint;
private Paint mTextPaint;

private float mStartAngle = 0;
private int mCenter;
private int mPadding;
private int mTargetIndex;
private int mRoundOfNumber = 4;
private boolean isRunning = false;

private int defaultBackgroundColor = -1;
private Drawable drawableCenterImage;
private int textColor = 0xffffffff;


private void init() {
    mArcPaint = new Paint();
    mArcPaint.setAntiAlias(true);
    mArcPaint.setDither(true);

    mTextPaint = new Paint();
    mTextPaint.setColor(textColor);
    mTextPaint.setTextSize(TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_SP,14,
            getResources().getDisplayMetrics()));

    mRange = new RectF(mPadding, mPadding, mPadding+mRadius, mPadding+mRadius);
}


private void drawText(Canvas canvas, float tmpAngle, float sweepAngle, String mStr) {
    Path path = new Path();
    path.addArc(mRange,tmpAngle,sweepAngle);

    float textWidth = mTextPaint.measureText(mStr);
    int hOffset = (int) (mRadius * Math.PI / mLuckyItemList.size()/2-textWidth/2);

    int vOffset = mRadius/2/4;

    canvas.drawTextOnPath(mStr, path, hOffset, vOffset, mTextPaint);
}

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

    if (mLuckyItemList == null) {
        return;
    }

    drawBackgroundColor(canvas, defaultBackgroundColor);

    init();

    float tmpAngle = mStartAngle;
    float sweepAngle = 360 / mLuckyItemList.size();

    for(int i = 0; i < mLuckyItemList.size(); i++) {
        mArcPaint.setColor(mLuckyItemList.get(i).color);
        canvas.drawArc(mRange, tmpAngle, sweepAngle, true, mArcPaint);

        drawText(canvas, tmpAngle, sweepAngle, mLuckyItemList.get(i).text);
        drawImage(canvas, tmpAngle, BitmapFactory.decodeResource(getResources(), mLuckyItemList.get(i).icon));

        tmpAngle += sweepAngle;
      }  
   }
}

0 个答案:

没有答案