Created
March 19, 2013 00:47
-
-
Save sealskej/5192530 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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