Java Android中的自定义形状视图
我需要以编程方式构建一组自定义形状的视图。问题是,这些视图并非都是相同的形状,形状将根据我拥有的某些元素的数量来确定 假设我有3个元素:[“A”、“B”、“C”]视觉效果应如下所示: 如果有2个要素,则如下所示: 我在Swift中使用自定义视图中的一些自定义图形完成了这项工作,但我不确定java的语法。即使只是一个简单的例子,画一个形状类似将帮助我在正确的方向Java Android中的自定义形状视图,java,swift,drawing,shape,Java,Swift,Drawing,Shape,我需要以编程方式构建一组自定义形状的视图。问题是,这些视图并非都是相同的形状,形状将根据我拥有的某些元素的数量来确定 假设我有3个元素:[“A”、“B”、“C”]视觉效果应如下所示: 如果有2个要素,则如下所示: 我在Swift中使用自定义视图中的一些自定义图形完成了这项工作,但我不确定java的语法。即使只是一个简单的例子,画一个形状类似将帮助我在正确的方向 //Get points for bounding view //Pad = space between polyg
//Get points for bounding view
//Pad = space between polygons
//leftX = Left bound of parent view
//rightX = Right bound of parent view
//topY = Top bound of parent view
//bottomY = Bottom bound of parent view
//width = Width of a single polygon before transform
//peak = How far a polygon will reach into the space of another polygon after transform (1/3 polygon width)
//tip = peak with padding taken into account
var context = UIGraphicsGetCurrentContext()
var pad:CGFloat = (CGFloat)(Numbers.APPLICATIONPADDING)
var leftX:CGFloat = (CGFloat)(self.bounds.origin.x)
var rightX:CGFloat = (CGFloat)(self.bounds.size.width)
var topY:CGFloat = (CGFloat)(self.bounds.origin.y)
var bottomY:CGFloat = (CGFloat)(self.bounds.size.height)
var width:CGFloat = (CGFloat)(self.bounds.width) / (CGFloat)(applications.count)
var peak:CGFloat = width/3.0
var tip:CGFloat = peak - pad
//Draw all applications
var appNum:CGFloat = 1.0
var appCount:CGFloat = (CGFloat)(applications.count)
var endTopX:CGFloat = 0.0
var endBottomX:CGFloat = 0.0
var temp:CGFloat!
var textRightX:CGFloat = 0.0
var textLeftX:CGFloat = 0.0
var label:UILabel!
for app in applications{
if(app.color != nil){
CGContextSetFillColorWithColor(context, app.color!.CGColor)
}
else{
CGContextSetFillColorWithColor(context, Colors.PARTCOLORDEFAULT.CGColor)
}
//Each polygon will breach into the space of the next and the previous after transform by the tip with a given padding
//Each text field will stretch across the middle of each polygon with a small padding (3) to prevent overreaching
if(appNum == 1 && applications.count == 1){
CGContextMoveToPoint(context, leftX, topY)
CGContextAddLineToPoint(context, width, topY)
CGContextAddLineToPoint(context, width, bottomY)
CGContextAddLineToPoint(context, leftX, bottomY)
}
if(appNum == 1){
CGContextMoveToPoint(context, leftX, topY)
CGContextAddLineToPoint(context, width + tip, topY)
CGContextAddLineToPoint(context, width - peak, bottomY)
CGContextAddLineToPoint(context, leftX, bottomY)
textLeftX = leftX + (leftX - leftX) / 2 + 3
textRightX = (width - peak) + ((width + tip) - (width - peak)) / 2.0 - 10
endTopX = width + tip
endBottomX = width - peak
}
else if(appNum == appCount){
CGContextMoveToPoint(context, endTopX + pad, topY)
CGContextAddLineToPoint(context, rightX, topY)
CGContextAddLineToPoint(context, rightX, bottomY)
CGContextAddLineToPoint(context, endBottomX + pad, bottomY)
textLeftX = (endBottomX + pad) + ((endTopX + pad) - (endBottomX + pad)) / 2.0 + 10
textRightX = rightX + (rightX - rightX) / 2.0 - 10
endTopX = width
endBottomX = width
}
else{
CGContextMoveToPoint(context, endTopX + pad, topY)
CGContextAddLineToPoint(context, width * appNum + tip, topY)
CGContextAddLineToPoint(context, width * appNum - peak, bottomY)
CGContextAddLineToPoint(context, endBottomX + pad, bottomY)
textLeftX = (endBottomX + pad) + ((endTopX + pad) - (endBottomX + pad)) / 2 + 10
textRightX = (width * appNum - peak) + ((width * appNum + tip) - (width * appNum - peak)) / 2 - 3
endTopX = width * appNum + tip
endBottomX = width * appNum - peak
}
CGContextFillPath(context)
//Add text label
label = UILabel(frame: CGRectMake(textLeftX, topY, (textRightX-textLeftX), bottomY))
label.textAlignment = NSTextAlignment.Center
label.text = app.baseSeries
label.numberOfLines = 1
label.adjustsFontSizeToFitWidth = true
label.font = UIFont(name:"OpenSans-SemiBold", size:30)
self.addSubview(label)
appBounds.append(label.frame)
appNum++
}
self.layer.borderColor = UIColor.lightGrayColor().CGColor
self.layer.borderWidth = 1.0
我在自定义布局类的onDraw方法中使用以下代码实现了这一点。swift to代码在java代码块上方注释掉,这是它的等价部分
@Override
protected void onDraw(Canvas canvas){
//Get points for bounding view
//Pad = space between polygons
//leftX = Left bound of parent view
//rightX = Right bound of parent view
//topY = Top bound of parent view
//bottomY = Bottom bound of parent view
//width = Width of a single polygon before transform
//peak = How far a polygon will reach into the space of another polygon after transform (1/3 polygon width)
//tip = peak with padding taken into account
// var context = UIGraphicsGetCurrentContext()
// var pad:CGFloat = (CGFloat)(Numbers.APPLICATIONPADDING)
// var leftX:CGFloat = (CGFloat)(self.bounds.origin.x)
// var rightX:CGFloat = (CGFloat)(self.bounds.size.width)
// var topY:CGFloat = (CGFloat)(self.bounds.origin.y)
// var bottomY:CGFloat = (CGFloat)(self.bounds.size.height)
// var width:CGFloat = (CGFloat)(self.bounds.width) / (CGFloat)(applications.count)
// var peak:CGFloat = width/3.0
// var tip:CGFloat = peak - pad
//
//Draw all applications
// var appNum:CGFloat = 1.0
// var appCount:CGFloat = (CGFloat)(applications.count)
// var endTopX:CGFloat = 0.0
// var endBottomX:CGFloat = 0.0
// var temp:CGFloat!
// var textRightX:CGFloat = 0.0
// var textLeftX:CGFloat = 0.0
// var label:UILabel!
if(applications.length > 0){
//float density = getContext().getResources().getDisplayMetrics().density;
int appNum = 1;
int appCount = applications.length;
float endTopX = 0;
float endBottomX = 0;
float textRightX = 0;
float textLeftX = 0;
float pad = 10;
float leftX = 0;
float rightX = viewWidth;
float topY = 0;
float bottomY = viewHeight;
float width = viewWidth / applications.length;
float peak = viewWidth / 3;
float tip = peak - pad;
String colorS;
// for app in applications{
for(ApplicationWithNotes app : applications){
path.reset();
// if(app.color != nil){
// CGContextSetFillColorWithColor(context, app.color!.CGColor)
// }
// else{
// CGContextSetFillColorWithColor(context, Colors.PARTCOLORDEFAULT.CGColor)
// }
//Each polygon will breach into the space of the next and the previous after transform by the tip with a given padding
//Each text field will stretch across the middle of each polygon with a small padding (3) to prevent overreaching
// if(appNum == 1 && applications.count == 1){
// CGContextMoveToPoint(context, leftX, topY)
// CGContextAddLineToPoint(context, width, topY)
// CGContextAddLineToPoint(context, width, bottomY)
// CGContextAddLineToPoint(context, leftX, bottomY)
// }
if(appNum == 1 && applications.length == 1){
path.moveTo(leftX, topY);
path.lineTo(width, topY);
path.lineTo(width, bottomY);
path.lineTo(leftX, bottomY);
path.close();
borderPath.moveTo(leftX, topY);
borderPath.lineTo(width, topY);
borderPath.lineTo(width, bottomY);
borderPath.lineTo(leftX, bottomY);
borderPath.close();
textLeftX = 0;
textRightX = viewWidth;
}
// if(appNum == 1){
// CGContextMoveToPoint(context, leftX, topY)
// CGContextAddLineToPoint(context, width + tip, topY)
// CGContextAddLineToPoint(context, width - peak, bottomY)
// CGContextAddLineToPoint(context, leftX, bottomY)
// textLeftX = leftX + (leftX - leftX) / 2 + 3
// textRightX = (width - peak) + ((width + tip) - (width - peak)) / 2.0 - 10
// endTopX = width + tip
// endBottomX = width - peak
// }
else if(appNum == 1){
path.moveTo(leftX, topY);
path.lineTo(width + tip, topY);
path.lineTo(width - peak, bottomY);
path.lineTo(leftX, bottomY);
path.close();
borderPath.moveTo(leftX, topY);
borderPath.lineTo(width + tip, topY);
borderPath.lineTo(width - peak, bottomY);
borderPath.lineTo(leftX, bottomY);
borderPath.close();
textLeftX = leftX + (leftX - leftX) / 2 + 3;
textRightX = (width - peak) + ((width + tip) - (width - peak)) / 2 - 10;
endTopX = width + tip;
endBottomX = width - peak;
}
// else if(appNum == appCount){
// CGContextMoveToPoint(context, endTopX + pad, topY)
// CGContextAddLineToPoint(context, rightX, topY)
// CGContextAddLineToPoint(context, rightX, bottomY)
// CGContextAddLineToPoint(context, endBottomX + pad, bottomY)
// textLeftX = (endBottomX + pad) + ((endTopX + pad) - (endBottomX + pad)) / 2.0 + 10
// textRightX = rightX + (rightX - rightX) / 2.0 - 10
// endTopX = width
// endBottomX = width
// }
else if(appNum == appCount){
path.moveTo(endTopX + pad, topY);
path.lineTo(rightX, topY);
path.lineTo(rightX, bottomY);
path.lineTo(endBottomX + pad, bottomY);
path.close();
borderPath.moveTo(endTopX + pad, topY);
borderPath.lineTo(rightX, topY);
borderPath.lineTo(rightX, bottomY);
borderPath.lineTo(endBottomX + pad, bottomY);
borderPath.close();
textLeftX = (endBottomX + pad) + ((endTopX + pad) - (endBottomX + pad)) / 2 + 10;
textRightX = rightX + (rightX - rightX) / 2 - 10;
endTopX = width;
endBottomX = width;
}
// else{
// CGContextMoveToPoint(context, endTopX + pad, topY)
// CGContextAddLineToPoint(context, width * appNum + tip, topY)
// CGContextAddLineToPoint(context, width * appNum - peak, bottomY)
// CGContextAddLineToPoint(context, endBottomX + pad, bottomY)
// textLeftX = (endBottomX + pad) + ((endTopX + pad) - (endBottomX + pad)) / 2 + 10
// textRightX = (width * appNum - peak) + ((width * appNum + tip) - (width * appNum - peak)) / 2 - 3
// endTopX = width * appNum + tip
// endBottomX = width * appNum - peak
// }
else{
path.moveTo(endTopX + pad, topY);
path.lineTo(width * appNum + tip, topY);
path.lineTo(width * appNum - peak, bottomY);
path.lineTo(endBottomX + pad, bottomY);
path.close();
borderPath.moveTo(endTopX + pad, topY);
borderPath.lineTo(width * appNum + tip, topY);
borderPath.lineTo(width * appNum - peak, bottomY);
borderPath.lineTo(endBottomX + pad, bottomY);
borderPath.close();
textLeftX = (endBottomX + pad) + ((endTopX + pad) - (endBottomX + pad)) / 2 + 10;
textRightX = (width * appNum - peak) + ((width * appNum + tip) - (width * appNum - peak)) / 2 - 3;
endTopX = width * appNum + tip;
endBottomX = width * appNum - peak;
}
// CGContextFillPath(context)
paint.setStyle(Paint.Style.FILL);
if(app.application.colorCode != null){
colorS = "#" + app.application.colorCode;
paint.setColor(Color.parseColor(colorS));
}
else{
paint.setColor(getResources().getColor(R.color.default_application));
}
canvas.drawPath(path, paint);
// borderPaint.setStyle(Paint.Style.STROKE);
// borderPaint.setColor(getResources().getColor(R.color.application_border));
// canvas.drawPath(borderPath, borderPaint);
//
//Add text label
// label = UILabel(frame: CGRectMake(textLeftX, topY, (textRightX-textLeftX), bottomY))
// label.textAlignment = NSTextAlignment.Center
// label.text = app.baseSeries
// label.numberOfLines = 1
// label.adjustsFontSizeToFitWidth = true
// label.font = UIFont(name:"OpenSans-SemiBold", size:30)
//
// self.addSubview(label)
// appBounds.append(label.frame)
//
// appNum++
textViews.add(app.application.baseSeries);
textViewsDimens.add(new float[]{textLeftX, textRightX});
appNum++;
// }
}
// self.layer.borderColor = UIColor.lightGrayColor().CGColor
// self.layer.borderWidth = 1.0
//
if(!addedTextViews){
createTextViews();
}
}
}
private void createTextViews(){
AutoResizeTextView label;
LayoutParams layout;
for(int i = 0; i < textViews.size(); i++){
label = new AutoResizeTextView(getContext());
label.setText(textViews.get(i));
label.setTextAppearance(getContext(), R.style.ApplicationText);
layout = new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
layout.topMargin = 0;
layout.height = viewHeight;
layout.leftMargin = (int) textViewsDimens.get(i)[0];
layout.width = (int) textViewsDimens.get(i)[1] - (int) textViewsDimens.get(i)[0];
label.setLayoutParams(layout);
this.addView(label);
label.setGravity(Gravity.CENTER);
}
addedTextViews = true;
}
@覆盖
受保护的void onDraw(画布){
//获取边界视图的点
//Pad=多边形之间的空间
//leftX=父视图的左边界
//rightX=父视图的右边界
//topY=父视图的上边界
//bottomY=父视图的底部边界
//宽度=变换前单个多边形的宽度
//peak=变换后多边形将到达另一个多边形空间的距离(1/3多边形宽度)
//tip=考虑了填充的峰值
//var context=UIGraphicsGetCurrentContext()
//变量pad:CGFloat=(CGFloat)(Numbers.APPLICATIONPADDING)
//var leftX:CGFloat=(CGFloat)(self.bounds.origin.x)
//var rightX:CGFloat=(CGFloat)(self.bounds.size.width)
//变量topY:CGFloat=(CGFloat)(self.bounds.origin.y)
//变量bottomY:CGFloat=(CGFloat)(self.bounds.size.height)
//变量宽度:CGFloat=(CGFloat)(self.bounds.width)/(CGFloat)(applications.count)
//变量峰值:CGFloat=宽度/3.0
//变量提示:CGFloat=峰值-焊盘
//
//绘制所有应用程序
//var appNum:CGFloat=1.0
//var-appCount:CGFloat=(CGFloat)(applications.count)
//变量endTopX:CGFloat=0.0
//var endBottomX:CGFloat=0.0
//变量温度:CGFloat!
//var textRightX:CGFloat=0.0
//var textLeftX:CGFloat=0.0
//变量标签:UILabel!
如果(applications.length>0){
//浮动密度=getContext().getResources().getDisplayMetrics().density;
int-appNum=1;
int appCount=applications.length;
float-endTopX=0;
浮点数x=0;
float textRightX=0;
浮动文本leftx=0;
浮垫=10;
float leftX=0;
浮动右X=视图宽度;
浮动topY=0;
浮动底部=视图高度;
浮动宽度=视图宽度/应用程序长度;
浮动峰值=视图宽度/3;
浮顶=峰值-衬垫;
字符串颜色;
//应用程序中的应用程序{
for(ApplicationWithNotes应用程序:应用程序){
path.reset();
//如果(app.color!=nil){
//CGContextSetFillColorWithColor(上下文,app.color!.CGColor)
// }
//否则{
//CGContextSetFillColorWithColor(上下文,Colors.PARTCOLORDEFAULT.CGColor)
// }
//每个多边形都将在变换后通过具有给定填充的尖端进入下一个和上一个多边形的空间
//每个文本字段将通过一个小填充(3)延伸到每个多边形的中间,以防止过度延伸
//if(appNum==1&&applications.count==1){
//CGContextMoveToPoint(上下文、leftX、topY)
//CGContextAddLineToPoint(上下文、宽度、主题)
//CGContextAddLineToPoint(上下文、宽度、底部)
//CGContextAddLineToPoint(上下文、leftX、bottomY)
// }
if(appNum==1&&applications.length==1){
path.moveTo(leftX,topY);
路径.lineTo(宽度,顶部);
路径.lineTo(宽度,底部);
lineTo(leftX,bottomY);
path.close();
borderPath.moveTo(leftX,topY);
borderPath.lineTo(宽度,topY);
borderPath.lineTo(宽度,底部);
lineTo(leftX,bottomY);
close();
textLeftX=0;
textRightX=视图宽度;
}
//如果(appNum==1){
//CGContextMoveToPoint(上下文、leftX、topY)
//CGContextAddLineToPoint(上下文、宽度+提示、主题)
//CGContextAddLineToPoint(上下文、宽度-峰值、底部)
//CGContextAddLineToPoint(上下文、leftX、bottomY)
//text leftX=leftX+(leftX-leftX)/2+3
//textRightX=(宽度-峰值)+(宽度+尖端)-(宽度-峰值))/2.0-10
//endTopX=宽度+尖端
//endBottomX=宽度-峰值
// }
else if(appNum==1){
path.moveTo(leftX,topY);
路径.lineTo(宽度+尖端,顶部);
线路图(宽度-峰值,底部);
lineTo(leftX,bottomY);
path.close();
borderPath.moveTo(leftX,topY);
borderPath.lineTo(宽度+尖端,顶部);
线条(宽度-峰值,底部);
lineTo(leftX,bottomY);
close();
textLeftX=leftX+(leftX-leftX)/2+3;
textRightX=(宽度-峰值)+(宽度+尖端)-(宽度-峰值))/2-10;
endTopX=宽度+尖端;
endBottomX=宽度-峰值;
}
//else if(appNum==appCount){
/