Skip to content

Commit

Permalink
Add the BallView add/remove animation from view itself
Browse files Browse the repository at this point in the history
  • Loading branch information
etet2007 committed Jan 30, 2018
1 parent d7ed929 commit 8562e12
Show file tree
Hide file tree
Showing 5 changed files with 59 additions and 92 deletions.
2 changes: 1 addition & 1 deletion .idea/misc.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 2 additions & 4 deletions app/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -7,15 +7,13 @@
<uses-permission android:name="android.permission.VIBRATE"/>
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/>
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
<!-- android:icon="@mipmap/ic_launcher"
android:roundIcon="@mipmap/ic_launcher_round"

-->

<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher_app"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_app"
android:label="@string/app_name"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<activity
Expand Down
126 changes: 50 additions & 76 deletions app/src/main/java/com/chenyee/stephenlau/floatingball/BallView.java
Original file line number Diff line number Diff line change
Expand Up @@ -253,49 +253,52 @@ public void onAnimationUpdate(ValueAnimator animation) {
});
}

public void refreshAddAnimator() {
Keyframe kf0 = Keyframe.ofFloat(0f, 0);
Keyframe kf1 = Keyframe.ofFloat(1f, ballRadius);
PropertyValuesHolder ballRadiusValuesHolder = PropertyValuesHolder.ofKeyframe("ballRadius", kf0,kf1);
Keyframe kf2 = Keyframe.ofFloat(0f, 0);
Keyframe kf3 = Keyframe.ofFloat(1f, ballRadius+15);
PropertyValuesHolder backgroundRadiusValuesHolder = PropertyValuesHolder.ofKeyframe("mBackgroundRadius", kf2,kf3);

onAddAnimate = ObjectAnimator.ofPropertyValuesHolder(this, ballRadiusValuesHolder,backgroundRadiusValuesHolder);
onAddAnimate.setDuration(400);
onAddAnimate.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
@Override
public void onAnimationUpdate(ValueAnimator animation) {
invalidate();
}});
onAddAnimate.start();

// 另外一种动画,感觉不好
// ValueAnimator.AnimatorUpdateListener update=new ValueAnimator.AnimatorUpdateListener() {
public void performAddAnimator() {
//可以设置更加细致的动画,这里只是修改圆的半径大小,背景图没处理,适用性低。
// Keyframe kf0 = Keyframe.ofFloat(0f, 0);
// Keyframe kf1 = Keyframe.ofFloat(1f, ballRadius);
// PropertyValuesHolder ballRadiusValuesHolder = PropertyValuesHolder.ofKeyframe("ballRadius", kf0,kf1);
// Keyframe kf2 = Keyframe.ofFloat(0f, 0);
// Keyframe kf3 = Keyframe.ofFloat(1f, ballRadius+15);
// PropertyValuesHolder backgroundRadiusValuesHolder = PropertyValuesHolder.ofKeyframe("mBackgroundRadius", kf2,kf3);
//
// onAddAnimate = ObjectAnimator.ofPropertyValuesHolder(this, ballRadiusValuesHolder,backgroundRadiusValuesHolder);
// onAddAnimate.setDuration(400);
// onAddAnimate.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
// @Override
// public void onAnimationUpdate(ValueAnimator animation) {
// invalidate();
// }
// };
// ObjectAnimator animator1=ObjectAnimator.ofFloat(this, "ballRadius", 0F,ballRadius);
// ObjectAnimator animator2=ObjectAnimator.ofFloat(this, "mBackgroundRadius", ballRadius,ballRadius+15);
// mBackgroundRadius=0;
// animator1.addUpdateListener(update);
// animator1.setDuration(300);
// animator2.setDuration(100);
// animator2.addUpdateListener(update);
// AnimatorSet set = new AnimatorSet();
// set.play(animator2).after(animator1);
// set.start();
// set.addListener(new Animator.AnimatorListener() {
// }});
// onAddAnimate.start();

//View自带的动画。
setScaleX(0);
setScaleY(0);
animate()
.scaleY(1).scaleX(1)
.setDuration(200)
.start();
}

public void performRemoveAnimator() {
// Keyframe kf0 = Keyframe.ofFloat(0f, ballRadius);
// Keyframe kf1 = Keyframe.ofFloat(1f, 0);
// PropertyValuesHolder ballRadiusValuesHolder = PropertyValuesHolder.ofKeyframe("ballRadius", kf0,kf1);
// Keyframe kf2 = Keyframe.ofFloat(0f, mBackgroundRadius);
// Keyframe kf3 = Keyframe.ofFloat(1f, 0);
// PropertyValuesHolder backgroundRadiusValuesHolder = PropertyValuesHolder.ofKeyframe("mBackgroundRadius", kf2,kf3);
//
//
// onRemoveAnimate = ObjectAnimator.ofPropertyValuesHolder(this, ballRadiusValuesHolder,backgroundRadiusValuesHolder);
// onRemoveAnimate.addListener(new Animator.AnimatorListener() {
// @Override
// public void onAnimationStart(Animator animation) {
//
// }
//
// @Override
// public void onAnimationEnd(Animator animation) {
//
// WindowManager windowManager = (WindowManager) getContext().getSystemService(Context.WINDOW_SERVICE);
// windowManager.removeView(BallView.this);
// }
//
// @Override
Expand All @@ -308,48 +311,20 @@ public void onAnimationUpdate(ValueAnimator animation) {
//
// }
// });
// onRemoveAnimate.setDuration(400);
// onRemoveAnimate.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
// @Override
// public void onAnimationUpdate(ValueAnimator animation) {
// invalidate();
// }
// });
// onRemoveAnimate.start();

}

public void refreshRemoveAnimator() {
Keyframe kf0 = Keyframe.ofFloat(0f, ballRadius);
Keyframe kf1 = Keyframe.ofFloat(1f, 0);
PropertyValuesHolder ballRadiusValuesHolder = PropertyValuesHolder.ofKeyframe("ballRadius", kf0,kf1);
Keyframe kf2 = Keyframe.ofFloat(0f, mBackgroundRadius);
Keyframe kf3 = Keyframe.ofFloat(1f, 0);
PropertyValuesHolder backgroundRadiusValuesHolder = PropertyValuesHolder.ofKeyframe("mBackgroundRadius", kf2,kf3);


onRemoveAnimate = ObjectAnimator.ofPropertyValuesHolder(this, ballRadiusValuesHolder,backgroundRadiusValuesHolder);
onRemoveAnimate.addListener(new Animator.AnimatorListener() {
@Override
public void onAnimationStart(Animator animation) {
}

@Override
public void onAnimationEnd(Animator animation) {
WindowManager windowManager = (WindowManager) getContext().getSystemService(Context.WINDOW_SERVICE);
windowManager.removeView(BallView.this);
}

@Override
public void onAnimationCancel(Animator animation) {

}

@Override
public void onAnimationRepeat(Animator animation) {

}
});
onRemoveAnimate.setDuration(400);
onRemoveAnimate.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
@Override
public void onAnimationUpdate(ValueAnimator animation) {
invalidate();
}
});
onRemoveAnimate.start();
//View自带的动画。
animate()
.scaleY(0).scaleX(0)
.setDuration(200)
.start();
}

@Override
Expand All @@ -361,7 +336,6 @@ protected void onDraw(Canvas canvas) {
canvas.drawCircle(ballCenterX, ballCenterY, ballRadius, mBallPaint);
if(useBackground)
canvas.drawBitmap(bitmapCrop,-bitmapCrop.getWidth()/2+ballCenterX,-bitmapCrop.getHeight()/2+ballCenterY,mBallPaint);

}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@
import android.graphics.Point;
import android.os.Build;
import android.preference.PreferenceManager;
import android.view.Display;
import android.view.Gravity;
import android.view.WindowManager;
import android.view.WindowManager.LayoutParams;
Expand Down Expand Up @@ -70,7 +69,7 @@ public void addBallView(Context context) {

windowManager.addView(mBallView, params);

updateData();
updateBallViewData();

// mBallView.setOpacity(defaultSharedPreferences.getInt("opacity",125));
// mBallView.changeFloatBallSizeWithRadius(defaultSharedPreferences.getInt("size",25));
Expand All @@ -85,7 +84,7 @@ public void addBallView(Context context) {
// useBackground = defaultSharedPreferences.getBoolean("useBackground", false);
// setUseBackground(useBackground);

mBallView.refreshAddAnimator();
mBallView.performAddAnimator();

isOpenBall=true;
saveFloatBallData();
Expand All @@ -94,9 +93,10 @@ public void addBallView(Context context) {

public void removeBallView(final Context context) {
if (mBallView != null) {
mBallView.refreshRemoveAnimator();
isOpenBall=false;

mBallView.performRemoveAnimator();

saveFloatBallData();
mBallView = null;
}
Expand Down Expand Up @@ -125,7 +125,6 @@ public void setSize(int size) {

mBallView.invalidate();
}

}
public void setBackgroundPic(Context context,String imagePath){
if (mBallView != null) {
Expand Down Expand Up @@ -159,7 +158,7 @@ public void setUseBackground(boolean useBackground) {
}
}
// 根据SharedPreferences中的数据更新BallView的显示参数
public void updateData() {
public void updateBallViewData() {
if (mBallView != null) {
//Opacity
mBallView.setOpacity(defaultSharedPreferences.getInt(SharedPreferencesUtil.KEY_OPACITY,125));
Expand Down
Original file line number Diff line number Diff line change
@@ -1,15 +1,11 @@
package com.chenyee.stephenlau.floatingball;

import android.accessibilityservice.AccessibilityService;
import android.content.Context;
import android.content.Intent;
import android.os.Build;
import android.os.Bundle;
import android.os.ResultReceiver;
import android.util.Log;
import android.view.View;
import android.view.accessibility.AccessibilityEvent;
import android.view.inputmethod.InputMethodManager;


/**
Expand Down Expand Up @@ -127,7 +123,7 @@ public int onStartCommand(Intent intent, int flags, int startId) {
}

if(type==TYPE_UPDATE_DATA){
mFloatBallManager.updateData();
mFloatBallManager.updateBallViewData();
}
}
}
Expand Down

0 comments on commit 8562e12

Please sign in to comment.