服务器之家

服务器之家 > 正文

Android实现纸飞机的简单操作

时间:2022-02-25 15:21     来源/作者:洄游鱼lu

在项目中,我们要求做一个纸飞机的功能:就是当打开这个界面时,会有4架纸飞机从屏幕左侧飞入,然后到达自己的位置坐上下浮动,同时云彩也不断地从屏幕右侧飘到屏幕左侧。当你点击其中一个纸飞机时,这个纸飞机先向上飞出屏幕外,再从左侧飞入,当飞机回到原来位置时,弹出一个消息框。下面直接上代码:

一、首先自定义一个RelativeLayout,主要目的就是制作飞机的进入动画:

 

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
public class PaperPlaneLayout extends RelativeLayout implements View.OnClickListener{
  private OnClickListener mOnClickListener;
 
  //自定义布局的宽、高
  private int mHeight;
  private int mWidth;
  private LayoutParams lp;
  private Drawable[] drawables;
  private Random random = new Random();
 
  //获取4架纸飞机的宽高
  private int dHeight;
  private int dWidth;
 
  private int mX;
  private int mY;
 
  public PaperPlaneLayout(Context context) {
    super(context);
    init();
  }
 
  public PaperPlaneLayout(Context context,
    AttributeSet attrs) {
    super(context, attrs);
    init();
  }
 
  public PaperPlaneLayout(Context context,
    AttributeSet attrs, int defStyleAttr) {
    super(context, attrs, defStyleAttr);
    init();
  }
 
  @TargetApi(Build.VERSION_CODES.LOLLIPOP)
  public PaperPlaneLayout(Context context,
    AttributeSet attrs,int defStyleAttr, int defStyleRes) {
    super(context, attrs, defStyleAttr, defStyleRes);
    init();
  }
 
  private void init() {
    // 初始化显示的图片
    drawables = new Drawable[4];
    Drawable pink =
      getResources().getDrawable(R.drawable.pl_pink);
    Drawable yellow =
      getResources().getDrawable(R.drawable.pl_yellow);
    Drawable green =
      getResources().getDrawable(R.drawable.pl_green);
    Drawable blue =
      getResources().getDrawable(R.drawable.pl_blue);
 
    drawables[0] = blue;
    drawables[1] = yellow;
    drawables[2] = green;
    drawables[3] = pink;
    // 获取图的宽高 用于后面的计算
    // 注意 我这里4张图片的大小都是一样的,所以我只取了一个
    dHeight = UIUtility.dipTopx(getContext(), 80);
    dWidth = UIUtility.dipTopx(getContext(), 80);
    lp = new LayoutParams(dWidth, dHeight);
  }
 
  @Override
  protected void onMeasure(int widthMeasureSpec,
    int heightMeasureSpec) {
    super.onMeasure(widthMeasureSpec, heightMeasureSpec);
    mWidth = getMeasuredWidth();
    mHeight = getMeasuredHeight();
  }
 
  //真正动画开始的入口,从外部进行调用,x、y分别表示飞机进入之后所
  //停留的位置坐标
  public void addHeart(int x, int y, int position) {
    mX = x;
    mY = y;
    ImageView imageView = new ImageView(getContext());
    // 随机选一个
    imageView.setImageDrawable(drawables[position]);
    imageView.setLayoutParams(lp);
    addView(imageView);
    //获取进入前后动画
    Animator set = getAnimator(imageView);
    set.start();
    imageView.setOnClickListener(this);
  }
 
  private Animator getAnimator(View target) {
    AnimatorSet set = getEnterAnimator(target);
    AnimatorSet set2 = getLineAnimation(target);
    AnimatorSet finalSet = new AnimatorSet();
    finalSet.playSequentially(set, set2);
    finalSet.setInterpolator(new LinearInterpolator());
    finalSet.setTarget(target);
    return finalSet;
  }
 
  private AnimatorSet getEnterAnimator(final View target) {
    ObjectAnimator alpha = ObjectAnimator
      .ofFloat(target, View.ALPHA, 0.2f, 1f);
    ObjectAnimator translationX = ObjectAnimator
      .ofFloat(target, View.TRANSLATION_X,
        -2 * mWidth, -mWidth);
    AnimatorSet enter = new AnimatorSet();
    enter.setDuration(500);
    enter.setInterpolator(new LinearInterpolator());
    enter.playTogether(translationX, alpha);
    enter.setTarget(target);
    return enter;
  }
 
  private AnimatorSet getLineAnimation(final View iconView) {
    ObjectAnimator transX = ObjectAnimator
      .ofFloat(iconView, "translationX", -dWidth, mX);
    ObjectAnimator transY = ObjectAnimator
      .ofFloat(iconView, "translationY",
        (mHeight - dHeight) / 2, mY);
      transY.
        setInterpolator(PathInterpolatorCompat
        .create(0.7f, 1f));
 
    AnimatorSet flyUpAnim = new AnimatorSet();
    flyUpAnim.setDuration(900);
    flyUpAnim.playTogether(transX, transY);
    flyUpAnim.setTarget(iconView);
    return flyUpAnim;
  }
 
  @Override
  public void onClick(View v) {
    if (mOnClickListener != null) {
      mOnClickListener.onClick((ImageView) v);
    }
  }
 
  //定义ImageView单击事件
  public interface OnClickListener {
    void onClick(ImageView v);
  }

二、接下来就是布局文件的搭建了(只选取一部分控件)

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
<RelativeLayout
  xmlns:android="http://schemas.android.com/apk/res/android"
  android:id="@+id/relative_plane_bj"
  android:layout_width="match_parent"
  android:layout_height="match_parent"
  android:background="@drawable/paper_plane_bg">
 
  <!--白云-->
  <ImageView
    android:id="@+id/img_white_cloud"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:src="@drawable/paper_plane_cloud"
    android:layout_centerHorizontal="true"
    android:layout_marginTop="30dp" />
 
  <!--自定义的飞机布局动画-->
  <com.cloudi.forum.view.PaperPlaneLayout
    android:id="@+id/plane_layout"
    android:layout_width="match_parent"
    android:layout_height="match_parent"/>
</RelativeLayout>

三、接下来就可以在Activity中使用了:

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
public class PlaneActivity extends AppCompatActivity{
  @Bind(R.id.img_white_cloud)
  ImageView mImgWhiteCloud;
  @Bind(R.id.plane_layout)
  PaperPlaneLayout mPlaneLayout;
 
  private Context mContext;
  private ObjectAnimator objCloudAnim;
  private TranslateAnimation planeAnimation;
 
  private float iconX, iconY;
  //设置飞机是否已点击,如果为true,则另一个飞机不可点击
  private boolean mIsClick = true;
 
  @Override
  protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_plane_layout);
    ButterKnife.bind(this);
    mContext = getApplicationContext();
    //初始化动画
    initAnimation();
    initListener();
  }
 
  private void initAnimation() {
    //设定纸飞机进入的位置
    initPlaneEnterAnimation();
    //飞机进入后做上下浮动
    initPlaneAnimation();
    //云彩循环从屏幕右侧飘到屏幕左侧
    initCloudAnimation();
  }
 
  //设定纸飞机进入的位置
  private void initPlaneEnterAnimation() {
    for (int i = 0; i < 4; i++) {
      final int temp = i;
      mPlaneLayout.post(new Runnable() {
        @Override
        public void run() {
          //下面的值根据用户自己设定
          if (temp == 0) {
            mPlaneLayout.addHeart(
              100dp, 140dp, 0);
          }
          if (temp == 1) {
            mPlaneLayout.addHeart(
              屏宽 - 120dp, 190dp, 1);
          }
          if (temp == 2) {
            mPlaneLayout.addHeart(
              30dp, 240dp, 2);
          }
          if (temp == 3) {
            mPlaneLayout.addHeart(
              屏宽 - 210, 290, 3);
          }
        }
      });
    }
  }
 
  //飞机进入后做上下浮动
  private void initPlaneAnimation() {
    planeAnimation = new TranslateAnimation(0, 0, -10, 10);
    planeAnimation.setDuration(1000);
    planeAnimation.setRepeatCount(Animation.INFINITE);
    planeAnimation.setRepeatMode(Animation.REVERSE);
    mPlaneLayout.setAnimation(planeAnimation);
    planeAnimation.start();
  }
 
  //云彩循环从屏幕右侧飘到屏幕左侧
  private void initCloudAnimation() {
    if (objCloudAnim == null) {
      objCloudAnim = ObjectAnimator
        .ofFloat(mImgWhiteCloud, "translationX",
          屏宽 - 50, -屏宽);
      // 设置持续时间
      objCloudAnim.setDuration(5000);
      // 设置循环播放
      objCloudAnim.setRepeatCount(
        ObjectAnimator.INFINITE);
    }
    objCloudAnim.start();
  }
  private void initListener() {
    mPlaneLayout.setOnClickListener(new
      PaperPlaneLayout.OnClickListener() {
      @Override
      public void onClick(ImageView v) {
        if (mIsClick) {
          mIsClick = false;
          iconX = v.getX();
          iconY = v.getY();
          //当点击某一个纸飞机时,飞机会有一个飞出动画
          planeOutAnimation(v);
        }
      }
    });
  }
 
  /**
   * 飞机飞出动画
   */
  private void planeOutAnimation(final View iconView) {
    AnimatorSet flyUpAnim = new AnimatorSet();
    flyUpAnim.setDuration(600);
 
    ObjectAnimator transX = ObjectAnimator
      .ofFloat(iconView, "translationX",
        iconView.getX(),
        UIUtility.getScreenWidth(mContext) * 2);
    ObjectAnimator transY = ObjectAnimator
      .ofFloat(iconView, "translationY",
        0,
        - UIUtility.getScreenHeight(mContext) * 2);
    transY.setInterpolator(PathInterpolatorCompat
      .create(0.7f, 1f));
    ObjectAnimator rotation = ObjectAnimator
      .ofFloat(iconView, "rotation", -45, 0);
    rotation.setInterpolator(new DecelerateInterpolator());
    ObjectAnimator rotationX = ObjectAnimator
      .ofFloat(iconView, "rotationX", 0, 60);
    rotationX.setInterpolator(
      new DecelerateInterpolator());
 
    flyUpAnim.playTogether(transX, transY, rotationX,
        ObjectAnimator
          .ofFloat(iconView, "scaleX", 1, 0.5f),
        ObjectAnimator
          .ofFloat(iconView, "scaleY", 1, 0.5f),
        rotation
    );
    flyUpAnim.addListener(new Animator.AnimatorListener() {
      @Override
      public void onAnimationStart(Animator animation) {
 
      }
 
      @Override
      public void onAnimationEnd(Animator animation) {
        // 飞机飞入动画
        downPlaneAnimation(iconView);
      }
 
      @Override
      public void onAnimationCancel(Animator animation) {
 
      }
 
      @Override
      public void onAnimationRepeat(Animator animation) {
 
      }
    });
    flyUpAnim.start();
  }
 
  /**
   * 飞机飞入动画
   */
  private void downPlaneAnimation(final View iconView) {
    final int offDistX = -iconView.getRight();
    final int offDistY = -UIUtility.dipTopx(mContext, 10);
    AnimatorSet flyDownAnim = new AnimatorSet();
    flyDownAnim.setDuration(500);
    ObjectAnimator transX1 = ObjectAnimator
      .ofFloat(iconView, "translationX",
        UIUtility.getScreenWidth(mContext), offDistX);
    ObjectAnimator transY1 = ObjectAnimator
      .ofFloat(iconView, "translationY",
        - UIUtility.getScreenHeight(mContext),
        offDistY);
    transY1.setInterpolator(
      PathInterpolatorCompat.create(0.1f, 1f));
    ObjectAnimator rotation1 = ObjectAnimator
      .ofFloat(iconView, "rotation",
        iconView.getRotation(), 0);
    rotation1.setInterpolator(
      new AccelerateInterpolator());
    flyDownAnim.playTogether(transX1, transY1,
        ObjectAnimator
          .ofFloat(iconView, "scaleX", 0.5f, 0.9f),
        ObjectAnimator
          .ofFloat(iconView, "scaleY", 0.5f, 0.9f),
        rotation1
    );
    flyDownAnim.addListener(
      new Animator.AnimatorListener() {
      @Override
      public void onAnimationStart(Animator animation) {
        iconView.setRotationY(180);
      }
 
      @Override
      public void onAnimationEnd(Animator animation) {
 
      }
 
      @Override
      public void onAnimationCancel(Animator animation) {
 
      }
 
      @Override
      public void onAnimationRepeat(Animator animation) {
 
      }
    });
 
    AnimatorSet flyInAnim = new AnimatorSet();
    flyInAnim.setDuration(500);
    flyInAnim.setInterpolator(
      new DecelerateInterpolator());
    ObjectAnimator tranX2 = ObjectAnimator
      .ofFloat(iconView, "translationX",
        offDistX, iconX);
    ObjectAnimator tranY2 = ObjectAnimator
      .ofFloat(iconView, "translationY",
        offDistY, iconY);
    ObjectAnimator rotationX2 = ObjectAnimator
      .ofFloat(iconView, "rotationX", 30, 0);
    flyInAnim.playTogether(tranX2, tranY2, rotationX2,
    ObjectAnimator.ofFloat(iconView, "scaleX", 0.9f, 1f),
    ObjectAnimator.ofFloat(iconView, "scaleY", 0.9f, 1f));
    flyInAnim.setStartDelay(100);
    flyInAnim.addListener(new Animator.AnimatorListener() {
      @Override
      public void onAnimationStart(Animator animation) {
        iconView.setRotationY(0);
      }
 
      @Override
      public void onAnimationEnd(Animator animation) {
 
      }
 
      @Override
      public void onAnimationCancel(Animator animation) {
 
      }
 
      @Override
      public void onAnimationRepeat(Animator animation) {
 
      }
    });
 
    AnimatorSet mFlyAnimator = new AnimatorSet();
    mFlyAnimator.playSequentially(flyDownAnim, flyInAnim);
    mFlyAnimator.start();
  }

这样一来纸飞机的进入和点击离开动画就完成了。

 

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持服务器之家。

原文链接:https://blog.csdn.net/u013253709/article/details/76640783

标签:

相关文章

热门资讯

2022年最旺的微信头像大全 微信头像2022年最新版图片
2022年最旺的微信头像大全 微信头像2022年最新版图片 2022-01-10
蜘蛛侠3英雄无归3正片免费播放 蜘蛛侠3在线观看免费高清完整
蜘蛛侠3英雄无归3正片免费播放 蜘蛛侠3在线观看免费高清完整 2021-08-24
背刺什么意思 网络词语背刺是什么梗
背刺什么意思 网络词语背刺是什么梗 2020-05-22
yue是什么意思 网络流行语yue了是什么梗
yue是什么意思 网络流行语yue了是什么梗 2020-10-11
2020微信伤感网名听哭了 让对方看到心疼的伤感网名大全
2020微信伤感网名听哭了 让对方看到心疼的伤感网名大全 2019-12-26
返回顶部