編輯:中級開發
<?XML version="1.0" encoding="utf-8"?>
<LinearLayout XMLns:android="http://schemas.android.com/apk/res/android"
android:orIEntation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="#ffffff"
android:id="@+id/layout_main"
>
<VIEwFlipper android:id="@+id/details"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<LinearLayout
android:orIEntation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="#ffffff">
<TextVIEw android:id="@+id/tv_country"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:textColor="#000000"
android:textStyle="bold"
android:textSize="18px"
android:text="Country" >
</TextVIEw>
<Spinner android:text=""
android:id="@+id/spinner_country"
android:layout_width="200px"
android:layout_height="55px">
</Spinner>
</LinearLayout>
<LinearLayout
android:orIEntation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="#ffffff">
<TextVIEw android:id="@+id/tv_income"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:textColor="#000000"
android:textStyle="bold"
android:textSize="18px"
android:text="Income" >
</TextVIEw>
<EditText android:text=""
android:id="@+id/et_income"
android:layout_width="200px"
android:layout_height="55px">
</EditText>
</LinearLayout>
</VIEwFlipper>
</LinearLayout>這個代碼只是去掉了兩個button,另外要注意的是加了一句android:id="@+id/layout_main"
因為我們要在這個層次上進行動作設置:
Java代碼
1.public class Activity1 extends Activity implements OnTouchListener{
2.
3. float downXValue;
4.
5. /** Called when the activity is first created. */
6. @Override
7. public void onCreate(Bundle savedInstanceState) {
8. super.onCreate(savedInstanceState);
9.
10. // Set main.XML as the layout for this Activity
11. setContentVIEw(R.layout.main);
12.
13. // Add these two lines
14. LinearLayout layMain = (LinearLayout) findVIEwById(R.id.layout_main);
15. layMain.setOnTouchListener((OnTouchListener) this);
16.
17. // Add a few countrIEs to the spinner
18. Spinner spinnerCountries = (Spinner) findVIEwById(R.id.spinner_country);
19. ArrayAdapter countryArrayAdapter = new ArrayAdapter(this,
20. android.R.layout.simple_spinner_dropdown_item,
21. new String[] { "Canada", "USA" });
22. spinnerCountrIEs.setAdapter(countryArrayAdapter);
23.
24. }
25.
26. public boolean onTouch(VIEw arg0, MotionEvent arg1) {
27.
28. // Get the action that was done on this touch event
29. switch (arg1.getAction())
30. {
31. case MotionEvent.ACTION_DOWN:
32. {
33. // store the X value when the user's finger was pressed down
34. downXValue = arg1.getX();
35. break;
36. }
37.
38. case MotionEvent.ACTION_UP:
39. {
40. // Get the X value when the user released his/her finger
41. float currentX = arg1.getX();
42.
43. // going backwards: pushing stuff to the right
44. if (downXValue < currentX)
45. {
46. // Get a reference to the VIEwFlipper
47. ViewFlipper vf = (ViewFlipper) findVIEwById(R.id.details);
48. // Set the animation
49. vf.setAnimation(AnimationUtils.loadAnimation(this, R.anim.push_left_out));
50. // Flip!
51. vf.showPrevious();
52. }
53.
54. // going forwards: pushing stuff to the left
55. if (downXValue > currentX)
56. {
57. // Get a reference to the VIEwFlipper
58. ViewFlipper vf = (ViewFlipper) findVIEwById(R.id.details);
59. // Set the animation
60. vf.setInAnimation(AnimationUtils.loadAnimation(this, R.anim.push_left_in));
61. // Flip!
62. vf.showNext();
63. }
64. break;
65. }
66. }
67.
68. // if you return false, these actions will not be recorded
69. return true;
70. }
public class Activity1 extends Activity implements OnTouchListener{
float downXValue;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// Set main.XML as the layout for this Activity
setContentVIEw(R.layout.main);
// Add these two lines
LinearLayout layMain = (LinearLayout) findVIEwById(R.id.layout_main);
layMain.setOnTouchListener((OnTouchListener) this);
// Add a few countrIEs to the spinner
Spinner spinnerCountries = (Spinner) findVIEwById(R.id.spinner_country);
ArrayAdapter countryArrayAdapter = new ArrayAdapter(this,
android.R.layout.simple_spinner_dropdown_item,
new String[] { "Canada", "USA" });
spinnerCountrIEs.setAdapter(countryArrayAdapter);
}
public boolean onTouch(VIEw arg0, MotionEvent arg1) {
// Get the action that was done on this touch event
switch (arg1.getAction())
{
case MotionEvent.ACTION_DOWN:
{
// store the X value when the user's finger was pressed down
downXValue = arg1.getX();
break;
}
case MotionEvent.ACTION_UP:
{
// Get the X value when the user released his/her finger
float currentX = arg1.getX();
// going backwards: pushing stuff to the right
if (downXValue < currentX)
{
// Get a reference to the VIEwFlipper
ViewFlipper vf = (ViewFlipper) findVIEwById(R.id.details);
// Set the animation
vf.setAnimation(AnimationUtils.loadAnimation(this, R.anim.push_left_out));
// Flip!
vf.showPrevious();
}
// going forwards: pushing stuff to the left
if (downXValue > currentX)
{
// Get a reference to the VIEwFlipper
ViewFlipper vf = (ViewFlipper) findVIEwById(R.id.details);
// Set the animation
vf.setInAnimation(AnimationUtils.loadAnimation(this, R.anim.push_left_in));
// Flip!
vf.showNext();
}
break;
}
}
// if you return false, these actions will not be recorded
return true;
}
上面的代碼實現的是通過ontouchListener方法,通過該方法我們判斷鼠標的摁下和松開之後的變化來實現動畫。
那麼如何通過手勢事件呢?
Java代碼
1.public class Main extends Activity {
2.
3. private static final int SWIPE_MIN_DISTANCE = 120;
4. private static final int SWIPE_MAX_OFF_PATH = 250;
5. private static final int SWIPE_THRESHOLD_VELOCITY = 200;
6. private GestureDetector gestureDetector;
7. VIEw.OnTouchListener gestureListener;
8. private Animation slideLeftIn;
9. private Animation slideLeftOut;
10. private Animation slideRightIn;
11. private Animation slideRightOut;
12. private ViewFlipper vIEwFlipper;
13. @Override
14. public void onCreate(Bundle savedInstanceState) {
15. super.onCreate(savedInstanceState);
16. setContentVIEw(R.layout.main);
17. viewFlipper = (ViewFlipper)findVIEwById(R.id.layout_main);
18. slideLeftIn = AnimationUtils.loadAnimation(this, R.anim.slide_left_in);
19. slideLeftOut = AnimationUtils.loadAnimation(this, R.anim.slide_left_out);
20. slideRightIn = AnimationUtils.loadAnimation(this, R.anim.slide_right_in);
21. slideRightOut = AnimationUtils.loadAnimation(this, R.anim.slide_right_out);
22.
23. gestureDetector = new GestureDetector(new MyGestureDetector());
24. gestureListener = new VIEw.OnTouchListener() {
25. public boolean onTouch(VIEw v, MotionEvent event) {
26. if (gestureDetector.onTouchEvent(event)) {
27. return true;
28. }
29. return false;
30. }
31. };
32. }
33. class MyGestureDetector extends SimpleOnGestureListener {
34. @Override
35. public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX, float velocityY) {
36. try {
37. if (Math.abs(e1.getY() - e2.getY()) > SWIPE_MAX_OFF_PATH)
38. return false;
39. // right to left swipe
40. if(e1.getX() - e2.getX() > SWIPE_MIN_DISTANCE && Math.abs(velocityX) > SWIPE_THRESHOLD_VELOCITY) {
41. vIEwFlipper.setInAnimation(slideLeftIn);
42. vIEwFlipper.setOutAnimation(slideLeftOut);
43. vIEwFlipper.showNext();
44. } else if (e2.getX() - e1.getX() > SWIPE_MIN_DISTANCE && Math.abs(velocityX) > SWIPE_THRESHOLD_VELOCITY) {
45. vIEwFlipper.setInAnimation(slideRightIn);
46. vIEwFlipper.setOutAnimation(slideRightOut);
47. vIEwFlipper.showPrevious();
48. }
49. } catch (Exception e) {
50. // nothing
51. }
52. return false;
53. }
54. }
55.
56. @Override
57. public boolean onTouchEvent(MotionEvent event) {
58. if (gestureDetector.onTouchEvent(event))
59. return true;
60. else
61. return false;
62. }
public class Main extends Activity {
private static final int SWIPE_MIN_DISTANCE = 120;
private static final int SWIPE_MAX_OFF_PATH = 250;
private static final int SWIPE_THRESHOLD_VELOCITY = 200;
private GestureDetector gestureDetector;
VIEw.OnTouchListener gestureListener;
private Animation slideLeftIn;
private Animation slideLeftOut;
private Animation slideRightIn;
private Animation slideRightOut;
private ViewFlipper vIEwFlipper;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentVIEw(R.layout.main);
viewFlipper = (ViewFlipper)findVIEwById(R.id.layout_main);
slideLeftIn = AnimationUtils.loadAnimation(this, R.anim.slide_left_in);
slideLeftOut = AnimationUtils.loadAnimation(this, R.anim.slide_left_out);
slideRightIn = AnimationUtils.loadAnimation(this, R.anim.slide_right_in);
slideRightOut = AnimationUtils.loadAnimation(this, R.anim.slide_right_out);
gestureDetector = new GestureDetector(new MyGestureDetector());
gestureListener = new VIEw.OnTouchListener() {
public boolean onTouch(VIEw v, MotionEvent event) {
if (gestureDetector.onTouchEvent(event)) {
return true;
}
return false;
}
};
}
class MyGestureDetector extends SimpleOnGestureListener {
@Override
public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX, float velocityY) {
try {
if (Math.abs(e1.getY() - e2.getY()) > SWIPE_MAX_OFF_PATH)
return false;
// right to left swipe
if(e1.getX() - e2.getX() > SWIPE_MIN_DISTANCE && Math.abs(velocityX) > SWIPE_THRESHOLD_VELOCITY) {
vIEwFlipper.setInAnimation(slideLeftIn);
vIEwFlipper.setOutAnimation(slideLeftOut);
vIEwFlipper.showNext();
} else if (e2.getX() - e1.getX() > SWIPE_MIN_DISTANCE && Math.abs(velocityX) > SWIPE_THRESHOLD_VELOCITY) {
vIEwFlipper.setInAnimation(slideRightIn);
vIEwFlipper.setOutAnimation(slideRightOut);
vIEwFlipper.showPrevious();
}
} catch (Exception e) {
// nothing
}
return false;
}
}
@Override
public boolean onTouchEvent(MotionEvent event) {
if (gestureDetector.onTouchEvent(event))
return true;
else
return false;
} 該方法通過SimpleOnGestureListener 來實現,主要通過獲得坐標餓方法實現
簡介: BFS 是一款專門為 Linux 桌面環境所設計的內核調度器,它基於 Staircase Deadline 和 EEVDF 算法,支持 Linux 2
簡介: 在這個由五個部分所組成的系列的第一部分中,您將接觸到移動 Web 應用程序中最流行的新技術:地理定位。高端智能手機都內置 GPS,現在您將了解 Web
簡介: HTML 5 針對移動 Web 應用程序引入了大量新特性,其中包括一些可視化特性,它們通常會帶來強烈的視覺沖擊。Canvas 是最引人注目的新 UI
簡介: 對於需要跨應用程序執行期間或生命期而維護重要信息的應用程序來說,能夠在移動設備上本地存儲數據是一種非常關鍵的功能。作為一名開發人員,您經常需要存儲諸如