<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" > <AdapterViewFlipper android:id="@+id/flipper" android:layout_width="match_parent" android:layout_height="match_parent" android:flipInterval="5000" android:layout_alignParentTop="true" /> <Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignParentBottom="true" android:layout_alignParentLeft="true" android:onClick="prev" android:text="上一个" /> <Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignParentBottom="true" android:layout_centerHorizontal="true" android:onClick="next" android:text="下一个" /> <Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignParentBottom="true" android:layout_alignParentRight="true" android:onClick="auto" android:text="自动播放" /> </RelativeLayout>
package com.example.adapterviewfilipper; import android.app.Activity; import android.os.Bundle; import android.view.View; import android.view.ViewGroup; import android.view.ViewGroup.LayoutParams; import android.widget.AdapterViewFlipper; import android.widget.BaseAdapter; import android.widget.ImageView; public class MainActivity extends Activity { int[] imageIds=new int[]{ R.drawable.shuangzi,R.drawable.shuangyu,R.drawable.chunv, R.drawable.tiancheng,R.drawable.tianxie,R.drawable.sheshou,R.drawable.juxie, R.drawable.shuiping,R.drawable.shizi,R.drawable.baiyang,R.drawable.jinniu,R.drawable.mojie }; AdapterViewFlipper flipper; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); flipper=(AdapterViewFlipper) findViewById(R.id.flipper); BaseAdapter adapter=new BaseAdapter(){ @Override public int getCount() { return imageIds.length; } @Override public Object getItem(int position) { return position; } @Override public long getItemId(int position) { return position; } @Override public View getView(int position, View convertView, ViewGroup parent) { ImageView imageView=new ImageView(MainActivity.this); imageView.setImageResource(imageIds[position]); imageView.setScaleType(ImageView.ScaleType.FIT_XY); imageView.setLayoutParams(new LayoutParams(LayoutParams.MATCH_PARENT,LayoutParams.MATCH_PARENT)); return imageView; } }; flipper.setAdapter(adapter); } public void prev(View source){ flipper.showPrevious(); flipper.stopFlipping(); } public void next(View source){ flipper.showNext(); flipper.stopFlipping(); } public void auto(View source){ flipper.startFlipping(); } }
时间: 2024-10-06 17:32:50