码迷,mamicode.com
首页 > 移动开发 > 详细

安卓 摇一摇 包含 objectAnimator valueAnimator的动画效果

时间:2017-10-10 11:22:55      阅读:191      评论:0      收藏:0      [点我收藏+]

标签:create   mcu   res   imp   gets   paint   defs   rri   animator   

public class MyAnimationDemo extends BaseActivity  implements SensorEventListener{
    private View mCircleLight;
    private ObjectAnimator mCircleLightAnimator;
    private CurtainView mCurtainView;
    private ImageView mIvStar;
    private ObjectAnimator mIvStarAnimator;
    private long lastShakeTime;
    private int mAnimationCurrentState;

    private Sensor mAccecerateSonsor;
    private SensorManager mSensorManager;

    @Override
    public void onAccuracyChanged(Sensor sensor, int accuracy) {

    }//精度发生变化时触发

    @Override
    public void onSensorChanged(SensorEvent event) {
        if (event.sensor.getType()==TYPE_ACCELEROMETER) {
            float value[] = event.values;
            float x = value[0];
            float y = value[1];
            float z = value[2];
            if (System.currentTimeMillis() - lastShakeTime > 2000 &&Math.abs(x)>17&&Math.abs(y)>17&&Math.abs(z)>17){
                lastShakeTime = System.currentTimeMillis();
                startAnimation();

            }

        }
    }//传感器value值发生变化时触发

    @Override
    public void onNetworkChanged() {

    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.acrivity_animation_demo);
        initview();
        initAnimator();
    }
    private void initview(){
        mCircleLight = (View)findViewById(R.id.animation_light);
        mCurtainView = (CurtainView)findViewById(R.id.animation_curtain);
        mIvStar = (ImageView)findViewById(R.id.animation_star);
    }

    private void initAnimator(){
        mCircleLightAnimator = new ObjectAnimator();
        mCircleLightAnimator.setTarget(mCircleLight);
        mCircleLightAnimator.setPropertyName("rotation");
        mCircleLightAnimator.setFloatValues(0,360);
        mCircleLightAnimator.setDuration(1000);
        mCircleLightAnimator.setRepeatMode(ValueAnimator.RESTART);
        mCircleLightAnimator.setInterpolator(new LinearInterpolator());
        mCircleLightAnimator.addListener(new Animator.AnimatorListener() {
            @Override
            public void onAnimationStart(Animator animation) {

            }

            @Override
            public void onAnimationEnd(Animator animation) {
                mCurtainView.closeWindow();
            }

            @Override
            public void onAnimationCancel(Animator animation) {

            }

            @Override
            public void onAnimationRepeat(Animator animation) {

            }
        });

        mIvStarAnimator = new ObjectAnimator();
       // mIvStarAnimator.ofFloat(mIvStar,"rotationY",0,180);
        mIvStarAnimator.setTarget(mIvStar);
        mIvStarAnimator.setPropertyName("rotateX");
        mIvStarAnimator.setFloatValues(0,360);
        mIvStarAnimator.setInterpolator(new LinearInterpolator());
        mIvStarAnimator.setDuration(2000);
        mIvStarAnimator.setRepeatMode(ValueAnimator.RESTART);

    }
    private void startAnimation(){
        mCurtainView.setVisibility(View.VISIBLE);
        mCurtainView.openWindow();
        mCircleLightAnimator.start();
    }

    @Override
    protected void onResume() {
        super.onResume();
        mSensorManager =(SensorManager) getApplicationContext().getSystemService(SENSOR_SERVICE);
        if (mSensorManager != null) {
            mAccecerateSonsor = mSensorManager.getDefaultSensor(Sensor.TYPE_ACCELEROMETER);
            if (mAccecerateSonsor!=null){
                mSensorManager.registerListener(this,mAccecerateSonsor,SensorManager.SENSOR_DELAY_UI);
            }
        }
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        if (mSensorManager != null) {
            mAccecerateSonsor = mSensorManager.getDefaultSensor(Sensor.TYPE_ACCELEROMETER);
            if (mAccecerateSonsor!=null){
                mSensorManager.unregisterListener(this,mAccecerateSonsor);
            }
        }

    }
}
onpause的时候也记得注销 传感器呀
自定义view 利用ValueAnimator实现窗帘开闭合动画

public
class CurtainView extends View implements ValueAnimator.AnimatorUpdateListener,Animation.AnimationListener { int mCurtainstate = 0;//0 完全打开 1 打开 2 闭合动作 3 完全闭合 private Paint mPaint; private RectF mRectF; private ValueAnimator mAnimator; public CurtainView(Context context) { this(context,null); } public CurtainView(Context context, @Nullable AttributeSet attrs) { this(context, attrs,0); } public CurtainView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) { super(context, attrs, defStyleAttr); mPaint = new Paint(); mPaint.setColor(Color.WHITE); mAnimator = new ValueAnimator(); mAnimator.setDuration(500); mAnimator.addUpdateListener(this); mAnimator.setInterpolator(new DecelerateInterpolator()); } @Override protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) { super.onMeasure(widthMeasureSpec, heightMeasureSpec); setMeasuredDimension(Math.min(widthMeasureSpec,heightMeasureSpec),Math.min(widthMeasureSpec,heightMeasureSpec)); } @Override protected void onDraw(Canvas canvas) { super.onDraw(canvas); if (mRectF==null){ mRectF = new RectF(0,0,getMeasuredWidth(),getMeasuredHeight()); } if (mAnimator.getAnimatedValue()!=null){ int i = (int)mAnimator.getAnimatedValue(); canvas.drawArc(mRectF,90-i,i*2,false,mPaint);//s上半边窗帘 canvas.drawArc(mRectF,270-i,i*2,false,mPaint); } } public void openWindow(){ mCurtainstate = 1; mAnimator.setIntValues(0,90); mAnimator.start(); } public void closeWindow(){ mCurtainstate=2; mAnimator.setIntValues((int)mAnimator.getAnimatedValue(),0); mAnimator.start(); } @Override public void onAnimationUpdate(ValueAnimator animation) { invalidate(); } @Override public void onAnimationRepeat(Animation animation) { } @Override public void onAnimationStart(Animation animation) { } @Override public void onAnimationEnd(Animation animation) { if (mCurtainstate ==1){ mCurtainstate = 0; }else if (mCurtainstate==2){ mCurtainstate=3; } } }

 

安卓 摇一摇 包含 objectAnimator valueAnimator的动画效果

标签:create   mcu   res   imp   gets   paint   defs   rri   animator   

原文地址:http://www.cnblogs.com/vitabebeauty/p/7644379.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!