Created
August 14, 2012 04:40
-
-
Save tondol/3346306 to your computer and use it in GitHub Desktop.
ICSのHWAccelerationで死なない角丸効果
This file contains 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 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