我正在尝试使用viewPager在android中的Bottom Navigation Bar中创建图片滑块。 我想将GIF图像加载到viewPager但它无法正常工作。如果有可能请告诉我它是如何可能的 这是我的xml代码。
<FrameLayout
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"
tools:context=".HomeFragment">
<!-- TODO: Update blank fragment layout -->
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center"
android:orientation="vertical"
android:paddingBottom="60dp"
android:paddingTop="60dp">
<android.support.v4.view.ViewPager
android:id="@+id/mainviewPager1"
android:layout_width="match_parent"
android:layout_height="200dp"
android:layout_alignParentLeft="true" />
</LinearLayout>
</FrameLayout>
这是我的HomeFragment类,我将viewPager添加到片段中。
public class HomeFragment extends Fragment
{
ViewPager viewPager;
public HomeFragment()
{
// Required empty public constructor
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)
{
View v = inflater.inflate(R.layout.fragment_home, container,false);
viewPager = (ViewPager) v.findViewById(R.id.mainviewPager1);
// Inflate the layout for this fragment
ViewPagerAdapter viewPagerAdapter = new ViewPagerAdapter(getContext());
viewPager.setAdapter(viewPagerAdapter);
}
return v;
}
这是我的viewPagerAdaper.java代码。
public class ViewPagerAdapter extends PagerAdapter
{
Context context;
LayoutInflater layoutInflater;
Integer[] images={R.drawable.fimage,R.drawable.simage,R.drawable.timage};
public ViewPagerAdapter(Context context) {
this.context = context;
}
@Override
public int getCount()
{
return images.length;
}
@Override
public boolean isViewFromObject(View view, Object object)
{
return view==object;
}
@Override
public Object instantiateItem(ViewGroup container, int position) {
layoutInflater=(LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View view=layoutInflater.inflate(R.layout.custom_layout,null);
ImageView imageView=(ImageView)view.findViewById(R.id.customimageView2);
imageView.setImageResource(images[position]);
ViewPager vp=(ViewPager) container;
vp.addView(view,0);
return view;
}
@Override
public void destroyItem(ViewGroup container, int position, Object object) {
ViewPager vp=(ViewPager) container;
View v=(View) object;
vp.removeView(v);
}
}
答案 0 :(得分:1)
ImageView
不支持GIF格式,如果您想处理GIF图片,可以使用此库https://github.com/koral--/android-gif-drawable并在pl.droidsonroids.gif.GifImageView
内添加custom_layout.xml
。
将ImageView
替换为GifImageView