Android实现图片自动切换功能(实例代码详解)
在Android中图片的自动切换不仅可以实现自动切换,而且还可以使用手动切换。而且一般在切换的时候,在图片下方还带有其他内容的切换,用来标记是第几个图片的切换。
这种效果在我们日常生活中很常见,例如某宝购物,一些商城都可以使用到,用户体验度极好,今天小编就通过实例代码给大家分享android 图片自动切换功能的实现。
实现效果如下:
具体的示例代码如下:
布局代码:
<?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:id="@+id/viewContent" 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:id="@+id/p1" android:layout_width="5dp" android:layout_height="5dp" android:layout_marginLeft="2dp" android:layout_marginRight="2dp" android:background="@drawable/dot_focus"/> <View android:id="@+id/p2" android:layout_width="5dp" android:layout_height="5dp" android:layout_marginLeft="2dp" android:layout_marginRight="2dp" android:background="@drawable/dot_normal"/> <View android:id="@+id/p3" 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); } //显示的点的集合 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) { //点的效果切换 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); } }; }
总结
到此这篇关于Android实现图片自动切换功能的文章就介绍到这了,更多相关android 图片自动切换内容请搜索亿速云以前的文章或继续浏览下面的相关文章希望大家以后多多支持亿速云!