I have four screen/layouts on main activity and it works when I swipe, I have implemented
@Override
public void onPageScrolled(int position, float positionOffset,
int positionOffsetPixels){}
and t works (animate like cube).
For one switch between layouts I have ten times called ( I Log.i) onPageScrolled function and I record all values. I am trying to implement animation on click of menu button.
I tried like
public void automaticAnimate(final int next, final int current, final int direction) {
Thread thread = new Thread() {
public void run() {
int steps = Math.abs(current - next);
if (steps > 0) {
if (direction == TO_LEFT) {
float leftPositionOffset[] = new float[] { 0.98796296f,
0.886111114f, 0.49444443f, 0.44537038f,
0.37314814f, 0.21018519f, 0.11388889f,
0.05925926f, 0.026851851f, 0.010185185f,
0.0037037036f, 0.000001f, 0.0f };
int leftPositionOffsetPixels[] = new int[] { 1067, 957,
534, 481, 403, 227, 123, 64, 29, 11, 4, 1, 0 };
for (int i = 0; i < leftPositionOffset.length; i++) {
int panel = (i == 0) ? current : current - 1;
onPageScrolled(panel, leftPositionOffset[i],
leftPositionOffsetPixels[i]);
try {
Thread.sleep(30);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
} else {
float rightPositionOffset[] = new float[] {
0.004629612f, 0.020370483f, 0.07407403f,
0.17314816f, 0.27592587f, 0.44537044f,
0.54444456f, 0.69722223f, 0.70370364f,
0.7907407f, 0.86018515f, 0.91203713f,
0.94537044f, 0.9694445f, 0.98333335f,
0.9916666f, 0.9972222f, 0.99999f, 0.0f };
int rightPositionOffsetPixels[] = new int[] { 4, 22,
79, 187, 297, 481, 588, 748, 753, 759, 854,
928, 985, 1021, 1047, 1062, 1070, 1077, 1078, 0 };
for (int i = 0; i < rightPositionOffset.length; i++) {
int panel = (i == (rightPositionOffset.length - 1)) ? current + 1
: current;
onPageScrolled(panel, rightPositionOffset[i],
rightPositionOffsetPixels[i]);
try {
Thread.sleep(30);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}
}
};
thread.start();
}
but I am getting error because I cannot call onPageScrolled
from not UI thread
. How to call onPageScrolled
(that is going to put layouts on good positions for that moment) from not UI thread ?
You can't touch UI elements from threads other than the main (UI) thread. This is what AsyncTask is for:
public void automaticAnimate(final int next, final int current, final int direction) {
new AsyncTask<Void, Object, Void>() {
@Override
protected void onProgressUpdate(Object... values) {
int position = (Integer) values[0];
float positionOffset = (Float) values[1];
int positionOffsetPixels = (Integer) values[3];
onPageScrolled(position, positionOffset,
positionOffsetPixels);
}
@Override
protected Void doInBackground(Void... params) {
int steps = Math.abs(current - next);
if (steps > 0) {
if (direction == TO_LEFT) {
float leftPositionOffset[] = new float[] { 0.98796296f,
0.886111114f, 0.49444443f, 0.44537038f,
0.37314814f, 0.21018519f, 0.11388889f,
0.05925926f, 0.026851851f, 0.010185185f,
0.0037037036f, 0.000001f, 0.0f };
int leftPositionOffsetPixels[] = new int[] { 1067, 957,
534, 481, 403, 227, 123, 64, 29, 11, 4, 1, 0 };
for (int i = 0; i < leftPositionOffset.length; i++) {
int panel = (i == 0) ? current : current - 1;
publishProgress(panel, leftPositionOffset[i],
leftPositionOffsetPixels[i]);
try {
Thread.sleep(30);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
} else {
float rightPositionOffset[] = new float[] {
0.004629612f, 0.020370483f, 0.07407403f,
0.17314816f, 0.27592587f, 0.44537044f,
0.54444456f, 0.69722223f, 0.70370364f,
0.7907407f, 0.86018515f, 0.91203713f,
0.94537044f, 0.9694445f, 0.98333335f,
0.9916666f, 0.9972222f, 0.99999f, 0.0f };
int rightPositionOffsetPixels[] = new int[] { 4, 22,
79, 187, 297, 481, 588, 748, 753, 759, 854,
928, 985, 1021, 1047, 1062, 1070, 1077, 1078, 0 };
for (int i = 0; i < rightPositionOffset.length; i++) {
int panel = (i == (rightPositionOffset.length - 1)) ? current + 1
: current;
publishProgress(panel, rightPositionOffset[i],
rightPositionOffsetPixels[i]);
try {
Thread.sleep(30);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}
return null;
}
}.execute();
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.