ImageMovieAnimator - Image sliding Animator

This class is used to display a slide-moving image.

Usage

    ImageMovieAnimator imageAnimator = new ImageMovieAnimator(activity,(ImageView)findViewById(R.id.image),
                new int[]{R.drawable.image1,R.drawable.image2,...});
    imageAnimator.start();
import android.animation.Animator;
import android.animation.ValueAnimator;
import android.app.Activity;
import android.content.Context;
import android.content.res.Resources;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Point;
import android.graphics.Rect;
import android.util.DisplayMetrics;
import android.util.Log;
import android.view.ViewGroup;
import android.view.WindowManager;
import android.view.animation.LinearInterpolator;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.RelativeLayout;

import me.lvxingshe.android.MyApplication;
import me.lvxingshe.android.R;

/**
 * Created by Luncrzs on 2015/10/18.
 */
public class ImageMovieAnimator {
    private Context mContext;
    private ImageView iv;
    private int[] images;
    private int thisImageId = 0;
    private int screenWidth = 0;
    private int screenHeight = 0;
    Bitmap bitmap;
    ValueAnimator animator;

    public ImageMovieAnimator(Context context, ImageView imageView, int[] imageResources){
        mContext = context;
        iv = imageView;
        images = imageResources;
        WindowManager wm = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE);
        DisplayMetrics displayMetrics = new DisplayMetrics();
        wm.getDefaultDisplay().getMetrics(displayMetrics);
        screenHeight = displayMetrics.heightPixels + getNavigationBarHeight();
        Log.d("ImageAnimator","height="+displayMetrics.heightPixels+" screenHeight="+screenHeight);
        screenWidth = displayMetrics.widthPixels;
    }

    public void start(){
        bitmap = BitmapFactory.decodeResource(mContext.getResources(),images[thisImageId]);
        iv.setImageBitmap(bitmap);
        double screenRatio = (double)screenWidth/screenHeight;
        double bitmapRatio = (double)bitmap.getWidth()/bitmap.getHeight();
        final int imageHeight = screenRatio<bitmapRatio?screenHeight:(int)(((double)screenWidth/bitmap.getWidth())*bitmap.getHeight());
        final int imageWidth = screenRatio<bitmapRatio?(int)(((double)screenHeight/bitmap.getHeight())*bitmap.getWidth()):screenWidth;
        final int paddingValue = (imageWidth-screenWidth)/2;
        RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(imageWidth,imageHeight);
        iv.setLayoutParams(params);
        iv.setPadding(paddingValue, 0, 0, 0);
        animator = ValueAnimator.ofFloat(1, -1);
        animator.setDuration(screenRatio < bitmapRatio ? paddingValue * 50 : 5000);
        animator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
            @Override
            public void onAnimationUpdate(ValueAnimator animation) {
                float value = (float) animation.getAnimatedValue();

                iv.setPadding((int) (value * paddingValue), 0, 0, 0);
                if (value > 0.9) {
                    iv.setAlpha((float) (0.3 + 7 * (1 - value)));
                } else if (value < -0.9) {
                    iv.setAlpha((float) (0.3 + 7 * (value + 1)));
                }
                iv.invalidate();
            }
        });
        animator.addListener(new Animator.AnimatorListener() {
            @Override
            public void onAnimationStart(Animator animation) {

            }

            @Override
            public void onAnimationEnd(Animator animation) {
                thisImageId = (thisImageId + 1) < images.length ? thisImageId + 1 : 0;
                start();
            }

            @Override
            public void onAnimationCancel(Animator animation) {

            }

            @Override
            public void onAnimationRepeat(Animator animation) {

            }
        });
        animator.setInterpolator(new LinearInterpolator());
        animator.start();
    }

    public int getNavigationBarHeight(){
        Resources resources = mContext.getResources();
        int resourceId = resources.getIdentifier("navigation_bar_height", "dimen", "android");
        if (resourceId > 0) {
            return resources.getDimensionPixelSize(resourceId);
        }
        return 0;
    }

    public void setHeight(int height){
        screenHeight = height;
    }

    public void stop(){
        if(animator!=null && animator.isRunning()){
            animator.end();
        }
    }
}

标签: android, view

添加新评论