f56fa23fca87cce65bd11673277c7beb7d48d24d.svn-base 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  1. package eVVM.apk.widget;
  2. import android.content.Context;
  3. import android.content.res.TypedArray;
  4. import android.graphics.Canvas;
  5. import android.graphics.Color;
  6. import android.graphics.Paint;
  7. import android.graphics.Path;
  8. import android.graphics.Rect;
  9. import android.graphics.RectF;
  10. import android.support.annotation.Nullable;
  11. import android.util.AttributeSet;
  12. import android.view.View;
  13. import android.widget.RelativeLayout;
  14. import eVVM.apk.R;
  15. /**
  16. * 头像的背景 不是RectLayout 只是个view
  17. */
  18. public class RoundHeadbgRectLayout extends View {
  19. private int mWidth;
  20. private int mHeight;
  21. /**
  22. * 弧形高度
  23. */
  24. private int mArcHeight;
  25. /**
  26. * 背景颜色
  27. */
  28. private int mBgColor;
  29. private Paint mPaint;
  30. private Context mContext;
  31. public RoundHeadbgRectLayout(Context context) {
  32. this(context, null);
  33. }
  34. public RoundHeadbgRectLayout(Context context, @Nullable AttributeSet attrs) {
  35. this(context, attrs, 0);
  36. }
  37. public RoundHeadbgRectLayout(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
  38. super(context, attrs, defStyleAttr);
  39. TypedArray typedArray = context.obtainStyledAttributes(attrs, R.styleable.ArcView);
  40. mArcHeight = typedArray.getDimensionPixelSize(R.styleable.ArcView_arcHeight, 0);
  41. mBgColor=typedArray.getColor(R.styleable.ArcView_bgColor, Color.parseColor("#303F9F"));
  42. mContext = context;
  43. mPaint = new Paint();
  44. }
  45. @Override
  46. protected void onDraw(Canvas canvas) {
  47. super.onDraw(canvas);
  48. mPaint.setStyle(Paint.Style.FILL);
  49. mPaint.setColor(mBgColor);
  50. Rect rect = new Rect(0, 0, mWidth, mHeight - mArcHeight);
  51. canvas.drawRect(rect, mPaint);
  52. Path path = new Path();
  53. path.moveTo(0, mHeight - mArcHeight);
  54. path.quadTo(mWidth / 2, mHeight, mWidth, mHeight - mArcHeight);
  55. mPaint.setAntiAlias(true);
  56. canvas.drawPath(path, mPaint);
  57. }
  58. @Override
  59. protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
  60. super.onMeasure(widthMeasureSpec, heightMeasureSpec);
  61. int widthSize = MeasureSpec.getSize(widthMeasureSpec);
  62. int widthMode = MeasureSpec.getMode(widthMeasureSpec);
  63. int heightSize = MeasureSpec.getSize(heightMeasureSpec);
  64. int heightMode = MeasureSpec.getMode(heightMeasureSpec);
  65. if (widthMode == MeasureSpec.EXACTLY) {
  66. mWidth = widthSize;
  67. }
  68. if (heightMode == MeasureSpec.EXACTLY) {
  69. mHeight = heightSize;
  70. }
  71. setMeasuredDimension(mWidth, mHeight);
  72. }
  73. }