Java 如何在自定义视图中使用的android画布中居中任何自定义路径(形状)?
我使用pathClass创建了一个自定义的圆形方形形状,然后在自定义视图的画布中绘制自定义路径。当我给视图增加更多的高度时,比如说300高和100宽,但是当宽度更大时,它会转到顶部而不是视图的中心,自定义形状会转到左侧。我不知道问题是什么,我认为问题出在x,y。以下是路径代码:Java 如何在自定义视图中使用的android画布中居中任何自定义路径(形状)?,java,android,canvas,path,android-custom-view,Java,Android,Canvas,Path,Android Custom View,我使用pathClass创建了一个自定义的圆形方形形状,然后在自定义视图的画布中绘制自定义路径。当我给视图增加更多的高度时,比如说300高和100宽,但是当宽度更大时,它会转到顶部而不是视图的中心,自定义形状会转到左侧。我不知道问题是什么,我认为问题出在x,y。以下是路径代码: public static Path roundRectSquare(int height,int width, float topLeftRadius,float t
public static Path roundRectSquare(int height,int width,
float topLeftRadius,float topRightRadius,
float bottomRightRadius,float bottomLeftRadius){
Path path = new Path();
RectF bl = new RectF(0, height -2* bottomLeftRadius,2* bottomLeftRadius, height);
RectF br = new RectF(width -2* bottomRightRadius, height -2* bottomRightRadius, width, height);
RectF tl = new RectF(0,0,2* topLeftRadius,2* topLeftRadius);
RectF tr = new RectF(width -2* topRightRadius,0, width,2* topRightRadius);
path.reset();
path.moveTo(x + topLeftRadius, y);
path.lineTo(width - topRightRadius, y);
path.arcTo(tr,-90,90);
path.lineTo(width, y + topRightRadius);
path.lineTo(width, height - bottomRightRadius);
path.arcTo(br,0,90);
path.lineTo(width - bottomRightRadius, height);
path.lineTo(x + bottomLeftRadius, height);
path.arcTo (bl,90,90);
path.lineTo(x, height - bottomLeftRadius);
path.lineTo(x,y + topLeftRadius);
path.arcTo(tl,90,180);
path.close();
return path;
}
路径中的代码x和y是getWidth/2和getHeight/2
我在自定义视图绘制方法代码中使用了此路径:
@Override
public void draw(Canvas canvas) {
path = new Path();
boxPaint =new Paint();
int side = Math.min(this.getHeight(),this.getWidth());
boxPaint.setStyle(Paint.Style.FILL);
boxPaint.setColor(Color.YELLOW);
path = roundRectSquare(side,side,30,20,50,10);
canvas.drawPath(path,boxPaint);
super.draw(canvas);
}
那么有没有办法解决这个问题呢