使用ViewFlipper实现图片的轮播

参考文章:http://www.2cto.com/kf/201502/376967.html

xml文件

    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context="com.xingyue.xingyue.PassiveLearningActivity" 
    android:background="@color/white">

        android:id="@+id/viewFlipperPhoto"
     android:layout_width="wrap_content"
     android:layout_height="wrap_content"/>


activity文件
import com.setting.deviceslock.Photos;
import com.wdh.utils.CommonalityMethod;
import com.wdh.utils.FilePath;
import com.wdh.utils.LogUtils;
import android.app.Activity;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.drawable.BitmapDrawable;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.ImageView;
import android.widget.ViewFlipper;
public class PassiveLearningActivity extends Activity {

private final String Tag = "PassiveLearningActivity";
private ViewFlipper viewFlipper;
List listPhoto;

@SuppressWarnings("deprecation")
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_passive_learning);

viewFlipper = (ViewFlipper)findViewById(R.id.viewFlipperPhoto);

//获取制定文件夹下的图片

listPhoto = CommonalityMethod.getListOfPictures(CommonalityMethod.makeRootDirectory(FilePath.photoPath));

for (int i = 0; i < listPhoto.size(); i++) {
LogUtils.i(Tag, "listPhoto.size():   "+i +"path:  "+FilePath.photoPath+"/"+listPhoto.get(i).getPhotoName());


viewFlipper.addView(getView(FilePath.photoPath+"/"+listPhoto.get(i).getPhotoName()));
}

viewFlipper.setFlipInterval(3000);//播放图片间隔时间
viewFlipper.startFlipping();
}


@Override
protected void onDestroy() {
// TODO Auto-generated method stub
super.onDestroy();
}

@SuppressWarnings("deprecation")
private View getView(String photoPath){
Bitmap bmp = BitmapFactory.decodeFile(photoPath);
View view = new View(this);
view.setBackground(new BitmapDrawable(bmp));
return view;

}

}


//Photos 类,存放photo名字

public class Photos {


private String photoName;
public String getPhotoName() {
return photoName;
}

public void setPhotoName(String photoName) {
this.photoName = photoName;
}

}

//图片所在的文件路径

public class FilePath {

/*本地文件路径*/
public final static String localFolderPath = "/storage/sdcard0/jingjing";

/*被动学习图片路径*/
public final static String photoPath = "/storage/sdcard0/jingjing/photos";
}

public class CommonalityMethod {


private static String Tag = "CommonalityMethod";

//文件夹不存在则 生成文件夹
public static File makeRootDirectory(String filePath) {
    File file = null;
    try {
        file = new File(filePath);
        if (!file.exists()) {
            file.mkdir();
        }
    } catch (Exception e) {
     LogUtils.i(Tag, e+"");
  }
    
    return file;
}


//获取指定文件夹下的图片
public static List getListOfPictures(File fileName){
  
         File[] files = fileName.listFiles();// 列出所有文件  
         List listPhoto = new ArrayList();

 if (files != null) {
for (int i = 0; i < files.length; i++) {
 File file = files[i];  

//筛选png类型的图片
        if (file.getName().substring(file.getName().lastIndexOf(".")+1).equals("png")) {
        Photos photos = new Photos();
        photos.setPhotoName(file.getName());
        listPhoto.add(photos);
        }
    }
}
 
return listPhoto;
}

}

你可能感兴趣的:(使用ViewFlipper实现图片的轮播)