Android 获取手机中某一文件夹下的图片,并以banner的形式上下轮播
来源:互联网 发布:网龙网络2017校园招聘 编辑:程序博客网 时间:2024/04/29 20:05
1.实现图片上下轮播:
package com.unilife.fridge.haiercommon.ui.view;import android.content.Context;import android.graphics.drawable.Drawable;import android.net.Uri;import android.util.AttributeSet;import android.view.Gravity;import android.view.animation.Animation;import android.view.animation.DecelerateInterpolator;import android.view.animation.TranslateAnimation;import android.widget.FrameLayout;import android.widget.ImageView;import com.unilife.fridge.haiercommon.R;import java.io.File;import java.util.List;/** * Created by wangchm on 2017/12/6. * 图片上下轮播 */public class LooperImageView extends FrameLayout { private List<String> tipList; private int curTipIndex = 0; private long lastTimeMillis ; private static final int ANIM_DELAYED_MILLIONS = 3 * 1000; /** 动画持续时长 */ private static final int ANIM_DURATION = 1* 1000; private Animation anim_out, anim_in; private ImageView iv_out,iv_in; public LooperImageView(Context context) { super(context); initTipFrame(); initAnimation(); } public LooperImageView(Context context, AttributeSet attrs) { super(context, attrs); initTipFrame(); initAnimation(); } public LooperImageView(Context context, AttributeSet attrs, int defStyleAttr) { super(context, attrs, defStyleAttr); initTipFrame(); initAnimation(); } private void initTipFrame() { iv_out = newImageView(); iv_in = newImageView();// iv_in = backImageView(); addView(iv_out); addView(iv_in); } private ImageView newImageView(){ ImageView imageView = new ImageView(getContext()); LayoutParams lp = new LayoutParams( LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT, Gravity.CENTER_VERTICAL); lp.setMargins(0,0,0,10); imageView.setLayoutParams(lp); imageView.setScaleType(ImageView.ScaleType.CENTER_CROP);// imageView.setBackgroundResource(R.drawable.bg_album);// imageView.setPadding(0,0,0,20); return imageView; } private ImageView backImageView(){ ImageView imageView = new ImageView(getContext()); LayoutParams lp = new LayoutParams( LayoutParams.MATCH_PARENT-20, LayoutParams.MATCH_PARENT, Gravity.CENTER_VERTICAL); lp.setMargins(0,10,0,10); imageView.setLayoutParams(lp); imageView.setScaleType(ImageView.ScaleType.CENTER_CROP); imageView.setImageAlpha(50); return imageView; } private ImageView inImageView(ImageView view){ LayoutParams layoutParams = (LayoutParams) view.getLayoutParams(); layoutParams.width = LayoutParams.MATCH_PARENT; layoutParams.height = LayoutParams.MATCH_PARENT; return view; } /** * 将资源图片转换为Drawable对象 * @param ResId * @return */ private Drawable loadDrawable(int ResId) { Drawable drawable = getResources().getDrawable(ResId); drawable.setBounds(0, 0, drawable.getMinimumWidth() - 10, drawable.getMinimumHeight() - 10); return drawable; } private void initAnimation() { anim_out = newAnimation(0, -1); anim_in = newAnimation(2, 0); anim_in.setAnimationListener(new Animation.AnimationListener() { @Override public void onAnimationStart(Animation animation) { } @Override public void onAnimationRepeat(Animation animation) { } @Override public void onAnimationEnd(Animation animation) { updateTipAndPlayAnimationWithCheck(); } }); } private Animation newAnimation(float fromYValue, float toYValue) { Animation anim = new TranslateAnimation(Animation.RELATIVE_TO_SELF,0, Animation.RELATIVE_TO_SELF,0, Animation.RELATIVE_TO_PARENT,fromYValue, Animation.RELATIVE_TO_SELF, toYValue); this.setBackgroundColor(getResources().getColor(R.color.trans)); anim.setDuration(ANIM_DURATION); anim.setStartOffset(ANIM_DELAYED_MILLIONS); anim.setInterpolator(new DecelerateInterpolator()); return anim; } private void updateTipAndPlayAnimationWithCheck() { if (System.currentTimeMillis() - lastTimeMillis < 1000 ) { return ; } lastTimeMillis = System.currentTimeMillis(); updateTipAndPlayAnimation(); } private void updateTipAndPlayAnimation() { if (curTipIndex % 2 == 0) { updateImageTip(iv_out); iv_in.startAnimation(anim_out); iv_out.startAnimation(anim_in); this.bringChildToFront(iv_in); this.setBackgroundResource(R.drawable.bg_album); } else { updateImageTip(iv_in); iv_out.startAnimation(anim_out); iv_in.startAnimation(anim_in); this.bringChildToFront(iv_out); this.setBackgroundResource(R.drawable.bg_album); } } private void updateImageTip(ImageView tipView) { String tip = getNextTip(); tipView.setImageURI(Uri.fromFile(new File(tip))); } /** * 获取下一条消息 * @return */ private String getNextTip() { if (isListEmpty(tipList)) return null; return tipList.get(curTipIndex++ % tipList.size()); } public static boolean isListEmpty(List list) { return list == null || list.isEmpty(); } public void setImgTipList(List<String> tipList) { this.tipList = tipList; curTipIndex = 0; updateImageTip(iv_out); updateTipAndPlayAnimation(); }}
2.主界面及其布局:
<com.unilife.fridge.haiercommon.ui.view.LooperImageView android:id="@+id/albumLooper" android:layout_width="match_parent" android:layout_height="match_parent"></com.unilife.fridge.haiercommon.ui.view.LooperImageView>
主页面调用:
albumLooper = (LooperImageView) view.findViewById(R.id.albumLooper);
albumLooper.setImgTipList(getLocalAlbumPhoto());
获取本地文件夹里的图片:
/** 获取本地图片* */private List<String> localPic = new ArrayList<String>();public List<String> getLocalAlbumPhoto() { if(localPic != null){ localPic.clear(); } String name = ""; String path = Environment.getExternalStorageDirectory().getAbsolutePath() + "/messageBoard/photoImgs"; File dir = new File(path); if (dir.exists() && dir.isDirectory()) { File[] files = dir.listFiles(); if (files != null && files.length > 0) { for(int i=0;i<files.length;i++){ name = files[i].getAbsolutePath(); localPic.add(name); } } } return localPic;}
阅读全文
0 0
- Android 获取手机中某一文件夹下的图片,并以banner的形式上下轮播
- Android 图片轮播Banner的实现
- Android中以数据流的形式获取assets文件夹中文件的内容
- android 获取手机中的所有图片或某一目录下的图片
- banner(图片轮播)框架的使用
- Android Banner图片轮播
- banner的轮播
- Android轮播banner的使用
- Android 无线轮播 Banner 的使用
- android无限轮播banner图片并且解决banner图片只有2张出现白板的情况,触摸即不轮播
- 从网络端leanCloud获取上传的图片和文字并以listview的形式显示在android端
- Android实现Banner图片循环轮播
- Android广告图片轮播控件banner
- banner图片轮播
- Banner图片轮播
- Banner 图片轮播
- banner图片轮播
- 图片轮播 banner
- https 流程
- MOB第三方分享
- 基于springboot+bootstrap+mysql+redis搭建一套完整的权限架构【四】【编写基础开发工具】
- 车辆管理系统
- @ModelAttribute总结
- Android 获取手机中某一文件夹下的图片,并以banner的形式上下轮播
- RSA算法原理(一)
- Android activity跳转之finish()方法的作用
- 申请开发者账号所需材料--基本流程大纲
- Mysql学习笔记(一)
- 联想小新锐7000安装Win7系统
- java中的大事件
- Runtime类中的freeMemory,totalMemory,maxMemory
- G