Upstream version 7.35.143.0
[platform/framework/web/crosswalk.git] / src / xwalk / runtime / android / sample / src / org / xwalk / core / sample / AnimatableXWalkViewActivity.java
1 // Copyright (c) 2014 Intel Corporation. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4
5 package org.xwalk.core.sample;
6
7 import org.xwalk.core.XWalkView;
8 import org.xwalk.core.XWalkPreferences;
9
10 import android.animation.Animator;
11 import android.animation.AnimatorListenerAdapter;
12 import android.animation.AnimatorSet;
13 import android.animation.ObjectAnimator;
14 import android.app.Activity;
15 import android.os.Bundle;
16 import android.widget.Button;
17 import android.widget.LinearLayout;
18 import android.view.View;
19
20 /**
21  * Sample code to show how to use ANIMATED_XWALK_VIEW preference key to create
22  * animated XWalkView and apply alpha animation or scale animation on it.
23  */
24 public class AnimatableXWalkViewActivity extends XWalkBaseActivity {
25     private final static float ANIMATION_FACTOR = 0.6f;
26     private Button mRunAnimationButton;
27
28     private void startAnimation() {
29         AnimatorSet combo = new AnimatorSet();
30
31         float targetAlpha = mXWalkView.getAlpha() == 1.f ? ANIMATION_FACTOR : 1.f;
32         float targetScaleFactor = mXWalkView.getScaleX() == 1.f ? ANIMATION_FACTOR : 1.f;
33
34         ObjectAnimator fade = ObjectAnimator.ofFloat(mXWalkView,
35                 "alpha", mXWalkView.getAlpha(), targetAlpha);
36         ObjectAnimator scaleX = ObjectAnimator.ofFloat(mXWalkView,
37                 "scaleX", mXWalkView.getScaleX(), targetScaleFactor);
38         ObjectAnimator scaleY = ObjectAnimator.ofFloat(mXWalkView,
39                 "scaleY", mXWalkView.getScaleY(), targetScaleFactor);
40
41         combo.setDuration(400);
42         combo.playTogether(fade, scaleX, scaleY);
43         combo.start();
44     }
45
46     @Override
47     protected void onCreate(Bundle savedInstanceState) {
48         super.onCreate(savedInstanceState);
49
50         // ANIMATABLE_XWALK_VIEW preference key MUST be set before XWalkView creation.
51         XWalkPreferences.setValue(XWalkPreferences.ANIMATABLE_XWALK_VIEW, true);
52
53         setContentView(R.layout.animatable_xwview_layout);
54
55         mRunAnimationButton = (Button) findViewById(R.id.run_animation);
56         mRunAnimationButton.setOnClickListener(new View.OnClickListener() {
57             @Override
58             public void onClick(View v) {
59                 startAnimation();
60             }
61         });
62
63         mXWalkView = (XWalkView) findViewById(R.id.xwalkview);
64         mXWalkView.load("http://www.baidu.com", null);
65     }
66
67     @Override
68     public void onDestroy() {
69         super.onDestroy();
70
71         // Reset the preference for animatable XWalkView.
72         XWalkPreferences.setValue(XWalkPreferences.ANIMATABLE_XWALK_VIEW, false);
73     }
74 }