• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 
30     @Override
onCreate(Bundle savedInstanceState)31     protected void onCreate(Bundle savedInstanceState) {
32         super.onCreate(savedInstanceState);
33         final float dp = getResources().getDisplayMetrics().density;
34 
35         getWindow().setBackgroundDrawableResource(R.drawable.octo_bg);
36 
37         FrameLayout bg = new FrameLayout(this);
38         setContentView(bg);
39         bg.setAlpha(0f);
40         bg.animate().setStartDelay(500).setDuration(5000).alpha(1f).start();
41 
42         mImageView = new ImageView(this);
43         bg.addView(mImageView, new FrameLayout.LayoutParams(
44                 ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT));
45 
46         final OctopusDrawable octo = new OctopusDrawable(getApplicationContext());
47         octo.setSizePx((int) (OctopusDrawable.randfrange(40f,180f) * dp));
48         mImageView.setImageDrawable(octo);
49         octo.startDrift();
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 (octo.hitTest(motionEvent.getX(), motionEvent.getY())) {
58                             touching = true;
59                             octo.stopDrift();
60                         }
61                         break;
62                     case MotionEvent.ACTION_MOVE:
63                         if (touching) {
64                             octo.moveTo(motionEvent.getX(), motionEvent.getY());
65                         }
66                         break;
67                     case MotionEvent.ACTION_UP:
68                     case MotionEvent.ACTION_CANCEL:
69                         touching = false;
70                         octo.startDrift();
71                         break;
72                 }
73                 return true;
74             }
75         });
76     }
77 }
78