android常見手動和自動輪播圖效果
本文實例為大家分享了android手動和自動輪播圖效果的具體代碼,供大家參考,具體內(nèi)容如下
1、準備好需要的輪播圖片,圖片標題(初始化,聲明)。
/**輪播圖片*/ private int[] imageIds=new int[]{ R.drawable.ic_launcher, R.drawable.simple_player_control_focused_holo, R.drawable.dot_player1_1, R.drawable.jt5, }; /**輪播圖片的標題*/ private String[] titles=new String[]{ "我是一", "我是二", "我是三", "我是四", };
2、在你要輪播的布局里面加入下面的布局(相當于加入一個控件,看你想放哪里).
<FrameLayout android:layout_width="match_parent" android:layout_height="200dip" > <android.support.v4.view.ViewPager android:id="@+id/viewPager" 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>
3、把圖片和標題都設(shè)置控件里面。
/**顯示的圖片*/ images = new ArrayList<ImageView>(); for(int i=0;i<imageIds.length;i++){ ImageView imageView = new ImageView(getActivity()); imageView.setBackgroundResource(imageIds[i]); images.add(imageView); } /*顯示的圓點 */ dots = new ArrayList<View>(); dots.add(view.findViewById(R.id.dot_0)); dots.add(view.findViewById(R.id.dot_1)); dots.add(view.findViewById(R.id.dot_2)); dots.add(view.findViewById(R.id.dot_3)); /**輪播的標題*/ title = (TextView) view.findViewById(R.id.title); title.setText(titles[0]);
4、findViewById到控件布局里面的ViewPager,new 一個ViewpagerAdapter(),通過setOnPageChangeListener的方法來監(jiān)聽改變
viewPager = (ViewPager) view.findViewById(R.id.viewPager); adapter = new ViewPagerAdapter(); viewPager.setAdapter(adapter); viewPager.setOnPageChangeListener(new ViewPager.OnPageChangeListener() { @Override public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) { title.setText(titles[position]); dots.get(position).setBackgroundResource(R.drawable.dot_focused); dots.get(oldPosition).setBackgroundResource(R.drawable.dot_normal); oldPosition = position; currentItem = position; } @Override public void onPageSelected(int position) { } @Override public void onPageScrollStateChanged(int state) { } });
5、自定義一個ViewPagerAdapter
/** * 自定義Adapter * 內(nèi)部類 */ private class ViewPagerAdapter extends PagerAdapter { @Override public int getCount() { return images.size();//傳入的數(shù)據(jù) } @Override public boolean isViewFromObject(View arg0, Object arg1) { return arg0 == arg1; } @Override public void destroyItem(ViewGroup view, int position, Object object) { // TODO Auto-generated method stub // super.destroyItem(container, position, object); // view.removeView(view.getChildAt(position)); // view.removeViewAt(position); view.removeView(images.get(position)); } @Override public Object instantiateItem(ViewGroup view, int position) { // TODO Auto-generated method stub view.addView(images.get(position)); return images.get(position); } }
6、這些自己看著改主要是線程池,handler,定時輪換
/** * 圖片輪播任務(wù) * */ private class ViewPageTask implements Runnable{ @Override public void run() { currentItem = (currentItem + 1) % imageIds.length; mHandler.sendEmptyMessage(0); } } /** * 接收子線程傳遞過來的數(shù)據(jù) */ private Handler mHandler = new Handler(){ public void handleMessage(android.os.Message msg) { viewPager.setCurrentItem(currentItem); }; }; @Override public void onStop() { // TODO Auto-generated method stub super.onStop(); if(scheduledExecutorService != null){ scheduledExecutorService.shutdown(); scheduledExecutorService = null; } }
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
Android使用ExpandableListView實現(xiàn)三層嵌套折疊菜單
這篇文章主要介紹了Android使用ExpandableListView實現(xiàn)三層嵌套折疊菜單,對布局感興趣的同學(xué)可以參考下2021-04-04android實現(xiàn)視頻的加密和解密(使用AES)
本篇文章主要介紹了android實現(xiàn)視頻的加密和解密(使用AES),小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2017-05-05Android 進度條按鈕ProgressButton的實現(xiàn)代碼
這篇文章主要介紹了Android 進度條按鈕實現(xiàn)(ProgressButton)代碼,本文通過實例代碼給大家介紹的非常詳細,具有一定的參考借鑒價值 ,需要的朋友可以參考下2018-10-10Android本地驗證碼的簡易實現(xiàn)方法(防止暴力登錄)
驗證馬真是無處不在啊,主要作用是防止惡意暴力破解登錄,這篇文章主要介紹了Android本地驗證碼的簡易實現(xiàn)方法(防止暴力登錄),需要的朋友可以參考下2017-04-04