我正在尝试使用这样的背景制作Android视图:
我已经想出最简单的方法,就是将两个形状叠加在一起,使用不同的背景颜色:
每个元素的高度都不同,所以我需要在代码中制作形状 起初我已经开始使用xml来快速查看结果。我已经使用this post中的原则开始了,但我并没有真正接近任何有用的东西:
<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];
答案 0 :(得分:3)
如果您需要处理的是不同高度,请将其设为9个可绘制的补丁。查看开发人员指南:http://developer.android.com/guide/topics/graphics/2d-graphics.html#nine-patch http://developer.android.com/tools/help/draw9patch.html
如果您想要更接近iOS代码的东西,可以使用路径并绘制到画布。 例如,您可以创建自定义drawable来执行此操作:
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.ColorFilter;
import android.graphics.Paint;
import android.graphics.Path;
import android.graphics.drawable.Drawable;
public class PathDrawable extends Drawable {
Path mPath = new Path();
Paint mPaint = new Paint(Paint.ANTI_ALIAS_FLAG);
public PathDrawable(){
mPaint.setColor(Color.GRAY);
mPaint.setStyle(Paint.Style.FILL_AND_STROKE);
mPaint.setStrokeWidth(5);
}
@Override
public void draw(Canvas canvas) {
mPath.moveTo(10,10);
mPath.lineTo(10, canvas.getHeight()-10);
mPath.lineTo(canvas.getWidth()-50, canvas.getHeight()-10);
mPath.lineTo(canvas.getWidth()-10, canvas.getHeight()-50);
mPath.lineTo(canvas.getWidth()-10, 10);
mPath.close();
canvas.drawPath(mPath, mPaint);
}
@Override
public void setAlpha(int i) {}
@Override
public void setColorFilter(ColorFilter colorFilter) {}
@Override
public int getOpacity() {
return 0;
}
}
这里我只绘制其中一个形状,但你应该明白这个想法。
答案 1 :(得分:2)
我同意@durbnpoisn。
我添加了一个布局示例。黄色是透明的。