Dan Sandler | 49ddb0d | 2017-06-08 23:52:45 -0400 | [diff] [blame] | 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; |
Dan Sandler | e5d37d7 | 2017-10-18 16:06:28 -0400 | [diff] [blame] | 29 | private OctopusDrawable mOcto; |
Dan Sandler | 49ddb0d | 2017-06-08 23:52:45 -0400 | [diff] [blame] | 30 | |
| 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 Sandler | e5d37d7 | 2017-10-18 16:06:28 -0400 | [diff] [blame] | 47 | mOcto = new OctopusDrawable(getApplicationContext()); |
| 48 | mOcto.setSizePx((int) (OctopusDrawable.randfrange(40f,180f) * dp)); |
| 49 | mImageView.setImageDrawable(mOcto); |
Dan Sandler | 49ddb0d | 2017-06-08 23:52:45 -0400 | [diff] [blame] | 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: |
Dan Sandler | e5d37d7 | 2017-10-18 16:06:28 -0400 | [diff] [blame] | 57 | if (mOcto.hitTest(motionEvent.getX(), motionEvent.getY())) { |
Dan Sandler | 49ddb0d | 2017-06-08 23:52:45 -0400 | [diff] [blame] | 58 | touching = true; |
Dan Sandler | e5d37d7 | 2017-10-18 16:06:28 -0400 | [diff] [blame] | 59 | mOcto.stopDrift(); |
Dan Sandler | 49ddb0d | 2017-06-08 23:52:45 -0400 | [diff] [blame] | 60 | } |
| 61 | break; |
| 62 | case MotionEvent.ACTION_MOVE: |
| 63 | if (touching) { |
Dan Sandler | e5d37d7 | 2017-10-18 16:06:28 -0400 | [diff] [blame] | 64 | mOcto.moveTo(motionEvent.getX(), motionEvent.getY()); |
Dan Sandler | 49ddb0d | 2017-06-08 23:52:45 -0400 | [diff] [blame] | 65 | } |
| 66 | break; |
| 67 | case MotionEvent.ACTION_UP: |
| 68 | case MotionEvent.ACTION_CANCEL: |
| 69 | touching = false; |
Dan Sandler | e5d37d7 | 2017-10-18 16:06:28 -0400 | [diff] [blame] | 70 | mOcto.startDrift(); |
Dan Sandler | 49ddb0d | 2017-06-08 23:52:45 -0400 | [diff] [blame] | 71 | break; |
| 72 | } |
| 73 | return true; |
| 74 | } |
| 75 | }); |
| 76 | } |
Dan Sandler | e5d37d7 | 2017-10-18 16:06:28 -0400 | [diff] [blame] | 77 | |
| 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 Sandler | 49ddb0d | 2017-06-08 23:52:45 -0400 | [diff] [blame] | 89 | } |