AdapterView继承了AdapterViewAnimator,它也会显示Adapter提供的多个View组件,但每次只能显示一个View组件,程序可通过showPrevious和showNext()方法控制该组件显示上一个、下一个组件。
AdapterViewFlipper可以在多个View切换过程中使用渐隐渐现的动画效果,除此之外,还可以调用该组件的startFlipping()控制它“自动播放”下一个View组件。
一个显示图片的例子来说明AdapterViewFlipper的用法:
activiti_main.xml:
1 <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 2 xmlns:tools="http://schemas.android.com/tools" 3 android:layout_width="match_parent" 4 android:layout_height="match_parent" 5 tools:context="com.hct.adapterviewflipper.MainActivity" > 6 7 <AdapterViewFlipper 8 android:id="@+id/adapter_view_flipper_id" 9 android:layout_width="match_parent" 10 android:layout_height="match_parent" 11 android:layout_alignParentTop="true" 12 android:flipInterval="5000" /> 13 14 <Button 15 android:layout_width="wrap_content" 16 android:layout_height="wrap_content" 17 android:layout_alignParentBottom="true" 18 android:layout_alignParentLeft="true" 19 android:onClick="prev"<!-- 需要实现prev方法 --> 20 android:text="@string/pre_button_txt" /> 21 22 <Button 23 android:layout_width="wrap_content" 24 android:layout_height="wrap_content" 25 android:layout_alignParentBottom="true" 26 android:layout_alignParentRight="true" 27 android:onClick="next" <!-- 需要实现的方法 --> 28 android:text="@string/next_button_txt" /> 29 30 <Button 31 android:layout_width="wrap_content" 32 android:layout_height="wrap_content" 33 android:layout_alignParentBottom="true" 34 android:layout_centerHorizontal="true" 35 android:onClick="auto_play"<!-- 需要实现的方法 -->36 android:text="@string/auto_button_txt" />3738 </RelativeLayout>
MainActivity.java:
1 public class MainActivity extends Activity { 2 private int[] m_image_ids = new int[] { R.drawable.baiyang, 3 R.drawable.jinniu, R.drawable.shuangzi, R.drawable.juxie, 4 R.drawable.shizi, R.drawable.chunv, R.drawable.tiancheng, 5 R.drawable.tianxie, R.drawable.sheshou, R.drawable.mojie, 6 R.drawable.shuiping, R.drawable.shuangyu }; 7 private AdapterViewFlipper m_adapterFlipper; 8 9 @Override 10 protected void onCreate(Bundle savedInstanceState) { 11 super.onCreate(savedInstanceState); 12 setContentView(R.layout.activity_main); 13 m_adapterFlipper = (AdapterViewFlipper) findViewById(R.id.adapter_view_flipper_id); 14 BaseAdapter baseAdapter = new BaseAdapter() { 15 16 @Override 17 public View getView(int position, View convertView, ViewGroup parent) { 18 // TODO Auto-generated method stub 19 ImageView imageView = new ImageView(MainActivity.this); 20 imageView.setImageResource(m_image_ids[position]); 21 // 设置image的缩放类型 22 imageView.setScaleType(ImageView.ScaleType.FIT_XY); 23 imageView.setLayoutParams(new LayoutParams( 24 LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT)); 25 return imageView; 26 } 27 28 @Override 29 public long getItemId(int position) { 30 // TODO Auto-generated method stub 31 return position; 32 } 33 34 @Override 35 public Object getItem(int position) { 36 // TODO Auto-generated method stub 37 return position; 38 } 39 40 @Override 41 public int getCount() { 42 // TODO Auto-generated method stub 43 return m_image_ids.length; 44 } 45 }; 46 m_adapterFlipper.setAdapter(baseAdapter); 47 } 48 49 public void prev(View source){ 50 //显示上一个组件 51 m_adapterFlipper.showPrevious(); 52 m_adapterFlipper.stopFlipping(); 53 } 54 55 public void next(View source){ 56 //显示下一个组件 57 m_adapterFlipper.showNext(); 58 m_adapterFlipper.stopFlipping(); 59 } 60 61 public void auto_play(View source){ 62 //自动播放 63 m_adapterFlipper.startFlipping(); 64 } 65 66 @Override 67 public boolean onCreateOptionsMenu(Menu menu) { 68 // Inflate the menu; this adds items to the action bar if it is present. 69 getMenuInflater().inflate(R.menu.main, menu); 70 return true; 71 } 72 73 @Override 74 public boolean onOptionsItemSelected(MenuItem item) { 75 // Handle action bar item clicks here. The action bar will 76 // automatically handle clicks on the Home/Up button, so long 77 // as you specify a parent activity in AndroidManifest.xml. 78 int id = item.getItemId(); 79 if (id == R.id.action_settings) { 80 return true; 81 } 82 return super.onOptionsItemSelected(item); 83 } 84 }
AdapterViewFlipper的功能和用法
时间: 2024-10-10 22:55:27