-
Notifications
You must be signed in to change notification settings - Fork 0
/
ImageSlider.java
170 lines (136 loc) · 4.23 KB
/
ImageSlider.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
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
package com.plus.bussinessclusters;
import java.util.Random;
import android.content.Context;
import android.graphics.Bitmap;
import android.util.AttributeSet;
import android.view.animation.AccelerateInterpolator;
import android.view.animation.AlphaAnimation;
import android.view.animation.Animation;
import android.view.animation.Animation.AnimationListener;
import android.view.animation.AnimationSet;
import android.view.animation.DecelerateInterpolator;
import android.widget.ImageView;
public class ImageSlider extends ImageView {
private Bitmap[] Images=null;
private int[] ResourceImages=null;
Animation fadeIn = new AlphaAnimation(0, 1);
Animation fadeOut = new AlphaAnimation(1, 0);
AnimationSet animation = new AnimationSet(false); // change to false
int fadeInDuration = 500; // Configure time values here
int timeBetween = 3000;
int fadeOutDuration = 1000;
int ImageIndex=1;
int resourceIndex=1;
int minbetween=3000;
int maxbetween=3000;
boolean square=false;
//constructors
public ImageSlider(Context context) {
super(context);
// TODO Auto-generated constructor stub
}
public ImageSlider(Context context, AttributeSet attrs) {
super(context, attrs);
}
public ImageSlider(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
}
//private funcs
private void CalculateTime()
{
//calculate timeBetween
Random r = new Random();
int Rnd = r.nextInt(maxbetween-minbetween) + minbetween;
timeBetween=Rnd;
}
//Methods
public void SetMinShowtime(int time)
{
this.minbetween=time;
}
public void SetMaxShowtime(int time)
{
this.maxbetween=time;
}
public void keepSquare(boolean state)
{
this.square=state;
}
public void SetImages(Bitmap[] images)
{
this.Images=images;
setImageBitmap(Images[0]);
}
public void SetImages(int[] images)
{
this.ResourceImages=images;
setImageResource(images[0]);
}
//start Methods
private void init()
{
fadeIn.setInterpolator(new DecelerateInterpolator()); // add this
fadeIn.setDuration(fadeInDuration);
fadeOut.setInterpolator(new AccelerateInterpolator()); // and this
fadeOut.setStartOffset(fadeOutDuration+timeBetween);
fadeOut.setDuration(fadeOutDuration);
animation.addAnimation(fadeIn);
animation.addAnimation(fadeOut);
animation.setRepeatCount(1);
}
public void StartBackgroung()
{
CalculateTime();
if(Images.length<=1)
return;
init();
animation.setAnimationListener(new AnimationListener() {
public void onAnimationEnd(Animation animation) {
if(ImageIndex+1>=Images.length)
ImageIndex=0;
else
ImageIndex++;
setImageBitmap(Images[ImageIndex]);
startAnimation(animation);
}
public void onAnimationRepeat(Animation animation) {
// TODO Auto-generated method stub
}
public void onAnimationStart(Animation animation) {
// TODO Auto-generated method stub
}
});
startAnimation(animation);
}
public void StartResource()
{
CalculateTime();
if(ResourceImages.length<=1)
return;
init();
animation.setAnimationListener(new AnimationListener() {
public void onAnimationEnd(Animation animation) {
setImageResource(ResourceImages[resourceIndex]);
if(resourceIndex+1>=ResourceImages.length)
resourceIndex=0;
else
resourceIndex++;
startAnimation(animation);
}
public void onAnimationRepeat(Animation animation) {
// TODO Auto-generated method stub
}
public void onAnimationStart(Animation animation) {
// TODO Auto-generated method stub
}
});
startAnimation(animation);
}
//overrides
@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
super.onMeasure(widthMeasureSpec, heightMeasureSpec);
if(this.square)
setMeasuredDimension(getMeasuredWidth(), getMeasuredWidth()); //Snap to width
}
}