摩天轮

作者: 菜鸟何时起飞 | 来源:发表于2020-04-19 19:24 被阅读0次
摩天轮.gif
public class FerrisWheelLayout extends ViewGroup
{
   private double angle = 0F;
   private int DEFAULT_SIZE = 200;
   private String TAG = "FerrisWheelLayout";
   private Paint mPaint;

   public FerrisWheelLayout(Context context, AttributeSet attrs, int defStyleAttr)
   {
       super(context, attrs, defStyleAttr);
       init(context, attrs, defStyleAttr);
   }

   public FerrisWheelLayout(Context context, AttributeSet attrs)
   {
       this(context, attrs, 0);
   }

   public FerrisWheelLayout(Context context)
   {
       this(context, null);
   }

   private void init(Context context, AttributeSet attrs, int defStyleAttr)
   {
       mPaint = new Paint(Paint.ANTI_ALIAS_FLAG);
       mPaint.setColor(Color.BLACK);
       setWillNotDraw(false);
   }

   @Override
   protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec)
   {
       super.onMeasure(widthMeasureSpec, heightMeasureSpec);
       int width = measure(widthMeasureSpec);
       int height = measure(widthMeasureSpec);
       int childCount = getChildCount();
       for (int i = 0; i < childCount; i++)
       {
           View childAt = getChildAt(i);
           LayoutParams layoutParams = childAt.getLayoutParams();
           layoutParams.width = UnitConversionUtil.Dp2Px(40);
           layoutParams.height = UnitConversionUtil.Dp2Px(40);
       }
       int length = Math.min(width, height);
       setMeasuredDimension(length, length);
   }

   private int measure(int widthMeasureSpec)
   {
       int mode = MeasureSpec.getMode(widthMeasureSpec);
       int size = MeasureSpec.getSize(widthMeasureSpec);
       switch (mode)
       {
           case MeasureSpec.AT_MOST:
               return Math.min(size, DEFAULT_SIZE);
           case MeasureSpec.EXACTLY:
               return size;
           case MeasureSpec.UNSPECIFIED:
               return DEFAULT_SIZE;
           default:
               return 0;
       }
   }

   private int centerX;
   private int centerY;
   private int radius;

   @Override
   protected void onLayout(boolean changed, int l, int t, int r, int b)
   {
//        Log.e(TAG, "l" + l + " t" + t + " r" + r + " b" + b);
       centerX = (r - l) / 2;
       centerY = (b - t) / 2;
       radius = Math.min((r - l) / 2, (b - t) / 2) - 30;
//        Log.e(TAG, "centerX:" + centerX + "centerY:" + centerY + "radius:" + radius);
       int childCount = getChildCount();
       for (int i = 0; i < childCount; i++)
       {
           final int index = i;
           View child = getChildAt(i);
           LayoutParams layoutParams = child.getLayoutParams();
           int childWidth = layoutParams.width;
           int childHeight = layoutParams.height;

           child.layout(centerX + (int) (radius * Math.cos((Math.PI * 2) / childCount * i + angle)) - childWidth / 2,
                   centerY + (int) (radius * Math.sin((Math.PI * 2) / childCount * i + angle)) - childHeight / 2,
                   centerX + (int) (radius * Math.cos((Math.PI * 2) / childCount * i + angle)) + childWidth / 2,
                   centerY + (int) (radius * Math.sin((Math.PI * 2) / childCount * i + angle)) + childWidth / 2);
           child.setOnClickListener(new OnClickListener()
           {
               @Override
               public void onClick(View v)
               {
                   Toast.makeText(FerrisWheelLayout.this.getContext(), "这是第" + index + "张图片", Toast.LENGTH_SHORT).show();
               }
           });
//            Log.e(TAG, i + " left:" + (centerX + (int) (radius * Math.cos((Math.PI * 2) / childCount * i + angle)) - childWidth / 2) +
//                    " top:" + (centerY + (int) (radius * Math.sin((Math.PI * 2) / childCount * i + angle)) - childHeight / 2) +
//                    " right:" + (centerX + (int) (radius * Math.cos((Math.PI * 2) / childCount * i + angle)) + childWidth / 2) +
//                    " bottom:" + (centerY + (int) (radius * Math.sin((Math.PI * 2) / childCount * i + angle)) + childWidth / 2));
       }
   }

   @Override
   protected void onDraw(Canvas canvas)
   {
       super.onDraw(canvas);
       canvas.translate(centerX, centerY);
       int childCount = getChildCount();
       for (int i = 0; i < childCount; i++)
       {
           canvas.drawLine(0, 0, (int) (radius * Math.cos((Math.PI * 2) / childCount * i + angle))
                   , (int) (radius * Math.sin((Math.PI * 2) / childCount * i + angle)), mPaint);
       }
       canvas.translate(-centerX, -centerY);
   }

   private float startX;
   private float startY;
   private float currentX;
   private float currentY;
   private float endX;
   private float endY;

   @Override
   public boolean onTouchEvent(MotionEvent event)
   {
       Log.e(TAG, "onTouchEvent");
       switch (event.getAction())
       {
           case MotionEvent.ACTION_DOWN:
               Log.e(TAG, "onTouchEvent  ACTION_DOWN");
               startX = event.getX();
               startY = event.getY();
               break;
           case MotionEvent.ACTION_MOVE:
               Log.e(TAG, "onTouchEvent  ACTION_MOVE");
               currentX = event.getX();
               currentY = event.getY();
               changeAngle(startX, startY, currentX, currentY);
//               postInvalidate();/*这个方法 没有调用 onLayout方法 所以子view位置没有改变*/
               requestLayout();
               startX = currentX;
               startY = currentY;
               break;
           case MotionEvent.ACTION_UP:
               Log.e(TAG, "onTouchEvent  ACTION_UP");
               endX = event.getX();
               endY = event.getY();
               break;
       }
       return true;
   }

   private void changeAngle(float startX, float startY, float currentX, float currentY)
   {
       angle += Math.atan2(currentY - centerY, currentX - centerX) - Math.atan2(startY - centerY, startX - centerX);
   }
}

相关文章

  • 摩天轮

    维也纳摩天轮是至今仍在营运的一座19世纪摩天轮,1920年,巴黎摩天轮拆除后,维也纳摩天轮就成为世界最高的摩天轮。...

  • 摩天轮的爱情 -

    摩天轮在每个地区都有,有人说你爱一个就和他她去摩天轮上面就会很幸福。摩天轮是充满开心幸福的地方。你会对摩天轮...

  • 渤海之眼

    今年暑假,我去了刚建好的渤海之眼。 无辐式摩天轮,摩天轮的一种。传统的摩天轮都是中间有轴,绕轴转动,而无辐式摩天轮...

  • 随遇而安的旅途:广州塔摩天轮

    广州塔的摩天轮被誉为目前世界上最大的横向摩天轮。 摩天轮位于广州塔塔身顶450米高空处,也是目前世界最高摩天轮。2...

  • ✿雌蕊花艺✿时光轮回,让幸福蔓延至每个春夏秋冬

    摩天轮代表着幸福,每一个在仰望摩天轮的人,都是在仰望幸福。传说一起坐摩天轮的恋人最终会以分手告终,但当摩天轮达到最...

  • 随笔

    你相信摩天轮的解咒语 而我 只相信摩天轮的魔咒

  • 摩天轮

    文/何必悲欢 乌龟问蜗牛, 摩天轮是什么? 蜗牛说, 我的壳就是摩天轮。 乌龟问槐树, 摩天轮是什么? 槐树说, ...

  • 梦幻摩天轮|作者/江雪〖原创新诗(283)〗

    梦幻摩天轮 作者/江雪 摩天轮之高 与天同高 不然 如何摩天? 摩天轮之摩天 在于轮 不然 如何摩天? 每次坐上摩...

  • 美林湖的摩天轮

    美林湖的摩天轮 那天我们去游美林湖摩天轮,它在美林湖社区湖岸西路,是目前国内最大的屋顶摩天轮,据...

  • 英国伦敦眼,美得不像实力派

    伦敦眼 The London Eye —摩天轮的传说— 关于摩天轮,有一个美丽的传说。人们都相信,摩天轮的每一个格...

网友评论

      本文标题:摩天轮

      本文链接:https://www.haomeiwen.com/subject/xcbbbhtx.html