Skip to content

Commit 8ed21ea

Browse files
Fixed #7 -- Stop rotation after one cycle (#28)
Rotations can be stopped after the no. of cycles user has specified. Co-authored-by: Ritik kumar <[email protected]>
1 parent 29a3bf0 commit 8ed21ea

File tree

4 files changed

+135
-20
lines changed

4 files changed

+135
-20
lines changed

app/src/main/java/com/sdsmdg/harjot/rotatingtextlibrary/MainActivity.java

Lines changed: 41 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
import android.os.Bundle;
66
import android.support.v7.app.AppCompatActivity;
77
import android.text.TextUtils;
8+
89
import android.view.View;
910
import android.view.animation.AccelerateInterpolator;
1011
import android.view.animation.DecelerateInterpolator;
@@ -13,6 +14,7 @@
1314
import android.widget.EditText;
1415
import android.widget.Spinner;
1516

17+
import com.sdsmdg.harjot.rotatingtext.RotatingTextSwitcher;
1618
import com.sdsmdg.harjot.rotatingtext.RotatingTextWrapper;
1719
import com.sdsmdg.harjot.rotatingtext.models.Rotatable;
1820

@@ -22,8 +24,12 @@
2224
public class MainActivity extends AppCompatActivity {
2325

2426
RotatingTextWrapper rotatingTextWrapper;
27+
RotatingTextSwitcher rotatingTextSwitcher;
2528
Rotatable rotatable, rotatable2;
2629
Spinner s1;
30+
String word;
31+
int nCycles;
32+
EditText enterCycles;
2733
EditText e1;
2834

2935
Button button;
@@ -33,6 +39,7 @@ protected void onCreate(Bundle savedInstanceState) {
3339
super.onCreate(savedInstanceState);
3440
setContentView(R.layout.activity_main);
3541

42+
rotatingTextSwitcher = new RotatingTextSwitcher(this);
3643
Typeface typeface = Typeface.createFromAsset(getAssets(), "fonts/Raleway-Light.ttf");
3744
Typeface typeface2 = Typeface.createFromAsset(getAssets(), "fonts/Reckoner_Bold.ttf");
3845

@@ -41,26 +48,33 @@ protected void onCreate(Bundle savedInstanceState) {
4148
rotatingTextWrapper.setTypeface(typeface2);
4249

4350
// rotatable = new Rotatable(Color.parseColor("#FFA036"), 1000, "Word00", "Word01", "Word02");
51+
rotatable2 = new Rotatable(Color.parseColor("#123456"), 1000, "word", "word01", "word02");
52+
rotatable2.setSize(25);
53+
rotatable2.setTypeface(typeface);
54+
rotatable2.setInterpolator(new DecelerateInterpolator());
55+
rotatable2.setAnimationDuration(500);
56+
57+
4458
rotatable = new Rotatable(Color.parseColor("#FFA036"), 1000, "rotating", "text", "library");
4559
rotatable.setSize(25);
4660
rotatable.setTypeface(typeface);
4761
rotatable.setInterpolator(new AccelerateInterpolator());
4862
rotatable.setAnimationDuration(500);
4963

50-
rotatable2 = new Rotatable(Color.parseColor("#123456"), 1000, "word", "word01", "word02");
51-
rotatable2.setSize(25);
52-
rotatable2.setTypeface(typeface);
53-
rotatable2.setInterpolator(new DecelerateInterpolator());
54-
rotatable2.setAnimationDuration(500);
64+
word = rotatable.getTextAt(0);
5565

5666
rotatingTextWrapper.setContent("?abc ? abc", rotatable, rotatable2);
5767
// rotatingTextWrapper.setContent("? abc", rotatable);
5868

5969
s1 = (Spinner) findViewById(R.id.spinner);
70+
71+
72+
6073
List<Integer> list = new ArrayList<Integer>();
6174
list.add(1);
6275
list.add(2);
6376
list.add(3);
77+
6478
ArrayAdapter<Integer> dataAdapter = new ArrayAdapter<Integer>(this,
6579
android.R.layout.simple_spinner_item, list);
6680
dataAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
@@ -80,6 +94,26 @@ public void onClick(View view) {
8094
}
8195
}
8296
});
97+
98+
Button button2 = findViewById(R.id.set_button);
99+
button2.setOnClickListener(new View.OnClickListener() {
100+
@Override
101+
public void onClick(View view) {
102+
enterCycles = (EditText) findViewById(R.id.enterCycles);
103+
104+
105+
if(!enterCycles.getText().toString().equals("") && enterCycles.getText() != null) {
106+
nCycles = Integer.parseInt(enterCycles.getText().toString());
107+
}
108+
109+
rotatable.setInitialWord(word);
110+
rotatable.setCycles(nCycles);
111+
112+
if (rotatingTextWrapper.getSwitcherList().get(0).isPaused()) {
113+
rotatingTextWrapper.resume(0);
114+
}
115+
}
116+
});
83117
}
84118

85119
public void replaceWord(View view) {
@@ -90,5 +124,7 @@ public void replaceWord(View view) {
90124
rotatingTextWrapper.setAdaptable(true);
91125
rotatingTextWrapper.addWord(0, (int) s1.getSelectedItem() - 1, newWord);
92126
}
127+
93128
}
94129
}
130+

app/src/main/res/layout/activity_main.xml

Lines changed: 43 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@
3434
android:padding="5dp">
3535

3636
</Spinner>
37+
3738
</LinearLayout>
3839

3940
<Button
@@ -44,12 +45,47 @@
4445
android:onClick="replaceWord"
4546
android:text="replace" />
4647

47-
<Button
48-
android:id="@+id/pause_button"
49-
android:layout_width="wrap_content"
50-
android:layout_height="wrap_content"
51-
android:layout_alignParentBottom="true"
52-
android:layout_centerHorizontal="true"
53-
android:text="Pause/Resume" />
48+
<RelativeLayout
49+
android:layout_width="match_parent"
50+
android:layout_height="match_parent"
51+
android:layout_alignParentBottom="true">
52+
53+
<Button
54+
android:id="@+id/pause_button"
55+
android:layout_width="wrap_content"
56+
android:layout_height="wrap_content"
57+
android:layout_alignParentBottom="true"
58+
android:text="pause/resume" />
59+
60+
61+
<EditText
62+
android:id="@+id/enterCycles"
63+
android:layout_width="wrap_content"
64+
android:layout_height="wrap_content"
65+
android:layout_marginLeft="10dp"
66+
android:textSize="15sp"
67+
android:layout_marginRight="10dp"
68+
69+
70+
android:layout_alignParentBottom="true"
71+
72+
android:hint="No. of cycles"
73+
android:ems="10"
74+
android:layout_toRightOf="@id/pause_button"
75+
android:layout_toLeftOf="@id/set_button"
76+
android:inputType="number"
77+
android:layout_toEndOf="@id/pause_button"
78+
android:layout_toStartOf="@id/set_button" />
79+
80+
<Button
81+
android:id="@+id/set_button"
82+
android:layout_width="wrap_content"
83+
android:layout_height="wrap_content"
84+
android:text="Set"
85+
android:layout_alignParentBottom="true"
86+
android:layout_alignParentRight="true"
87+
/>
88+
</RelativeLayout>
89+
5490

5591
</RelativeLayout>

rotatingtext/src/main/java/com/sdsmdg/harjot/rotatingtext/RotatingTextSwitcher.java

Lines changed: 23 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -125,7 +125,8 @@ public void run() {
125125
public void run() {
126126
if (isPaused) {
127127
pauseRender();
128-
} else {
128+
}
129+
else {
129130
animationInterface.setAnimationRunning(true);
130131
resumeRender();
131132
animateInHorizontal();
@@ -324,14 +325,28 @@ public void run() {
324325
public void run() {
325326
if (isPaused) {
326327
pauseRender();
327-
} else {
328-
oldText = currentText;
329-
currentText = rotatable.getNextWord();
330-
animationInterface.setAnimationRunning(true);
331-
resumeRender();
332-
animateInHorizontal();
333-
animateOutHorizontal();
328+
}
334329

330+
else {
331+
332+
if (currentText.equals(rotatable.getInitialWord()) && rotatable.getCycles() != 0) {
333+
rotatable.countCycles(true);
334+
}
335+
336+
if (rotatable.countCycles(false) >= rotatable.getCycles()+1 && rotatable.getCycles() != 0) {
337+
rotatable.setCycles(0);
338+
isPaused = true;
339+
pauseRender();
340+
}
341+
342+
else {
343+
oldText = currentText;
344+
currentText = rotatable.getNextWord();
345+
animationInterface.setAnimationRunning(true);
346+
resumeRender();
347+
animateInHorizontal();
348+
animateOutHorizontal();
349+
}
335350
}
336351
}
337352
});

rotatingtext/src/main/java/com/sdsmdg/harjot/rotatingtext/models/Rotatable.java

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,10 @@ public class Rotatable {
3434

3535
private int FPS = 60;
3636

37+
private int nCycles = 0, countCycles = 0;
38+
private String initialWord = "";
39+
40+
3741
public Rotatable(int updateDuration, String... text) {
3842
this.updateDuration = updateDuration;
3943
this.text = text;
@@ -56,6 +60,30 @@ public void setColor(int color) {
5660
setUpdated(true);
5761
}
5862

63+
public int countCycles(boolean bool) {
64+
if(bool) {
65+
countCycles = countCycles + 1;
66+
}
67+
return countCycles;
68+
}
69+
70+
public void setCycles(int val) {
71+
this.nCycles = val;
72+
countCycles = 0;
73+
}
74+
75+
public int getCycles() {
76+
return nCycles;
77+
}
78+
79+
public void setInitialWord(String initialWord) {
80+
this.initialWord = initialWord;
81+
}
82+
83+
public String getInitialWord() {
84+
return initialWord;
85+
}
86+
5987
public String[] getText() {
6088
return text;
6189
}

0 commit comments

Comments
 (0)