Skip to content

Instantly share code, notes, and snippets.

@sealskej
Created March 19, 2013 00:47
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save sealskej/5192530 to your computer and use it in GitHub Desktop.
Save sealskej/5192530 to your computer and use it in GitHub Desktop.
public class SkewedImageView extends ImageView {
public SkewedImageView(Context context) {
super(context);
disableHwAcceleration();
}
public SkewedImageView(Context context, AttributeSet attrs) {
super(context, attrs);
disableHwAcceleration();
}
public SkewedImageView(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
disableHwAcceleration();
}
private void disableHwAcceleration() {
if (android.os.Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB) {
setLayerType(View.LAYER_TYPE_SOFTWARE, null);
}
}
@Override
protected void onDraw(Canvas canvas) {
Drawable drawable = getDrawable();
int drawableWidth = drawable.getIntrinsicWidth();
int drawableHeight = drawable.getIntrinsicHeight();
float triangleSize = drawableWidth * 0.25f;
Path triangle = new Path();
triangle.moveTo(0, 0);
triangle.lineTo(drawableWidth - triangleSize, 0);
triangle.lineTo(drawableWidth, triangleSize);
triangle.lineTo(drawableWidth, drawableHeight);
triangle.lineTo(0, drawableHeight);
triangle.close();
canvas.clipPath(triangle, Region.Op.REPLACE);
super.onDraw(canvas);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment