我已经创建了自己的 View 。它是一个半圆。我想让它对齐到屏幕底部。因为,我使用 drawArc 绘制它,它没有与底部对齐。我尝试过设置一个圆,中心的 y 坐标与屏幕底部坐标重合。我不想使用它,因为我希望为它实现一些额外的东西。这是我的自定义 View 代码。
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;
}
}
我想将半圆与屏幕底部对齐。我有什么想法可以做到吗?
最佳答案
首先感谢大家的回答。您的回答部分正确。是的,用
android:layout_alignParentBottom="true"
会成功一半。假设我要画一个圆。在自定义 View 中,(0,0) 表示 View 最左边的坐标,而不是屏幕的坐标。假设有人想要半圆位于屏幕底部,就像我想做的那样,坐标猜测结果是一个问题。这是我实际解决它的方法。
px = getMeasuredWidth()/2;
py = getMeasuredHeight();
canvas.drawCircle(px, py, radius, paint);
此处,px
将是整个 View 宽度的中心, View 宽度的中点,py
将是分配给 View 的最后一个屏幕 y 轴坐标。
希望它对其他人也有帮助。
关于android - 将自定义 View 与屏幕底部对齐,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15592937/