d6c1fd038d0a36deaa46c55951737b5e9ecc850a.svn-base 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  1. package eVVM.apk.widget;
  2. import android.content.Context;
  3. import android.graphics.Canvas;
  4. import android.graphics.Path;
  5. import android.graphics.RectF;
  6. import android.util.AttributeSet;
  7. import android.widget.RelativeLayout;
  8. /**
  9. * 圆角的RelativeLayout
  10. */
  11. public class RoundRectLayout extends RelativeLayout {
  12. private Path mPath;
  13. private int mRadius;
  14. private int mWidth;
  15. private int mHeight;
  16. private int mLastRadius;
  17. public static final int MODE_NONE = 0;
  18. public static final int MODE_ALL = 1;
  19. public static final int MODE_LEFT = 2;
  20. public static final int MODE_TOP = 3;
  21. public static final int MODE_RIGHT = 4;
  22. public static final int MODE_BOTTOM = 5;
  23. private int mRoundMode = MODE_ALL;
  24. public RoundRectLayout(Context context) {
  25. super(context);
  26. init();
  27. }
  28. public RoundRectLayout(Context context, AttributeSet attrs) {
  29. super(context, attrs);
  30. init();
  31. }
  32. private void init(){
  33. //setBackgroundDrawable(new ColorDrawable(0x33ff0000));
  34. mPath = new Path();
  35. mPath.setFillType(Path.FillType.EVEN_ODD);
  36. setCornerRadius(30);
  37. }
  38. /**
  39. * 设置是否圆角裁边
  40. * @param roundMode
  41. */
  42. public void setRoundMode(int roundMode){
  43. mRoundMode = roundMode;
  44. }
  45. /**
  46. * 设置圆角半径
  47. * @param radius
  48. */
  49. public void setCornerRadius(int radius){
  50. mRadius = radius;
  51. }
  52. private void checkPathChanged(){
  53. if(getWidth() == mWidth && getHeight() == mHeight && mLastRadius == mRadius){
  54. return;
  55. }
  56. mWidth = getWidth();
  57. mHeight = getHeight();
  58. mLastRadius = mRadius;
  59. mPath.reset();
  60. switch (mRoundMode){
  61. case MODE_ALL:
  62. mPath.addRoundRect(new RectF(0, 0, mWidth, mHeight), mRadius, mRadius, Path.Direction.CW);
  63. break;
  64. case MODE_LEFT:
  65. mPath.addRoundRect(new RectF(0, 0, mWidth, mHeight),
  66. new float[]{mRadius, mRadius, 0, 0, 0, 0, mRadius, mRadius},
  67. Path.Direction.CW);
  68. break;
  69. case MODE_TOP:
  70. mPath.addRoundRect(new RectF(0, 0, mWidth, mHeight),
  71. new float[]{mRadius, mRadius, mRadius, mRadius, 0, 0, 0, 0},
  72. Path.Direction.CW);
  73. break;
  74. case MODE_RIGHT:
  75. mPath.addRoundRect(new RectF(0, 0, mWidth, mHeight),
  76. new float[]{0, 0, mRadius, mRadius, mRadius, mRadius, 0, 0},
  77. Path.Direction.CW);
  78. break;
  79. case MODE_BOTTOM:
  80. mPath.addRoundRect(new RectF(0, 0, mWidth, mHeight),
  81. new float[]{0, 0, 0, 0, mRadius, mRadius, mRadius, mRadius},
  82. Path.Direction.CW);
  83. break;
  84. }
  85. }
  86. @Override
  87. public void draw(Canvas canvas) {
  88. if(mRoundMode != MODE_NONE){
  89. int saveCount = canvas.save();
  90. checkPathChanged();
  91. canvas.clipPath(mPath);
  92. super.draw(canvas);
  93. canvas.restoreToCount(saveCount);
  94. }else {
  95. super.draw(canvas);
  96. }
  97. }
  98. }