一万年来谁著史,三千里外欲封侯。这篇文章主要讲述安卓 摇一摇 包含 objectAnimatorvalueAnimator的动画效果相关的知识,希望能为你提供帮助。
public class MyAnimationDemo extends BaseActivityimplements 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); }} publicvoid openWindow(){ mCurtainstate = 1; mAnimator.setIntValues(0,90); mAnimator.start(); }publicvoid 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; } } }
【安卓 摇一摇 包含 objectAnimatorvalueAnimator的动画效果】
推荐阅读
- Springboot启动后报错This application has no explicit mapping for /error, so you are seeing this as a fal
- Android零基础入门第73节(Activity初入门,创建和配置如此简单)
- Android签名信息查看
- 高德地图测两点距离android比较精确的
- 2017.10.9 response对象application对象session对象的区别
- 安卓(android)建立项目时失败,出现Android Manifest.xml file missing几种解决方法()
- 删除过期备份时报no channel allocated for maintenance(of an appropriate type)
- Xcode9.0+appium1.6.5真机环境运行app及遇坑知多少
- Android页面路由框架 —— ARouter