/
PlainColorFragment.java
110 lines (94 loc) · 3.79 KB
/
PlainColorFragment.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
/**
* View Pager Test is a small application to demonstrate how to use
* this excellent class provided by the android compatibility package
* to swap between running parts of an application with your finger.
* Copyright (C) 2011 Alexandro Blanco <ti3r.bubblenet@gmail.com>
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
package org.blanco.tests.viewpager;
import android.graphics.Color;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.widget.AnalogClock;
public class PlainColorFragment extends Fragment {
private static final String PLAIN_COLOR_FRAG_ARG_COLOR = "color";
private static final String PLAIN_COLOR_FRAG_ARG_ALPHA = "alpha";
int color = Color.GREEN;
View view = null;
AnalogClock clock = null;
Bundle viewRecreateState = new Bundle();
public static PlainColorFragment newInstance(int color) {
PlainColorFragment frag = new PlainColorFragment();
Bundle frag1Args = new Bundle();
frag1Args.putInt(PLAIN_COLOR_FRAG_ARG_COLOR, color);
frag.setArguments(frag1Args);
frag.setRetainInstance(true);
return frag;
}
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
this.color = (getArguments() != null) ? getArguments().getInt(PLAIN_COLOR_FRAG_ARG_COLOR) : Color.GRAY;
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
view = inflater.inflate(R.layout.color_layout, container, false);
clock = (AnalogClock) view.findViewById(R.id.analogClock1);
clock.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
vanishClock();
}
});
//If bundle comes from system to recreate saved state put in property
//to be available to restoreClockState
if (savedInstanceState != null)
viewRecreateState.putAll(savedInstanceState);
restoreClockState();
view.setBackgroundColor(color);
return view;
}
@Override
public void onDestroyView() {
viewRecreateState.putFloat(PLAIN_COLOR_FRAG_ARG_ALPHA, clock.getAlpha());
super.onDestroyView();
}
private void restoreClockState() {
float alpha = 1.0f;
if (viewRecreateState != null &&
viewRecreateState.containsKey(PLAIN_COLOR_FRAG_ARG_ALPHA))
alpha = viewRecreateState.getFloat(PLAIN_COLOR_FRAG_ARG_ALPHA);
clock.setAlpha(alpha);
}
public void startAnimationOnClock() {
Animation anim = AnimationUtils.loadAnimation(getActivity(),
android.R.anim.fade_out);
clock.startAnimation(anim);
}
public void vanishClock() {
if (clock.getAlpha() < 0.4f)
clock.setAlpha(1.0f);
else
clock.setAlpha(clock.getAlpha() - .2f);
}
@Override
public void onSaveInstanceState(Bundle outState) {
outState.putFloat(PLAIN_COLOR_FRAG_ARG_ALPHA, clock.getAlpha());
super.onSaveInstanceState(outState);
}
}