我正在尝试制作一个具有如下背景的 Android 视图:
我发现最简单的方法是用不同的背景颜色制作两个相互叠加的形状:
每个元素的高度不同,所以我需要在代码中制作形状。
起初我已经开始在 xml 中快速查看结果。我已经使用这篇文章中的原则开始了,但我并没有真正接近任何有用的东西:
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
<item>
<rotate
android:fromDegrees="45"
android:toDegrees="45"
android:pivotX="-60%"
android:pivotY="50%" >
<shape
android:shape="rectangle">
<solid
android:color="#FF00FF" />
</shape>
</rotate>
</item>
</layer-list>
我该怎么做?
任何方向的线索都会被应用,
我已经在 iOS 中这样做了,但形状在 Android 中的工作方式不同:
self.view.backgroundColor = [self getEventColor];
CALayer *backgroundLayer = self.view.layer;
CAShapeLayer *mask = CAShapeLayer.new;
mask.frame = backgroundLayer.bounds;
mask.fillColor = [[UIColor blackColor] CGColor];
CGFloat width = backgroundLayer.frame.size.width;
CGFloat height = backgroundLayer.frame.size.height;
CGMutablePathRef path = CGPathCreateMutable();
int cornerCutSize = 20;
if (cornerCutSize > height)
cornerCutSize = (int) height - 5;
CGPathMoveToPoint(path, NULL, 0, 0);
CGPathAddLineToPoint(path, nil, width, 0);
CGPathAddLineToPoint(path, nil, width, height - cornerCutSize);
CGPathAddLineToPoint(path, nil, width - cornerCutSize, height);
CGPathAddLineToPoint(path, nil, width, height);
CGPathAddLineToPoint(path, nil, 0, height);
CGPathAddLineToPoint(path, nil, 0, 0);
CGPathCloseSubpath(path);
mask.path = path;
CGPathRelease(path);
backgroundLayer.mask = mask;
UIColor *shadowColor = [[UIColor blackColor] colorWithAlphaComponent:0.12];
self.topView.backgroundColor = shadowColor;
//add border
CAShapeLayer *border = [CAShapeLayer new];
border.frame = backgroundLayer.bounds;
border.path = path;
border.lineWidth = ProgramItemBorderSize;
border.strokeColor = shadowColor.CGColor;
border.fillColor = nil;
[backgroundLayer addSublayer:border];