Android 获取手机中某一文件夹下的图片,并以banner的形式上下轮播

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








评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值