1 /*
2  * Copyright (C) 2017 The Android Open Source Project
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file
5  * except in compliance with the License. You may obtain a copy of the License at
6  *
7  *      http://www.apache.org/licenses/LICENSE-2.0
8  *
9  * Unless required by applicable law or agreed to in writing, software distributed under the
10  * License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
11  * KIND, either express or implied. See the License for the specific language governing
12  * permissions and limitations under the License.
13  */
14 
15 package com.android.egg.octo;
16 
17 import android.app.Activity;
18 import android.os.Bundle;
19 import android.view.MotionEvent;
20 import android.view.View;
21 import android.view.ViewGroup;
22 import android.widget.FrameLayout;
23 import android.widget.ImageView;
24 
25 import com.android.egg.R;
26 
27 public class Ocquarium extends Activity {
28     ImageView mImageView;
29     private OctopusDrawable mOcto;
30 
31     @Override
onCreate(Bundle savedInstanceState)32     protected void onCreate(Bundle savedInstanceState) {
33         super.onCreate(savedInstanceState);
34         final float dp = getResources().getDisplayMetrics().density;
35 
36         getWindow().setBackgroundDrawableResource(R.drawable.octo_bg);
37 
38         FrameLayout bg = new FrameLayout(this);
39         setContentView(bg);
40         bg.setAlpha(0f);
41         bg.animate().setStartDelay(500).setDuration(5000).alpha(1f).start();
42 
43         mImageView = new ImageView(this);
44         bg.addView(mImageView, new FrameLayout.LayoutParams(
45                 ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT));
46 
47         mOcto = new OctopusDrawable(getApplicationContext());
48         mOcto.setSizePx((int) (OctopusDrawable.randfrange(40f,180f) * dp));
49         mImageView.setImageDrawable(mOcto);
50 
51         mImageView.setOnTouchListener(new View.OnTouchListener() {
52             boolean touching;
53             @Override
54             public boolean onTouch(View view, MotionEvent motionEvent) {
55                 switch (motionEvent.getActionMasked()) {
56                     case MotionEvent.ACTION_DOWN:
57                         if (mOcto.hitTest(motionEvent.getX(), motionEvent.getY())) {
58                             touching = true;
59                             mOcto.stopDrift();
60                         }
61                         break;
62                     case MotionEvent.ACTION_MOVE:
63                         if (touching) {
64                             mOcto.moveTo(motionEvent.getX(), motionEvent.getY());
65                         }
66                         break;
67                     case MotionEvent.ACTION_UP:
68                     case MotionEvent.ACTION_CANCEL:
69                         touching = false;
70                         mOcto.startDrift();
71                         break;
72                 }
73                 return true;
74             }
75         });
76     }
77 
78     @Override
onPause()79     protected void onPause() {
80         mOcto.stopDrift();
81         super.onPause();
82     }
83 
84     @Override
onResume()85     protected void onResume() {
86         super.onResume();
87         mOcto.startDrift();
88     }
89 }
90