我创造了我自己的观点。它是一个半圆。我希望它与屏幕底部对齐。因为,我使用 drawArc 绘制它,它没有与底部对齐。我尝试过设置一个中心的y坐标与屏幕底部坐标重合的圆的替代方法。我不希望使用它,因为我希望实现一些额外的东西。这是我的自定义视图代码。
package legacy_systems.customview;
import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.graphics.RectF;
import android.util.AttributeSet;
import android.view.View;
public class CircleView extends View{
private Paint markerPaint;
private Paint textPaint;
private Paint circlePaint;
private int textHeight;
public CircleView(Context context)
{
super(context);
initCircleView();
}
public CircleView(Context context, AttributeSet attrs)
{
super(context, attrs);
initCircleView();
}
public CircleView(Context context, AttributeSet attrs, int defaultStyle)
{
super(context, attrs, defaultStyle);
initCircleView();
}
protected void initCircleView()
{
setFocusable(true);
circlePaint = new Paint(Paint.ANTI_ALIAS_FLAG);
circlePaint.setColor(getResources().getColor(android.R.color.black));
circlePaint.setStrokeWidth(3);
circlePaint.setStyle(Paint.Style.STROKE);
markerPaint = new Paint(Paint.ANTI_ALIAS_FLAG);
markerPaint.setColor(getResources().getColor(android.R.color.darker_gray));
}
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec)
{
int measuredWidth = measure(widthMeasureSpec);
int measuredHeight = measure(heightMeasureSpec);
int d = Math.min(measuredWidth, measuredHeight);
setMeasuredDimension(d,d);
}
@Override
protected void onDraw(Canvas canvas) {
// TODO Auto-generated method stub
int px, py;
px = getMeasuredWidth()/2;
py = getMeasuredHeight()/2;
int radius = Math.min(px, py);
RectF oval = new RectF();
oval.set(px-radius, py-radius, px+radius, py+radius);
canvas.drawArc(oval,180,180,false, circlePaint);
//canvas.drawCircle(px, py, radius, circlePaint);
canvas.save();
}
private int measure(int measureSpec)
{
int result = 0;
int specMode = MeasureSpec.getMode(measureSpec);
int specSize = MeasureSpec.getSize(measureSpec);
if(specMode == MeasureSpec.UNSPECIFIED)
{
result = 200;
}
else
{
result = specSize;
}
return result;
}
}
我想将半圆对齐到屏幕底部。任何想法我该怎么做?