blob: 8a06cc604f1e82f1cfbbcd0094fff98e5703f2ac [file] [log] [blame]
Dan Sandler49ddb0d2017-06-08 23:52:45 -04001/*
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
15package com.android.egg.octo;
16
17import android.app.Activity;
18import android.os.Bundle;
19import android.view.MotionEvent;
20import android.view.View;
21import android.view.ViewGroup;
22import android.widget.FrameLayout;
23import android.widget.ImageView;
24
25import com.android.egg.R;
26
27public class Ocquarium extends Activity {
28 ImageView mImageView;
Dan Sandlere5d37d72017-10-18 16:06:28 -040029 private OctopusDrawable mOcto;
Dan Sandler49ddb0d2017-06-08 23:52:45 -040030
31 @Override
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
Dan Sandlere5d37d72017-10-18 16:06:28 -040047 mOcto = new OctopusDrawable(getApplicationContext());
48 mOcto.setSizePx((int) (OctopusDrawable.randfrange(40f,180f) * dp));
49 mImageView.setImageDrawable(mOcto);
Dan Sandler49ddb0d2017-06-08 23:52:45 -040050
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:
Dan Sandlere5d37d72017-10-18 16:06:28 -040057 if (mOcto.hitTest(motionEvent.getX(), motionEvent.getY())) {
Dan Sandler49ddb0d2017-06-08 23:52:45 -040058 touching = true;
Dan Sandlere5d37d72017-10-18 16:06:28 -040059 mOcto.stopDrift();
Dan Sandler49ddb0d2017-06-08 23:52:45 -040060 }
61 break;
62 case MotionEvent.ACTION_MOVE:
63 if (touching) {
Dan Sandlere5d37d72017-10-18 16:06:28 -040064 mOcto.moveTo(motionEvent.getX(), motionEvent.getY());
Dan Sandler49ddb0d2017-06-08 23:52:45 -040065 }
66 break;
67 case MotionEvent.ACTION_UP:
68 case MotionEvent.ACTION_CANCEL:
69 touching = false;
Dan Sandlere5d37d72017-10-18 16:06:28 -040070 mOcto.startDrift();
Dan Sandler49ddb0d2017-06-08 23:52:45 -040071 break;
72 }
73 return true;
74 }
75 });
76 }
Dan Sandlere5d37d72017-10-18 16:06:28 -040077
78 @Override
79 protected void onPause() {
80 mOcto.stopDrift();
81 super.onPause();
82 }
83
84 @Override
85 protected void onResume() {
86 super.onResume();
87 mOcto.startDrift();
88 }
Dan Sandler49ddb0d2017-06-08 23:52:45 -040089}