首页 > 其他分享 >ImageView翻转效果

ImageView翻转效果

时间:2023-04-06 21:39:08浏览次数:48  
标签:效果 void private animation ImageView import android public 翻转


点击图中的星星开始翻转



源码:

import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Bitmap;
import android.graphics.Camera;
import android.graphics.Matrix;
import android.graphics.drawable.BitmapDrawable;
import android.graphics.drawable.Drawable;
import android.util.AttributeSet;
import android.view.View;
import android.view.animation.AccelerateDecelerateInterpolator;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.view.animation.DecelerateInterpolator;
import android.view.animation.Interpolator;
import android.view.animation.Transformation;
import android.widget.ImageView;

import fr.castorflex.android.flipimageview.R;

/**
 * Created with IntelliJ IDEA. User: castorflex Date: 30/12/12 Time: 16:25
 */
public class FlipImageView extends ImageView implements View.OnClickListener,
        Animation.AnimationListener {

    private static final int FLAG_ROTATION_X = 1 << 0;

    private static final int FLAG_ROTATION_Y = 1 << 1;

    private static final int FLAG_ROTATION_Z = 1 << 2;

    private static final Interpolator fDefaultInterpolator = new DecelerateInterpolator();

    private static int sDefaultDuration;

    private static int sDefaultRotations;

    private static boolean sDefaultAnimated;

    private static boolean sDefaultFlipped;


    public interface OnFlipListener {

        public void onClick(boolean flipped);

        public void onFlipStart();

        public void onFlipEnd();
    }

    private OnFlipListener mListener;

    private boolean mIsFlipped;

    private boolean mIsDefaultAnimated;

    private Drawable mDrawable;

    private Drawable mFlippedDrawable;

    private FlipAnimator mAnimation;

    private boolean mIsRotationXEnabled;

    private boolean mIsRotationYEnabled;

    private boolean mIsRotationZEnabled;

    public FlipImageView(Context context) {
        super(context);
        init(context, null, 0);
    }

    public FlipImageView(Context context, AttributeSet attrs) {
        super(context, attrs);
        init(context, attrs, 0);
    }

    public FlipImageView(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
        init(context, attrs, defStyle);
    }

    private void init(Context context, AttributeSet attrs, int defStyle) {
        sDefaultDuration = context.getResources().getInteger(R.integer.default_fiv_duration);
        sDefaultRotations = context.getResources().getInteger(R.integer.default_fiv_rotations);
        sDefaultAnimated = context.getResources().getBoolean(R.bool.default_fiv_isAnimated);
        sDefaultFlipped = context.getResources().getBoolean(R.bool.default_fiv_isFlipped);

        TypedArray a = context
                .obtainStyledAttributes(attrs, R.styleable.FlipImageView, defStyle, 0);
        mIsDefaultAnimated = a.getBoolean(R.styleable.FlipImageView_isAnimated, sDefaultAnimated);
        mIsFlipped = a.getBoolean(R.styleable.FlipImageView_isFlipped, sDefaultFlipped);
        mFlippedDrawable = a.getDrawable(R.styleable.FlipImageView_flipDrawable);
        int duration = a.getInt(R.styleable.FlipImageView_flipDuration, sDefaultDuration);
        int interpolatorResId = a.getResourceId(R.styleable.FlipImageView_flipInterpolator, 0);
        Interpolator interpolator = interpolatorResId > 0 ? AnimationUtils
                .loadInterpolator(context, interpolatorResId) : fDefaultInterpolator;
        int rotations = a.getInteger(R.styleable.FlipImageView_flipRotations, sDefaultRotations);
        mIsRotationXEnabled = (rotations & FLAG_ROTATION_X) != 0;
        mIsRotationYEnabled = (rotations & FLAG_ROTATION_Y) != 0;
        mIsRotationZEnabled = (rotations & FLAG_ROTATION_Z) != 0;

        mDrawable = getDrawable();

        mAnimation = new FlipAnimator();
        mAnimation.setAnimationListener(this);
        mAnimation.setInterpolator(interpolator);
        mAnimation.setDuration(duration);

        setOnClickListener(this);

        setImageDrawable(mIsFlipped ? mFlippedDrawable : mDrawable);

        a.recycle();
    }

    public boolean isRotationXEnabled() {
        return mIsRotationXEnabled;
    }

    public void setRotationXEnabled(boolean enabled) {
        mIsRotationXEnabled = enabled;
    }

    public boolean isRotationYEnabled() {
        return mIsRotationYEnabled;
    }

    public void setRotationYEnabled(boolean enabled) {
        mIsRotationYEnabled = enabled;
    }

    public boolean isRotationZEnabled() {
        return mIsRotationZEnabled;
    }

    public void setRotationZEnabled(boolean enabled) {
        mIsRotationZEnabled = enabled;
    }

    public FlipAnimator getFlipAnimation() {
        return mAnimation;
    }

    public void setInterpolator(Interpolator interpolator) {
        mAnimation.setInterpolator(interpolator);
    }

    public void setDuration(int duration) {
        mAnimation.setDuration(duration);
    }

    public boolean isFlipped() {
        return mIsFlipped;
    }

    public boolean isAnimated() {
        return mIsDefaultAnimated;
    }

    public void setAnimated(boolean animated) {
        mIsDefaultAnimated = animated;
    }

    public void setFlipped(boolean flipped) {
        setFlipped(flipped, mIsDefaultAnimated);
    }

    public void setFlipped(boolean flipped, boolean animated) {
        if (flipped != mIsFlipped) {
            toggleFlip(animated);
        }
    }

    public void toggleFlip() {
        toggleFlip(mIsDefaultAnimated);
    }

    public void toggleFlip(boolean animated) {
        if (animated) {
            mAnimation.setToDrawable(mIsFlipped ? mDrawable : mFlippedDrawable);
            startAnimation(mAnimation);
        } else {
            setImageDrawable(mIsFlipped ? mDrawable : mFlippedDrawable);
        }
        mIsFlipped = !mIsFlipped;
    }


    public void setOnFlipListener(OnFlipListener listener) {
        mListener = listener;
    }


    @Override
    public void onClick(View v) {
        toggleFlip();
        if (mListener != null) {
            mListener.onClick(mIsFlipped);
        }
    }

    @Override
    public void onAnimationStart(Animation animation) {
        if (mListener != null) {
            mListener.onFlipStart();
        }
    }

    @Override
    public void onAnimationEnd(Animation animation) {
        if (mListener != null) {
            mListener.onFlipEnd();
        }
    }

    @Override
    public void onAnimationRepeat(Animation animation) {
    }

    /**
     * Animation part All credits goes to coomar
     */
    public class FlipAnimator extends Animation {

        private Camera camera;

        private Drawable toDrawable;

        private float centerX;

        private float centerY;

        private boolean forward = true;

        private boolean visibilitySwapped;

        public void setToDrawable(Drawable to) {
            toDrawable = to;
            visibilitySwapped = false;
        }

        public FlipAnimator() {
            setFillAfter(true);
        }


        @Override
        public void initialize(int width, int height, int parentWidth, int parentHeight) {
            super.initialize(width, height, parentWidth, parentHeight);
            camera = new Camera();
            this.centerX = width / 2;
            this.centerY = height / 2;
        }

        @Override
        protected void applyTransformation(float interpolatedTime, Transformation t) {
            // Angle around the y-axis of the rotation at the given time. It is
            // calculated both in radians and in the equivalent degrees.
            final double radians = Math.PI * interpolatedTime;
            float degrees = (float) (180.0 * radians / Math.PI);

            // Once we reach the midpoint in the animation, we need to hide the
            // source view and show the destination view. We also need to change
            // the angle by 180 degrees so that the destination does not come in
            // flipped around. This is the main problem with SDK sample, it does not
            // do this.
            if (interpolatedTime >= 0.5f) {
                degrees -= 180.f;

                if (!visibilitySwapped) {
                    setImageDrawable(toDrawable);
                    visibilitySwapped = true;
                }
            }

            if (forward) {
                degrees = -degrees;
            }

            final Matrix matrix = t.getMatrix();

            camera.save();
            camera.translate(0.0f, 0.0f, (float) (150.0 * Math.sin(radians)));
            camera.rotateX(mIsRotationXEnabled ? degrees : 0);
            camera.rotateY(mIsRotationYEnabled ? degrees : 0);
            camera.rotateZ(mIsRotationZEnabled ? degrees : 0);
            camera.getMatrix(matrix);
            camera.restore();

            matrix.preTranslate(-centerX, -centerY);
            matrix.postTranslate(centerX, centerY);
        }
    }
}




使用方法:


import android.app.Activity;
import android.os.Bundle;
import android.view.animation.AccelerateDecelerateInterpolator;
import android.view.animation.AccelerateInterpolator;
import android.view.animation.AnticipateOvershootInterpolator;
import android.view.animation.BounceInterpolator;
import android.view.animation.DecelerateInterpolator;
import android.view.animation.Interpolator;
import android.view.animation.OvershootInterpolator;
import android.widget.ArrayAdapter;
import android.widget.CheckBox;
import android.widget.SeekBar;
import android.widget.Spinner;
import android.widget.TextView;

import fr.castorflex.android.flipimageview.R;
import fr.castorflex.android.flipimageview.library.FlipImageView;

public class SampleActivity extends Activity implements FlipImageView.OnFlipListener,
        SeekBar.OnSeekBarChangeListener {

    private static final String[] fData = new String[]{
            "Decelerate",
            "Accelerate",
            "AccelerateDecelerate",
            "Bounce",
            "Overshoot",
            "AnticipateOvershoot"

    };

    private static final Interpolator[] fInterpolators = new Interpolator[]{
            new DecelerateInterpolator(),
            new AccelerateInterpolator(),
            new AccelerateDecelerateInterpolator(),
            new BounceInterpolator(),
            new OvershootInterpolator(),
            new AnticipateOvershootInterpolator()
    };

    private SeekBar mSeekBar;

    private Spinner mSpinner;

    private TextView mTextViewDuration;

    private FlipImageView mFlipImageView;

    private CheckBox mCheckBoxX;

    private CheckBox mCheckBoxY;

    private CheckBox mCheckBoxZ;

    private TextView mTextViewAnimationListener;

    /**
     * Called when the activity is first created.
     */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        mTextViewAnimationListener = (TextView) findViewById(R.id.textview);
        mFlipImageView = (FlipImageView) findViewById(R.id.imageview);
        mSpinner = (Spinner) findViewById(R.id.spinner);
        mTextViewDuration = (TextView) findViewById(R.id.textview_duration);
        mSeekBar = (SeekBar) findViewById(R.id.seekbar);
        mCheckBoxX = (CheckBox) findViewById(R.id.checkedtextview_x);
        mCheckBoxY = (CheckBox) findViewById(R.id.checkedtextview_y);
        mCheckBoxZ = (CheckBox) findViewById(R.id.checkedtextview_z);

        mSpinner.setAdapter(
                new ArrayAdapter<String>(this, android.R.layout.simple_dropdown_item_1line, fData));

        mSeekBar.setOnSeekBarChangeListener(this);

        mFlipImageView.setOnFlipListener(this);
    }

    /FLIP IMAGE VIEW///

    @Override
    public void onClick(boolean flipped) {
        mFlipImageView.setInterpolator(fInterpolators[mSpinner.getSelectedItemPosition()]);
        mFlipImageView.setDuration(mSeekBar.getProgress());
        mFlipImageView.setRotationXEnabled(mCheckBoxX.isChecked());
        mFlipImageView.setRotationYEnabled(mCheckBoxY.isChecked());
        mFlipImageView.setRotationZEnabled(mCheckBoxZ.isChecked());
    }

    @Override
    public void onFlipStart() {
        mTextViewAnimationListener.setText("OnFlipStart");
    }

    @Override
    public void onFlipEnd() {
        mTextViewAnimationListener.setText("OnFlipEnd");
    }

    SEEK BAR/

    @Override
    public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
        mTextViewDuration.setText("" + progress);
    }

    @Override
    public void onStartTrackingTouch(SeekBar seekBar) {
    }

    @Override
    public void onStopTrackingTouch(SeekBar seekBar) {
    }
}





生动的喜欢与收藏按钮MaterialFavoriteButton


http://www.jcodecraeer.com/a/opensource/2015/1009/3555.html

标签:效果,void,private,animation,ImageView,import,android,public,翻转
From: https://blog.51cto.com/u_5454003/6174157

相关文章

  • 自定义View实现HTML图文环绕效果
    Android中并没有提供HTML图文环绕效果的View,最接近的算是TextView中的ImageSpan了,但并未完全实现图文环绕(图文混排)的效果。1、Android系统TextView的ImageSpan实现图文环绕代码如下:TextViewtv=newTextView(this);SpannableStringspanStr=n......
  • 食神摇摇中图片的晃动效果
    可以是这样子实现滴:btn_shake=(ImageView)findViewById(R.id.btn_shake);startShakeAnimation(btn_shake);privatevoidstartShakeAnimation(Viewv){ intpivot=Animation.RELATIVE_TO_SELF; CycleInterpolatorinterpolator=newCycleInterpolator(3.0f); RotateA......
  • 带动画效果的简单Tips显示
    项目地址:https://github.com/nhaarman/supertooltips我比较懒,懒得以后重新写了,所以直接copy了。用法:packagecom.haarman.supertooltips;importandroid.app.Activity;importandroid.os.Bundle;importandroid.os.Handler;importandroid.view.Layout......
  • 刮刮卡效果
    刮刮卡类:packagecom.reyo.view;importandroid.content.Context;importandroid.graphics.Bitmap;importandroid.graphics.Bitmap.Config;importandroid.graphics.Canvas;importandroid.graphics.Paint;importandroid.graphics.Path;importandroid.graphics.Port......
  • 左边可拖出菜单(页面)效果
    这个效果现在很多软件都实现了,其实说穿了很简单的。就是一个动画,做的地道点的加一个手势拖拉效果。我写的代码(未参考任何代码,不知道别人怎么实现的。):importjava.util.ArrayList;importandroid.content.Context;importandroid.os.Bundle;importandroid.util.Log;import......
  • 861. 翻转矩阵后的得分
    题目描述给了一个二维矩阵,矩阵的元素不是0就是1你可以进行任意次操作,让某行或者某列进行翻转元素的得分是每一行二进制的和问怎么操作可以让总得分最大?f1贪心+计算增量基本分析为啥可以贪心?(1)对每行来说,首位肯定是1最好,遮掩某些行需要翻转,某些不翻;(2)对同一列来说,大家的优先......
  • 直播电商平台开发,用el-checkout-group实现穿梭框el-transfer效果
    直播电商平台开发,用el-checkout-group实现穿梭框el-transfer效果一:页面构建 <el-form-itemlabel="选择模块:"prop="pubTime">  <divclass="app-select">   <divclass="list">    <el-inputv-model="searchWord&qu......
  • EasyUI闪屏,EasyUI页面加载提示:原理+代码+效果图
    使用EasyUI时,有个经常遇到的问题,页面还没有渲染完成的时候,就展现了。刚刚开始很混乱,等加载完成后,就好了。    $.parser.onComplete,这个是在所有组件解析完成后执行的事件。其实这个事件很有用的。很多在布局用到easyui的时候总会出现一个问题。就是在一进入主界面的时候,页......
  • web前端tips:CSS之sticky粘滞效果
    Sticky介绍Sticky是CSS3的一个定位属性,它可以让元素在滚动过程中“粘”在屏幕上的某个位置,直到滚动到某个临界点后才会跟随滚动。Sticky定位可以使得页面更具交互性和易用性,也提高了页面的可读性。Sticky定位可以通过以下代码实现:position:sticky;top:0;其中,top属性定义了......
  • vue引入粒子背景图效果
    安装依赖npminstallvue-particles--save-devmain.jsimportVueParticlesfrom'vue-particles'Vue.use(VueParticles)template  <vue-particles     color="#409EFF"     :particleOpacity="0.7"     :parti......