Android : Android: Squared Diamond Drawable

on Tuesday, April 7, 2015


I'm trying to create a drawable to use as a view background that consists of a single squared diamond centered. I've been playing with trying to create a square and rotating it 45 degrees, but it's not really working out as I'd expect. I was wondering if anyone has any suggestions or a working example that they could offer up to help me out. I've tried making this in XML, and recently switched to trying it in Java, like so:



public class DiamondView extends Drawable {

private Paint paint;

public DiamondView() {
paint = new Paint();
paint.setAntiAlias(true);
}

@Override
public void draw(Canvas canvas) {

int height = getBounds().height();
int width = getBounds().width();

RectF rect = new RectF( 0.0f, 0.0f, height / 2, height / 2 );

canvas.rotate( 45 );
canvas.translate( width / 4, 0 );
canvas.drawRect( rect, paint );


}

@Override
public void setAlpha(int alpha) {
paint.setAlpha(alpha);
}

@Override
public void setColorFilter(ColorFilter cf) {
paint.setColorFilter(cf);
}

@Override
public int getOpacity() {
return PixelFormat.TRANSLUCENT;
}


}


0 comments:

Post a Comment