本文实例讲述了Android ViewFlipper用法。分享给大家供大家参考,具体如下:
这里实现的效果是当手动滑动手机屏幕时会一个一个地显示图片,一次显示一张图片
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
|
package com.my.viewflippertest; import android.app.Activity; import android.os.Bundle; import android.view.GestureDetector; import android.view.GestureDetector.OnGestureListener; import android.view.MotionEvent; import android.widget.TextView; import android.widget.ViewFlipper; public class ViewFlipperTestActivity extends Activity implements OnGestureListener { private ViewFlipper vf; //声明一个ViewFlipper对象 private GestureDetector gd; private int [] d={ //定义一个图片集合 R.drawable.a, R.drawable.b, R.drawable.c, R.drawable.d, R.drawable.e, R.drawable.f, R.drawable.h, R.drawable.i, R.drawable.g }; /** Called when the activity is first created. */ @Override public void onCreate(Bundle savedInstanceState) { super .onCreate(savedInstanceState); setContentView(R.layout.main); gd= new GestureDetector( this ); vf=(ViewFlipper) this .findViewById(R.id.vf); //初始化对象 for ( int i= 0 ;i<d.length;i++){ vf.addView(addTextView(d[i])); //添加界面 } } public TextView addTextView( int a){ //把图片加入TextView中 TextView tv= new TextView( this ); tv.setBackgroundResource(a); return tv; } //触发接触事件 public boolean onTouchEvent(MotionEvent event){ return this .gd.onTouchEvent(event); } //接触事件发生后显示下一张图片 public boolean onFling(MotionEvent e1,MotionEvent e2, float velocityX, float velocityY){ this .vf.showNext(); return true ; } @Override public boolean onDown(MotionEvent e) { // TODO Auto-generated method stub return false ; } @Override public void onLongPress(MotionEvent e) { // TODO Auto-generated method stub } @Override public boolean onScroll(MotionEvent e1, MotionEvent e2, float distanceX, float distanceY) { // TODO Auto-generated method stub return false ; } @Override public void onShowPress(MotionEvent e) { // TODO Auto-generated method stub } @Override public boolean onSingleTapUp(MotionEvent e) { // TODO Auto-generated method stub return false ; } } |
希望本文所述对大家Android程序设计有所帮助。