Skip to content

Instantly share code, notes, and snippets.

@tondol
Created August 14, 2012 04:40
Show Gist options
  • Save tondol/3346306 to your computer and use it in GitHub Desktop.
Save tondol/3346306 to your computer and use it in GitHub Desktop.
ICSのHWAccelerationで死なない角丸効果
public class RoundedFrameLayout extends FrameLayout {
private Path path;
private static final int HONEY_COMB = 11;
private static final int LAYER_TYPE_SOFTWARE = 1;
public RoundedFrameLayout(Context context) {
super(context);
useSoftwareLayer();
}
public RoundedFrameLayout(Context context, AttributeSet attrs) {
super(context, attrs);
userSoftwareLayer();
}
private void useSoftwareLayer() {
// http://stackoverflow.com/questions/7401319/use-hardwareacceleration-flag-with-canvas-clippath
if (android.os.Build.VERSION.SDK_INT >= HONEY_COMB) {
try {
Method method = getClass().getMethod("setLayerType", int.class, Paint.class);
method.invoke(this, LAYER_TYPE_SOFTWARE, null);
} catch (NoSuchMethodException e) {
e.printStackTrace();
} catch (IllegalAccessException e) {
e.printStackTrace();
} catch (InvocationTargetException e) {
e.printStackTrace();
}
}
}
@Override
protected void dispatchDraw(Canvas canvas){
if (path == null) {
path = new Path();
path.addRoundRect(
new RectF(0, 0, getWidth(), getHeight()),
10, 10, Direction.CW);
}
canvas.clipPath(path);
super.dispatchDraw(canvas);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment