Android實(shí)現(xiàn)圖片自動(dòng)切換功能(實(shí)例代碼詳解)
在Android中圖片的自動(dòng)切換不僅可以實(shí)現(xiàn)自動(dòng)切換,而且還可以使用手動(dòng)切換。而且一般在切換的時(shí)候,在圖片下方還帶有其他內(nèi)容的切換,用來(lái)標(biāo)記是第幾個(gè)圖片的切換。
這種效果在我們?nèi)粘I钪泻艹R?jiàn),例如某寶購(gòu)物,一些商城都可以使用到,用戶體驗(yàn)度極好,今天小編就通過(guò)實(shí)例代碼給大家分享android 圖片自動(dòng)切換功能的實(shí)現(xiàn)。
實(shí)現(xiàn)效果如下:
具體的示例代碼如下:
布局代碼:
<?xml version='1.0' encoding='utf-8'?><ScrollView android:layout_height='match_parent' android:layout_width='match_parent' android:orientation='vertical' xmlns:android='http://schemas.android.com/apk/res/android'> <RelativeLayout android:layout_width='match_parent' android:layout_height='match_parent'> <FrameLayout android:layout_width='match_parent' android:layout_height='250dp' android:padding='20dp' android:orientation='vertical'> <androidx.viewpager.widget.ViewPager android: android:layout_width='match_parent' android:layout_height='match_parent'/> <LinearLayout android:layout_width='match_parent' android:layout_height='25dp' android:orientation='horizontal' android:layout_gravity='bottom' android:gravity='center' android:layout_marginTop='5dp' android:background='#E6E6E6'> <View android: android:layout_width='5dp' android:layout_height='5dp' android:layout_marginLeft='2dp' android:layout_marginRight='2dp' android:background='@drawable/dot_focus'/> <View android: android:layout_width='5dp' android:layout_height='5dp' android:layout_marginLeft='2dp' android:layout_marginRight='2dp' android:background='@drawable/dot_normal'/> <View android: android:layout_width='5dp' android:layout_height='5dp' android:layout_marginLeft='2dp' android:layout_marginRight='2dp' android:background='@drawable/dot_normal'/> </LinearLayout> </FrameLayout> </RelativeLayout></ScrollView>
dot_focus.xml文件代碼如下:
<?xml version='1.0' encoding='utf-8'?><shape android:shape='oval' xmlns:android='http://schemas.android.com/apk/res/android'> <corners android:radius='5dp'/> <solid android:color='@color/white'/></shape>
dot_normal.xml文件代碼如下:
<?xml version='1.0' encoding='utf-8'?><shape android:shape='oval' xmlns:android='http://schemas.android.com/apk/res/android'> <corners android:radius='5dp'/> <solid android:color='@color/blue'/></shape>
Activity代碼:
public class MainActivity extends AppCompatActivity { private ArrayList<ImageView> imageViews= new ArrayList<>(); private ArrayList<View>dots=new ArrayList<>(); private ViewPager vp; private int image[]={R.drawable.denglu,R.drawable.disanye,R.drawable.hhh}; private int oldPosition=0; private int currentItem; private ScheduledExecutorService scheduledExecutorService; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); //獲取圖片資源 for (int i=0;i<image.length;i++){ ImageView imageView=new ImageView(this); imageView.setImageResource(image[i]); imageViews.add(imageView); } //顯示的點(diǎn)的集合 dots.add(findViewById(R.id.p1)); dots.add(findViewById(R.id.p2)); dots.add(findViewById(R.id.p3)); vp=findViewById(R.id.viewContent); vp.setAdapter(new ViewPagerAdapter()); vp.setOnPageChangeListener(new ViewPager.OnPageChangeListener() { @Override public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) { } @Override public void onPageSelected(int position) { //點(diǎn)的效果切換 dots.get(oldPosition).setBackgroundResource(R.drawable.dot_normal); dots.get(position).setBackgroundResource(R.drawable.dot_fouced); oldPosition=position; currentItem=position; } @Override public void onPageScrollStateChanged(int state) { } }); } class ViewPagerAdapter extends PagerAdapter { @Override public int getCount() { return imageViews.size(); } @Override public boolean isViewFromObject(@NonNull View view, @NonNull Object object) { return view==object; } @Override public void destroyItem(@NonNull ViewGroup container, int position, @NonNull Object object) { View v=imageViews.get(position); container.removeView(v); } @NonNull @Override public Object instantiateItem(@NonNull ViewGroup container, int position) { View v=imageViews.get(position); container.addView(v); return v; } } @Override protected void onStart() { super.onStart(); scheduledExecutorService= Executors.newSingleThreadScheduledExecutor(); scheduledExecutorService.scheduleWithFixedDelay(new ViewPagerTask(),2,2, TimeUnit.SECONDS); } class ViewPagerTask implements Runnable{ @Override public void run() { currentItem=(currentItem+1)%image.length; handler.obtainMessage().sendToTarget(); } } Handler handler=new Handler(){ @Override public void handleMessage(@NonNull Message msg) { vp.setCurrentItem(currentItem); } };}
總結(jié)
到此這篇關(guān)于Android實(shí)現(xiàn)圖片自動(dòng)切換功能的文章就介紹到這了,更多相關(guān)android 圖片自動(dòng)切換內(nèi)容請(qǐng)搜索好吧啦網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持好吧啦網(wǎng)!
相關(guān)文章:
1. XML入門(mén)精解之結(jié)構(gòu)與語(yǔ)法2. ASP刪除img標(biāo)簽的style屬性只保留src的正則函數(shù)3. ASP動(dòng)態(tài)網(wǎng)頁(yè)制作技術(shù)經(jīng)驗(yàn)分享4. Xml簡(jiǎn)介_(kāi)動(dòng)力節(jié)點(diǎn)Java學(xué)院整理5. ASP基礎(chǔ)入門(mén)第二篇(ASP基礎(chǔ)知識(shí))6. CSS可以做的幾個(gè)令你嘆為觀止的實(shí)例分享7. ASP實(shí)現(xiàn)加法驗(yàn)證碼8. PHP session反序列化漏洞超詳細(xì)講解9. 解析原生JS getComputedStyle10. css代碼優(yōu)化的12個(gè)技巧
