YinYangFishFragment.java 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  1. package com.yxf.clippathlayout.sample;
  2. import android.graphics.Path;
  3. import android.graphics.RectF;
  4. import android.os.Bundle;
  5. import android.support.annotation.NonNull;
  6. import android.support.annotation.Nullable;
  7. import android.support.v4.app.Fragment;
  8. import android.view.LayoutInflater;
  9. import android.view.View;
  10. import android.view.ViewGroup;
  11. import com.yxf.clippathlayout.PathInfo;
  12. import com.yxf.clippathlayout.impl.ClipPathFrameLayout;
  13. import com.yxf.clippathlayout.pathgenerator.PathGenerator;
  14. public class YinYangFishFragment extends Fragment {
  15. private ClipPathFrameLayout mLayout;
  16. private View mYinFishView, mYangFishView;
  17. @Nullable
  18. @Override
  19. public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
  20. mLayout = (ClipPathFrameLayout) inflater.inflate(R.layout.fragment_yin_yang_fish, null);
  21. mYinFishView = mLayout.findViewById(R.id.yin_fish_view);
  22. mYangFishView = mLayout.findViewById(R.id.yang_fish_view);
  23. mYinFishView.setOnClickListener(new View.OnClickListener() {
  24. @Override
  25. public void onClick(View v) {
  26. MyApplication.displayToast("阴");
  27. }
  28. });
  29. mYangFishView.setOnClickListener(new View.OnClickListener() {
  30. @Override
  31. public void onClick(View v) {
  32. MyApplication.displayToast("阳");
  33. }
  34. });
  35. new PathInfo.Builder(new YinYangFishPathGenerator(270), mYinFishView)
  36. .setAntiAlias(true)
  37. .create()
  38. .apply();
  39. new PathInfo.Builder(new YinYangFishPathGenerator(90), mYangFishView)
  40. .create()
  41. .apply();
  42. return mLayout;
  43. }
  44. private static class YinYangFishPathGenerator implements PathGenerator {
  45. private int mDegree;
  46. private RectF mRectF = new RectF();
  47. private Path mPath = new Path();
  48. public YinYangFishPathGenerator(int degree) {
  49. mDegree = degree;
  50. }
  51. @Override
  52. public Path generatePath(Path old, View view, int width, int height) {
  53. double radians = Math.toRadians(mDegree);
  54. int radius = Math.min(width, height) / 2;
  55. int centerX = width / 2;
  56. int centerY = height / 2;
  57. if (old == null) {
  58. old = new Path();
  59. } else {
  60. old.reset();
  61. }
  62. mRectF.set(centerX - radius, centerY - radius, centerX + radius, centerY + radius);
  63. old.moveTo(centerX + (float) (radius * Math.cos(radians)),
  64. centerY + (float) (radius * Math.sin(radians)));
  65. old.arcTo(mRectF, mDegree, 180);
  66. int degree = mDegree + 180;
  67. double cRadians = Math.toRadians(degree);
  68. int cX = centerX + (int) (radius * Math.cos(cRadians) / 2);
  69. int cY = centerY + (int) (radius * Math.sin(cRadians) / 2);
  70. int cR = radius / 2;
  71. mRectF.set(cX - cR, cY - cR, cX + cR, cY + cR);
  72. old.arcTo(mRectF, degree, 180);
  73. old.close();
  74. mPath.reset();
  75. mPath.addCircle(cX, cY, cR / 3, Path.Direction.CW);
  76. old.op(mPath, Path.Op.DIFFERENCE);
  77. cX = centerX + (int) (radius * Math.cos(radians) / 2);
  78. cY = centerY + (int) (radius * Math.sin(radians) / 2);
  79. mRectF.set(cX - cR, cY - cR, cX + cR, cY + cR);
  80. mPath.reset();
  81. mPath.addArc(mRectF, mDegree, 180);
  82. mPath.close();
  83. old.op(mPath, Path.Op.DIFFERENCE);
  84. mPath.reset();
  85. mPath.moveTo(centerX + (float) (radius * Math.cos(radians)),
  86. centerY + (float) (radius * Math.sin(radians)));
  87. mPath.addCircle(cX, cY, cR / 3, Path.Direction.CW);
  88. old.op(mPath, Path.Op.UNION);
  89. return old;
  90. }
  91. }
  92. }