• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2014 The Android Open Source Project
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 package com.example.android.support.wearable.notifications;
18 
19 import android.animation.Animator;
20 import android.animation.AnimatorListenerAdapter;
21 import android.animation.ObjectAnimator;
22 import android.animation.PropertyValuesHolder;
23 import android.app.Activity;
24 import android.content.Context;
25 import android.os.Bundle;
26 import android.view.ViewGroup;
27 import android.view.animation.AccelerateDecelerateInterpolator;
28 import android.widget.FrameLayout;
29 import android.widget.ImageView;
30 import android.widget.TextView;
31 
32 import java.util.Random;
33 
34 /**
35  * Custom display activity for an animated sample notification.
36  */
37 public class AnimatedNotificationDisplayActivity extends Activity {
38     public static final String EXTRA_TITLE = "title";
39 
40     private static final int BASE_ANIMATION_DURATION_MS = 2000;
41 
42     private Random mRandom;
43     private int mAnimationRange;
44     private ImageView mImageView;
45     private Animator mAnimation;
46 
47     @Override
onCreate(Bundle savedInstanceState)48     protected void onCreate(Bundle savedInstanceState) {
49         super.onCreate(savedInstanceState);
50         setContentView(R.layout.activity_animated_notification_display);
51 
52         mRandom = new Random(System.currentTimeMillis());
53         mAnimationRange = getResources().getDimensionPixelSize(R.dimen.animation_range);
54 
55         String title = getIntent().getStringExtra(EXTRA_TITLE);
56         ((TextView) findViewById(R.id.title)).setText(title);
57 
58         mImageView = new ImageView(this);
59         mImageView.setImageResource(R.drawable.example_big_picture);
60 
61         ImageZoomView zoomView = new ImageZoomView(this, mImageView, mAnimationRange);
62         zoomView.setLayoutParams(new FrameLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT,
63                 ViewGroup.LayoutParams.MATCH_PARENT));
64 
65         ((FrameLayout) findViewById(R.id.container)).addView(zoomView, 0);
66 
67         createNextAnimation(false);
68     }
69 
createNextAnimation(boolean start)70     private void createNextAnimation(boolean start) {
71         float startX = mImageView.getTranslationX();
72         float startY = mImageView.getTranslationY();
73         float endX = -mRandom.nextInt(mAnimationRange);
74         float endY = -mRandom.nextInt(mAnimationRange);
75         float distance = (float) Math.sqrt(Math.pow(endX - startX, 2) + Math.pow(endY - startY, 2));
76 
77         mAnimation = ObjectAnimator.ofPropertyValuesHolder(mImageView,
78                 PropertyValuesHolder.ofFloat("translationX", startX, endX),
79                 PropertyValuesHolder.ofFloat("translationY", startY, endY));
80         mAnimation.setInterpolator(new AccelerateDecelerateInterpolator());
81 
82         mAnimation.setDuration(Math.max(BASE_ANIMATION_DURATION_MS / 10,
83                 (int) (distance * BASE_ANIMATION_DURATION_MS / mAnimationRange)));
84 
85         mAnimation.addListener(new AnimatorListenerAdapter() {
86             @Override
87             public void onAnimationEnd(Animator animation) {
88                 super.onAnimationEnd(animation);
89                 createNextAnimation(true);
90             }
91         });
92         if (start) {
93             mAnimation.start();
94         }
95     }
96 
97     @Override
onResume()98     protected void onResume() {
99         super.onResume();
100         mAnimation.start();
101     }
102 
103     @Override
onPause()104     protected void onPause() {
105         mAnimation.pause();
106         super.onPause();
107     }
108 
109     /** Helper view that zooms in on a child image view */
110     private static class ImageZoomView extends ViewGroup {
111         private final int mZoomLength;
112 
ImageZoomView(Context context, ImageView imageView, int zoomLength)113         public ImageZoomView(Context context, ImageView imageView, int zoomLength) {
114             super(context);
115             addView(imageView);
116             mZoomLength = zoomLength;
117         }
118 
119         @Override
onLayout(boolean changed, int left, int top, int right, int bottom)120         protected void onLayout(boolean changed, int left, int top, int right, int bottom) {
121             ImageView imageView = (ImageView) getChildAt(0);
122 
123             // Resize the image view to be at least mZoomLength pixels larger in both
124             // dimensions than the containing view.
125             int imageWidth = imageView.getDrawable().getIntrinsicWidth();
126             int imageHeight = imageView.getDrawable().getIntrinsicHeight();
127             int minSize = Math.max(right - left, bottom - top) + mZoomLength;
128             if (imageWidth > imageHeight) {
129                 imageWidth = minSize * imageWidth / imageHeight;
130                 imageHeight = minSize;
131             } else {
132                 imageHeight = minSize * imageHeight / imageWidth;
133                 imageWidth = minSize;
134             }
135             imageView.layout(left, top, left + imageWidth, top + imageHeight);
136         }
137     }
138 }
139