1 /*
2  * Copyright (C) 2013 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.insertingcells;
18 
19 import android.animation.ObjectAnimator;
20 import android.animation.PropertyValuesHolder;
21 import android.animation.ValueAnimator;
22 import android.content.Context;
23 import android.graphics.Canvas;
24 import android.graphics.Color;
25 import android.graphics.Paint;
26 import android.util.AttributeSet;
27 import android.view.View;
28 
29 /**
30  * This round view draws a circle from which the image pops out of and into
31  * the corresponding cell in the list.
32  */
33 public class RoundView extends View {
34 
35     private final int STROKE_WIDTH = 6;
36     private final int RADIUS = 20;
37     private final int ANIMATION_DURATION = 300;
38     private final float SCALE_FACTOR = 0.3f;
39 
40     private Paint mPaint;
41 
RoundView(Context context)42     public RoundView(Context context) {
43         super(context);
44         init();
45     }
46 
RoundView(Context context, AttributeSet attrs)47     public RoundView(Context context, AttributeSet attrs) {
48         super(context, attrs);
49         init();
50     }
51 
RoundView(Context context, AttributeSet attrs, int defStyle)52     public RoundView(Context context, AttributeSet attrs, int defStyle) {
53         super(context, attrs, defStyle);
54         init();
55     }
56 
init()57     private void init() {
58         mPaint = new Paint();
59         mPaint.setAntiAlias(true);
60         mPaint.setColor(Color.WHITE);
61         mPaint.setStyle(Paint.Style.STROKE);
62         mPaint.setStrokeWidth(STROKE_WIDTH);
63     }
64 
65     @Override
onDraw(Canvas canvas)66     protected void onDraw(Canvas canvas) {
67         canvas.drawCircle(canvas.getWidth() / 2, canvas.getHeight() / 2,
68                 RADIUS, mPaint);
69     }
70 
getScalingAnimator()71     public ObjectAnimator getScalingAnimator() {
72         PropertyValuesHolder imgViewScaleY = PropertyValuesHolder.ofFloat(View
73                 .SCALE_Y, SCALE_FACTOR);
74         PropertyValuesHolder imgViewScaleX = PropertyValuesHolder.ofFloat(View
75                 .SCALE_X, SCALE_FACTOR);
76 
77         ObjectAnimator imgViewScaleAnimator = ObjectAnimator
78                 .ofPropertyValuesHolder(this, imgViewScaleX, imgViewScaleY);
79         imgViewScaleAnimator.setRepeatCount(1);
80         imgViewScaleAnimator.setRepeatMode(ValueAnimator.REVERSE);
81         imgViewScaleAnimator.setDuration(ANIMATION_DURATION);
82 
83         return imgViewScaleAnimator;
84     }
85 }
86