Reputation: 7806
I am trying to make an Android View with a background like this:
I have figured out that the easiest way, properly is to make two shapes on top of each other with different background colors:
The height of the is different for each element, so I need to make the shape in code.
At first I have started in xml to quickly see the results. I have used the principles from this post to get started, but I don't really get close to anything usefull:
<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>
How should I do this?
Any clues of direction would be appriciated
I have done it in iOS like this but the shapes doesn't work the same way in 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];
Upvotes: 1
Views: 3969
Reputation: 2576
If all you have to deal with is varying height, make it a 9 patch drawable. Check out the developer guide : http://developer.android.com/guide/topics/graphics/2d-graphics.html#nine-patch http://developer.android.com/tools/help/draw9patch.html
If you want something closer to your iOS code, you could use a Path and draw to a canvas. For example, you could create a custom drawable to do this:
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;
}
}
Here I just draw one of the shapes, but you should get the idea.
Upvotes: 3
Reputation: 7792
I agree with @durbnpoisn.
I added an example of the layout. The yellow would be transparent.
Upvotes: 2