gpt4 book ai didi

android - 自定义 View 仅在 XML 预览中未正确对齐

转载 作者:行者123 更新时间:2023-11-29 01:12:23 25 4
gpt4 key购买 nike

我有一个自定义 View (一个圆圈),它在应用程序运行时正确呈现。但是,在 XML 预览 Pane 中查看时, View 未正确对齐。

下面是对齐不正确的预览 Pane 的屏幕截图。

enter image description here

下面是同一 View 在运行的应用程序上绘制时正确对齐的屏幕截图。

enter image description here

这是定义自定义 View 的代码 -

public class LinearTimerView extends View {

private Paint arcPaint;
private RectF rectF;

private int initialColor;
private int progressColor;
private int circleRadiusInDp;

// The point from where the color-fill animation will start.
private int startingAngle = 270;

// The point up-till which user wants the circle to be pre-filled.
private float preFillAngle;

public LinearTimerView(Context context,
AttributeSet attrs) {
super(context, attrs);

TypedArray typedArray = getContext().obtainStyledAttributes(attrs,
R.styleable.LinearTimerView);

// Retrieve the view attributes.
this.circleRadiusInDp =
(int) typedArray.getDimension(R.styleable.LinearTimerView_radius, 5);
int strokeWidthInDp =
(int) typedArray.getDimension(R.styleable.LinearTimerView_strokeWidth, 2);
this.initialColor =
typedArray.getColor(R.styleable.LinearTimerView_initialColor,
ContextCompat.getColor(getContext(), R.color.colorInitial));
this.progressColor =
typedArray.getColor(R.styleable.LinearTimerView_progressColor,
ContextCompat.getColor(getContext(), R.color.colorProgress));
this.startingAngle =
typedArray.getInt(R.styleable.LinearTimerView_startingPoint, 270);

// Define the size of the circle.
rectF = new RectF(
(int) convertDpIntoPixel(strokeWidthInDp),
(int) convertDpIntoPixel(strokeWidthInDp),
(int) convertDpIntoPixel(circleRadiusInDp * 2)
+ (int) convertDpIntoPixel(strokeWidthInDp),
(int) convertDpIntoPixel(circleRadiusInDp * 2)
+ (int) convertDpIntoPixel(strokeWidthInDp));

arcPaint = new Paint();
arcPaint.setAntiAlias(true);
arcPaint.setStyle(Paint.Style.STROKE);
arcPaint.setStrokeWidth((int) convertDpIntoPixel(strokeWidthInDp));

typedArray.recycle();
}

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

try {
// Grey Circle - This circle will be there by default.
arcPaint.setColor(initialColor);
canvas.drawCircle(rectF.centerX(), rectF.centerY(),
(int) convertDpIntoPixel(circleRadiusInDp), arcPaint);

// Green Arc (Arc with 360 angle) - This circle will be animated as time progresses.
arcPaint.setColor(progressColor);
canvas.drawArc(rectF, startingAngle, preFillAngle, false, arcPaint);
} catch (NullPointerException ex) {
ex.printStackTrace();
}
}

/**
* Method to get the degrees up-till which the arc is already pre-filled.
* @return
*/
public float getPreFillAngle() {
return preFillAngle;
}

public void setPreFillAngle(float preFillAngle) {
this.preFillAngle = preFillAngle;
}

/**
* Method to get the starting point of the angle
* @return
*/
public int getStartingPoint() {
return startingAngle;
}

public void setStartingPoint(int startingPointInDegrees) {
this.startingAngle = startingPointInDegrees;
}

/**
* Method to convert DPs into Pixels.
*/
private float convertDpIntoPixel(float dp) {
float scale = getResources().getDisplayMetrics().density;
return dp * scale + 0.5f;
} }

我该如何解决这个问题?

最佳答案

事实证明我没有覆盖 onMeasure() 方法。

我采用了提示形式 this回答并能够解决问题。

关于android - 自定义 View 仅在 XML 预览中未正确对齐,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42020925/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com