/
MyLocationsDataAdapter.java
154 lines (110 loc) · 4.97 KB
/
MyLocationsDataAdapter.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
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
package com.arpitonline.worldclock;
import java.util.ArrayList;
import org.joda.time.DateTime;
import org.joda.time.LocalTime;
import android.app.Activity;
import android.content.Context;
import android.graphics.Shader.TileMode;
import android.graphics.drawable.BitmapDrawable;
import android.view.ContextMenu;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.animation.Animation;
import android.view.animation.DecelerateInterpolator;
import android.view.animation.RotateAnimation;
import android.view.animation.TranslateAnimation;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;
import com.arpitonline.utils.MathUtils;
import com.arpitonline.worldclock.models.LocationVO;
public class MyLocationsDataAdapter extends CountriesAdapter implements View.OnCreateContextMenuListener{
public static boolean INTRO_ANIMATION_ENABLED = true;
private ArrayList<LocationVO> previouslyAnimated = new ArrayList<LocationVO>();
public MyLocationsDataAdapter (Activity activity, int textViewResourceId, ArrayList<LocationVO> items, int renderer){
super(activity, textViewResourceId, items, renderer);
}
public View getView(int position, View convertView, ViewGroup parent) {
LocationVO o = items.get(position);
if(o==null){
return null;
}
View v = convertView;
if (v == null) {
LayoutInflater vi = (LayoutInflater)(getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE));
v = vi.inflate(renderer, null);
v.setOnCreateContextMenuListener(this);
}
DateTime dateTime = o.getDateTime();
LocalTime time = dateTime.toLocalTime();
TextView cityTF = (TextView) v.findViewById(R.id.cityTF);
cityTF.setText(o.cityName);
TextView timeTF = (TextView) v.findViewById(R.id.currentTimeTF);
timeTF.setText(o.getFormattedTime());
TextView countryTF = (TextView) v.findViewById(R.id.dayTF);
countryTF.setText(dateTime.dayOfWeek().getAsText());
BitmapDrawable bg;
BitmapDrawable celestialImg;
if(time.getHourOfDay() >= 18 || time.getHourOfDay() < 6){
bg = (BitmapDrawable)this.getContext().getResources().getDrawable(R.drawable.night_bg);
celestialImg = (BitmapDrawable)this.getContext().getResources().getDrawable(R.drawable.moon);
}
else{
bg = (BitmapDrawable)this.getContext().getResources().getDrawable(R.drawable.day_bg);
celestialImg = (BitmapDrawable)this.getContext().getResources().getDrawable(R.drawable.sun);
}
RelativeLayout item = (RelativeLayout)v.findViewById(R.id.item_layout);
bg.setTileModeX(TileMode.REPEAT);
item.setBackgroundDrawable(bg);
final ImageView celestial = (ImageView)v.findViewById(R.id.celestial);
celestial.setImageDrawable(celestialImg);
//Log.i(TimelyApp.WORLD_CLOCK, "intro animation enabled? "+INTRO_ANIMATION_ENABLED);
//if(INTRO_ANIMATION_ENABLED == true){
if(previouslyAnimated.contains(o)== false){
previouslyAnimated.add(o);
RelativeLayout.LayoutParams params = (RelativeLayout.LayoutParams)celestial.getLayoutParams();
params.rightMargin = MathUtils.randRange(25, 50);
celestial.setLayoutParams(params);
TranslateAnimation anim = new TranslateAnimation(Animation.RELATIVE_TO_SELF, 0, Animation.RELATIVE_TO_SELF, 0,
Animation.RELATIVE_TO_SELF, 10.0f, Animation.RELATIVE_TO_SELF, 0);
anim.setDuration(2000);
anim.setInterpolator(new DecelerateInterpolator());
//anim.setFillAfter(true);
anim.setAnimationListener(new Animation.AnimationListener() {
@Override
public void onAnimationStart(Animation animation) {
// TODO Auto-generated method stub
}
@Override
public void onAnimationRepeat(Animation animation) {
// TODO Auto-generated method stub
}
@Override
public void onAnimationEnd(Animation animation) {
MyLocationsDataAdapter.INTRO_ANIMATION_ENABLED = false;
doRotationAnimation(celestial);
}
});
celestial.setAnimation(anim);
}else{
//doRotationAnimation(celestial);
}
return v;
}
private void doRotationAnimation(ImageView celestial){
RotateAnimation r = new RotateAnimation(0, 40, Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF, 0.5f);
r.setDuration((MathUtils.randRange(4, 8)/2)*1000);
//r.setStartTime(Animation.START_ON_FIRST_FRAME+10000);
r.setRepeatCount(Animation.INFINITE);
r.setRepeatMode(Animation.REVERSE);
celestial.clearAnimation();
celestial.setAnimation(r);
}
public void removeFromAnimatedObjects(LocationVO loc){
this.previouslyAnimated.remove(loc);
}
public void onCreateContextMenu(ContextMenu contextMenu, View view, ContextMenu.ContextMenuInfo contextMenuInfo) {
// empty implementation
}
}