ViewPager实现图片切换效果
作者:芒果蜜桃π
这篇文章主要为大家详细介绍了ViewPager实现图片切换效果,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
本文实例为大家分享了ViewPager实现图片切换效果的具体代码,供大家参考,具体内容如下
xml布局:
<RelativeLayout android:layout_width="match_parent" android:layout_height="260dp"> <android.support.v4.view.ViewPager android:id="@+id/view_pager" android:background="#000" android:layout_width="match_parent" android:layout_height="match_parent" /> <LinearLayout android:id="@+id/layout_point" android:background="#00000000" android:gravity="center" android:layout_width="match_parent" android:layout_height="20dp" android:orientation="horizontal" android:layout_marginBottom="40dp" android:layout_alignParentBottom="true"/> </RelativeLayout>
activity代码:
import android.content.Context; import android.os.Bundle; import android.support.annotation.Nullable; import android.support.v4.app.Fragment; import android.support.v4.view.PagerAdapter; import android.support.v4.view.ViewPager; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; import android.widget.ImageView; import android.widget.LinearLayout; import android.widget.ListView; import android.widget.TextView; import android.widget.Toast; import com.bumptech.glide.Glide; import com.example.uoso.myapplicationdemo.R; import java.text.SimpleDateFormat; import java.util.ArrayList; import java.util.Date; import java.util.List; /** * ViewPager显示图片 */ public class Fragment2 extends Fragment{ private ViewPager viewPager; private LinearLayout layoutPoint; private String[] imgs = {"1图片url" ,"2图片url", "3图片url"}; @Nullable @Override public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) { View view = inflater.inflate(R.layout.fragment2, null); viewPager = (ViewPager) view.findViewById(R.id.view_pager); layoutPoint = (LinearLayout) view.findViewById(R.id.layout_point); initView(); return view; } private void initView(){ //ViewPager相关 ViewPagerAdater myAdater = new ViewPagerAdater(getContext()); viewPager.setAdapter(myAdater); viewPager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() { @Override public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) { } @Override public void onPageSelected(int position) { //在滑动完成后向布局中添加小圆点 setLayoutPoint(position); } @Override public void onPageScrollStateChanged(int state) { } }); setLayoutPoint(0); } /** * 设置小圆点布局 * @param position */ private void setLayoutPoint(int position){ layoutPoint.removeAllViews(); for (int i = 0; i <3 ; i++) { ImageView imageView = new ImageView(getContext()); //设置ImageView LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(20, 20); params.setMargins(10,0,10,0); imageView.setLayoutParams(params); //设置小圆点样式 if (position==i){ imageView.setImageDrawable(getResources().getDrawable(R.drawable.point_white)); }else { imageView.setImageDrawable(getResources().getDrawable(R.drawable.point_gray)); } layoutPoint.addView(imageView); } } public class ViewPagerAdater extends PagerAdapter { private Context context; public ViewPagerAdater(Context context) { this.context = context; } @Override public int getCount() { return imgs.length; } @Override public Object instantiateItem(ViewGroup container, int position) { ImageView imageView = new ImageView(context); imageView.setScaleType(ImageView.ScaleType.FIT_XY); Glide.with(context).load(imgs[position]).into(imageView); container.addView(imageView); return imageView; } @Override public void destroyItem(ViewGroup container, int position, Object object) { container.removeView((ImageView) object); } @Override public boolean isViewFromObject(View view, Object object) { return view == object; } } }
小圆点样式:
<?xml version="1.0" encoding="utf-8"?> <shape xmlns:android="http://schemas.android.com/apk/res/android"> <solid android:color="#fddc04"/> <corners android:radius="5dp"/> </shape>
Glide依赖:
compile 'com.github.bumptech.glide:glide:3.7.0'
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持脚本之家。