Android图片轮播

来源:互联网 发布:阿里云 网站负责人 编辑:程序博客网 时间:2024/06/05 15:13

使用viewpaper实现Android的图片轮播

1.布局文件

<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" >    <FrameLayout        android:layout_width="match_parent"        android:layout_height="150dp" >        <android.support.v4.view.ViewPager            android:id="@+id/vp"            android:layout_width="match_parent"            android:layout_height="match_parent" />        <LinearLayout            android:layout_width="match_parent"            android:layout_height="35dip"            android:layout_gravity="bottom"            android:background="#33000000"            android:gravity="center"            android:orientation="vertical" >            <TextView                android:id="@+id/title"                android:layout_width="wrap_content"                android:layout_height="wrap_content"                android:text="图片标题"                android:textColor="@android:color/white" />            <LinearLayout                android:layout_width="wrap_content"                android:layout_height="wrap_content"                android:layout_marginTop="3dip"                android:orientation="horizontal" >                <View                    android:id="@+id/dot_0"                    android:layout_width="5dip"                    android:layout_height="5dip"                    android:layout_marginLeft="2dip"                    android:layout_marginRight="2dip"                     android:background="@drawable/dot_focused"/>                <View                    android:id="@+id/dot_1"                    android:layout_width="5dip"                    android:layout_height="5dip"                    android:layout_marginLeft="2dip"                    android:layout_marginRight="2dip"                     android:background="@drawable/dot_normal"/>                <View                    android:id="@+id/dot_2"                    android:layout_width="5dip"                    android:layout_height="5dip"                    android:layout_marginLeft="2dip"                    android:layout_marginRight="2dip"                     android:background="@drawable/dot_normal"/>                <View                    android:id="@+id/dot_3"                    android:layout_width="5dip"                    android:layout_height="5dip"                    android:layout_marginLeft="2dip"                    android:layout_marginRight="2dip"                     android:background="@drawable/dot_normal"/>                                            </LinearLayout>        </LinearLayout>    </FrameLayout></RelativeLayout>

2.MainActivity类

public class MainActivity extends Activity {private ViewPager mViewPaper;private List<ImageView> images;private List<View> dots;private int currentItem;private Context con=this;private int stopthread=0;//记录上一次点的位置private int oldPosition = 0;//存放图片的idprivate int[] imageIds = new int[]{R.drawable.a,R.drawable.b,R.drawable.c,R.drawable.d};//存放图片的标题private String[]  titles = new String[]{        "巩俐不低俗,我就不能低俗",        "扑树又回来啦!再唱经典老歌引万人大合唱",        "揭秘北京电影如何升级",        "乐视网TV版大派送"                };private TextView title;private ViewPagerAdapter adapter;private ScheduledExecutorService scheduledExecutorService;@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);mViewPaper = (ViewPager) findViewById(R.id.vp);//显示的图片images = new ArrayList<ImageView>();for(int i = 0; i < imageIds.length; i++){ImageView imageView = new ImageView(this);imageView.setBackgroundResource(imageIds[i]);images.add(imageView);}//显示的小点dots = new ArrayList<View>();dots.add(findViewById(R.id.dot_0));dots.add(findViewById(R.id.dot_1));dots.add(findViewById(R.id.dot_2));dots.add(findViewById(R.id.dot_3));//dots.add(findViewById(R.id.dot_4));title = (TextView) findViewById(R.id.title);title.setText(titles[0]);adapter = new ViewPagerAdapter();mViewPaper.setAdapter(adapter);mViewPaper.setOnPageChangeListener(new ViewPager.OnPageChangeListener() {@Overridepublic void onPageSelected(int position) {title.setText(titles[position]);dots.get(position).setBackgroundResource(R.drawable.dot_focused);dots.get(oldPosition).setBackgroundResource(R.drawable.dot_normal);oldPosition = position;currentItem = position;}@Overridepublic void onPageScrolled(int arg0, float arg1, int arg2) {}@Overridepublic void onPageScrollStateChanged(int arg0) {}});mViewPaper.setOnTouchListener(new OnTouchListener(){@Overridepublic boolean onTouch(View v, MotionEvent event) {// TODO Auto-generated method stubstopthread=1;switch (event.getAction()) {              case MotionEvent.ACTION_DOWN:              stopthread=1;            //Toast.makeText(con, "this is down", 300).show();                break;              case MotionEvent.ACTION_UP:              stopthread=0;            //Toast.makeText(con, "this is ACTION_UP", 300).show();                break;              case MotionEvent.ACTION_MOVE:              stopthread=1;            //Toast.makeText(con, "this is ACTION_MOVE", 100).show();                break;                                                           } return false;}});}/** * 自定义Adapter * @author liuyazhuang * */private class ViewPagerAdapter extends PagerAdapter{@Overridepublic int getCount() {return images.size();}@Overridepublic boolean isViewFromObject(View arg0, Object arg1) {return arg0 == arg1;}@Overridepublic void destroyItem(ViewGroup view, int position, Object object) {// TODO Auto-generated method stubview.removeView(images.get(position));}@Overridepublic Object instantiateItem(ViewGroup view, int position) {// TODO Auto-generated method stub ImageView iv = images.get(position);   final int num=position;         iv.setOnClickListener(new OnClickListener(){@Overridepublic void onClick(View v) {// TODO Auto-generated method stubToast.makeText(con, "this is "+num, 500).show();}});                            view.addView(images.get(position));return iv;}}@Overridepublic boolean onCreateOptionsMenu(Menu menu) {// Inflate the menu; this adds items to the action bar if it is present.getMenuInflater().inflate(R.menu.main, menu);return true;}/** * 利用线程池定时执行动画轮播 */@Overrideprotected void onStart() {// TODO Auto-generated method stubsuper.onStart();scheduledExecutorService = Executors.newSingleThreadScheduledExecutor();scheduledExecutorService.scheduleWithFixedDelay(new ViewPageTask(), 2, 2, TimeUnit.SECONDS);}private class ViewPageTask implements Runnable{@Overridepublic void run() {if(stopthread==0){currentItem = (currentItem + 1) % imageIds.length;mHandler.sendEmptyMessage(0);}}}/** * 接收子线程传递过来的数据 */private Handler mHandler = new Handler(){public void handleMessage(android.os.Message msg) {mViewPaper.setCurrentItem(currentItem);};};@Overrideprotected void onStop() {// TODO Auto-generated method stubsuper.onStop();}}

3.运行结果


4.源码下载

   不要任何积分偶大笑

  点击进入下载页面

1 0
原创粉丝点击