ImageSwitcher轮播图片(简单好用)_ide

        android:id="@+id/imageSwitcher"
android:background="@color/white"
android:layout_marginTop="17dp"
android:layout_width="match_parent"
android:layout_marginLeft="13dp"
android:layout_marginRight="13dp"
android:layout_height="186dp"
>

Fragment中

package com.ruhai.yibei.fragment;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.drawable.BitmapDrawable;
import android.graphics.drawable.Drawable;
import android.net.Uri;
import android.os.Build;
import android.os.Bundle;

import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;

import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.view.WindowManager;
import android.widget.ImageSwitcher;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ViewSwitcher;

import com.ruhai.yibei.R;


public class HomeFragment extends Fragment {
private LinearLayout mTitleBar;
private ImageSwitcher mImagSwitcher;
private int[] imgIds;


@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
imgIds = new int[]{R.mipmap.banner,R.mipmap.banner2};
// Inflate the layout for this fragment
return inflater.inflate(R.layout.fragment_home, container, false);
}

@Override
public void onActivityCreated(@Nullable Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
mTitleBar = (LinearLayout) getActivity().findViewById(R.id.title_bar);
setImmerseLayout(mTitleBar);

mImagSwitcher = (ImageSwitcher) getActivity().findViewById(R.id.imageSwitcher);

if(mImagSwitcher!=null){
mImagSwitcher.setFactory(new ViewSwitcher.ViewFactory() {
@Override
public View makeView() {
// makeView返回的是当前需要显示的ImageView控件,用于填充进ImageSwitcher中
return new ImageView(getActivity());
}
});
}
if(mImagSwitcher!=null){
mImagSwitcher.postDelayed(new Runnable() {
int currentIndex= 0;
@Override
public void run() {
mImagSwitcher.setBackgroundResource(imgIds[currentIndex]);
Log.e("wy", "mListString:"+imgIds[currentIndex]);
// Bitmap bit = BitmapFactory.decodeFile(imgIds[currentIndex]+"");
// Drawable drawable = new BitmapDrawable(bit);
// mImagSwitcher.setBackgroundDrawable(drawable);
if(currentIndex ==(imgIds.length - 1))
currentIndex = 0;
else
currentIndex++;
mImagSwitcher.postDelayed(this,3000);
}
},200);
}

}

//获取沉浸式布局
protected void setImmerseLayout(View view) {// view为标题栏
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
Window window = getActivity().getWindow();
window.addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
int statusBarHeight = getStatusBarHeight(getActivity().getBaseContext());
view.setPadding(0, statusBarHeight, 0, 0);
}
}

public int getStatusBarHeight(Context context) {
int result = 0;
int resourceId = context.getResources().getIdentifier("status_bar_height", "dimen",
"android");
if (resourceId > 0) {
result = context.getResources().getDimensionPixelSize(resourceId);
}
return result;
}
}