关闭

使用ViewFlipper实现图片的轮播

161人阅读 评论(0) 收藏 举报

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

xml文件

<RelativeLayout 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="com.xingyue.xingyue.PassiveLearningActivity" 
    android:background="@color/white">

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


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<Photos> 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<Photos> getListOfPictures(File fileName){
  
         File[] files = fileName.listFiles();// 列出所有文件  
         List<Photos> listPhoto = new ArrayList<Photos>();

 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;
}

}

0
0

查看评论
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
    个人资料
    • 访问:35575次
    • 积分:1086
    • 等级:
    • 排名:千里之外
    • 原创:65篇
    • 转载:62篇
    • 译文:0篇
    • 评论:2条
    最新评论