下面列出了android.view.animation.TranslateAnimation#setFillEnabled() 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
/**
* Same as moveCard, but without the user specified speed factor. Used for the win animation.
*
* @param card The card to move
* @param pX X-coordinate of the destination
* @param pY Y-coordinate of the destination
*/
public void moveCardSlow(final Card card, final float pX, final float pY) {
final CustomImageView view = card.view;
if (card.isInvisible()) {
return;
}
int distance = (int) Math.sqrt(Math.pow(pX - view.getX(), 2) + Math.pow(pY - view.getY(), 2));
TranslateAnimation animation = new TranslateAnimation(0, pX - view.getX(), 0, pY - view.getY());
try {
animation.setDuration((long) (distance * 100 / Card.width));
} catch (ArithmeticException e) {
animation.setDuration(200);
Log.e("Animate moveCard()", e.toString());
}
animation.setFillEnabled(true);
view.setDestination(pX, pY);
view.startAnimation(animation);
}
/**
* Moves a card to a new destination. FillEnabled is necessary, or else flickering will occur.
* The location is updated when the animation finishes, which happens in the onAnimationEnd()
* method of the custom image view.
*
* @param card The card to move
* @param pX X-coordinate of the destination
* @param pY Y-coordinate of the destination
*/
public void moveCard(final Card card, final float pX, final float pY) {
final CustomImageView view = card.view;
int distance = (int) Math.sqrt(Math.pow(pX - view.getX(), 2) + Math.pow(pY - view.getY(), 2));
TranslateAnimation animation = new TranslateAnimation(0, pX - view.getX(), 0, pY - view.getY());
//there were some reports about an exception here, so simply set duration with a fixed value
//if the exception occurs
try {
animation.setDuration((long) (distance * 100 / Card.width / speedFactor));
} catch (ArithmeticException e) {
animation.setDuration(100);
Log.e("Animate moveCard()", e.toString());
}
animation.setFillEnabled(true);
view.setDestination(pX, pY);
view.startAnimation(animation);
}
/**
* true the underline control to the click position
* @param position
*/
public void changeWeekDay(int position){
float offset = (float) (getWidth() - getPaddingLeft() - getPaddingRight()) / 7;
LinearLayout.LayoutParams params = (LayoutParams) iv_underline.getLayoutParams();
params.width = (int) offset;
iv_underline.setLayoutParams(params);
TranslateAnimation animation = new TranslateAnimation(
offset * currentPosition ,position * offset,0,0);
animation.setFillAfter(true);
animation.setFillEnabled(true);
animation.setDuration(300);
iv_underline.startAnimation(animation);
setTabTextStyle(currentPosition,false);
setTabTextStyle(position,true);
currentPosition = position;
}
@SuppressLint("NewApi")
public static void translateY(final View view, float fromY, float toY, long duration)
{
if (Build.VERSION.SDK_INT >= 12)
{
if (duration == 0)
view.setTranslationY(toY);
else
view.animate().translationY(toY).setDuration(duration).start();
}
else
{
TranslateAnimation translate = new TranslateAnimation(0, 0, fromY, toY);
translate.setDuration(duration);
translate.setFillEnabled(true);
translate.setFillBefore(true);
translate.setFillAfter(true);
addAnimation(view, translate);
}
}
private void bounceBackHeader(){
int yTranslate = state == State.REFRESHING ?
header.getHeight() - headerContainer.getHeight() :
-headerContainer.getHeight() - headerContainer.getTop();
bounceAnimation = new TranslateAnimation(
TranslateAnimation.ABSOLUTE, 0,
TranslateAnimation.ABSOLUTE, 0,
TranslateAnimation.ABSOLUTE, 0,
TranslateAnimation.ABSOLUTE, yTranslate);
bounceAnimation.setDuration(BOUNCE_ANIMATION_DURATION);
bounceAnimation.setFillEnabled(true);
bounceAnimation.setFillAfter(false);
bounceAnimation.setFillBefore(true);
//bounceAnimation.setInterpolator(new OvershootInterpolator(BOUNCE_OVERSHOOT_TENSION));
bounceAnimation.setAnimationListener(new HeaderAnimationListener(yTranslate));
startAnimation(bounceAnimation);
}
public static TranslateAnimation getPortraitTranslateAnimation(int start, int end, int durationMillis) {
TranslateAnimation translateAnimation = new TranslateAnimation(0.0F, 0.0F, (float) start, (float) end);
translateAnimation.setDuration((long) durationMillis);
translateAnimation.setFillEnabled(true);
translateAnimation.setFillAfter(true);
return translateAnimation;
}
private TranslateAnimation createTranslateAnimation(Point oldOffset, Point newOffset) {
TranslateAnimation translate = new TranslateAnimation(Animation.ABSOLUTE, oldOffset.x,
Animation.ABSOLUTE, newOffset.x,
Animation.ABSOLUTE, oldOffset.y,
Animation.ABSOLUTE, newOffset.y);
translate.setDuration(ANIMATION_DURATION);
translate.setFillEnabled(true);
translate.setFillAfter(true);
translate.setInterpolator(new AccelerateDecelerateInterpolator());
return translate;
}
public static void translateY(final View view, float fromY, float toY, long duration) {
if (Build.VERSION.SDK_INT >= 12) {
if (duration == 0)
view.setTranslationY(toY);
else
view.animate().translationY(toY).setDuration(duration).start();
} else {
TranslateAnimation translate = new TranslateAnimation(0, 0, fromY, toY);
translate.setDuration(duration);
translate.setFillEnabled(true);
translate.setFillBefore(true);
translate.setFillAfter(true);
addAnimation(view, translate);
}
}
protected final void translateChild(View v, Point prev, Point now) {
TranslateAnimation translate = new TranslateAnimation(Animation.ABSOLUTE, -now.x + prev.x, Animation.ABSOLUTE, 0, Animation.ABSOLUTE, -now.y
+ prev.y, Animation.ABSOLUTE, 0);
translate.setInterpolator(new AccelerateInterpolator(4f));
translate.setDuration(350);
translate.setFillEnabled(false);
translate.setFillAfter(false);
v.clearAnimation();
v.startAnimation(translate);
}
protected final void translateChild(View v, Point prev, Point now) {
TranslateAnimation translate = new TranslateAnimation(Animation.ABSOLUTE, -now.x + prev.x, Animation.ABSOLUTE, 0, Animation.ABSOLUTE, -now.y
+ prev.y, Animation.ABSOLUTE, 0);
translate.setInterpolator(new AccelerateInterpolator(4f));
translate.setDuration(350);
translate.setFillEnabled(false);
translate.setFillAfter(false);
v.clearAnimation();
v.startAnimation(translate);
}
protected final void translateChild(View v, Point prev, Point now) {
TranslateAnimation translate = new TranslateAnimation(Animation.ABSOLUTE, -now.x + prev.x, Animation.ABSOLUTE, 0, Animation.ABSOLUTE, -now.y
+ prev.y, Animation.ABSOLUTE, 0);
translate.setInterpolator(new AccelerateInterpolator(4f));
translate.setDuration(350);
translate.setFillEnabled(false);
translate.setFillAfter(false);
v.clearAnimation();
v.startAnimation(translate);
}
private void animateGap(int target) {
for (int i = 0; i < getChildCount(); i++) {
View v = getChildAt(i);
if (i == mLastDragged) {
continue;
}
int newPos = i;
if (mLastDragged < target && i >= mLastDragged + 1 && i <= target) {
newPos--;
} else if (target < mLastDragged && i >= target && i < mLastDragged) {
newPos++;
}
int oldPos = i;
if (newPositions.get(i) != -1) {
oldPos = newPositions.get(i);
}
if (oldPos == newPos) {
continue;
}
// animate
DEBUG_LOG("animateGap from=" + oldPos + ", to=" + newPos);
final Rect oldRect = getRectByPosition(oldPos);
final Rect newRect = getRectByPosition(newPos);
oldRect.offset(-v.getLeft(), -v.getTop());
newRect.offset(-v.getLeft(), -v.getTop());
TranslateAnimation translate = new TranslateAnimation(
oldRect.left, newRect.left,
oldRect.top, newRect.top);
translate.setDuration(ANIMATION_DURATION);
translate.setFillEnabled(true);
translate.setFillAfter(true);
v.clearAnimation();
v.startAnimation(translate);
newPositions.set(i, newPos);
}
}
private void animateGap(int target) {
for (int i = 0; i < getChildCount(); i++) {
View v = getChildAt(i);
if (i == mLastDragged) {
continue;
}
int newPos = i;
if (mLastDragged < target && i >= mLastDragged + 1 && i <= target) {
newPos--;
} else if (target < mLastDragged && i >= target && i < mLastDragged) {
newPos++;
}
int oldPos = i;
if (newPositions.get(i) != -1) {
oldPos = newPositions.get(i);
}
if (oldPos == newPos) {
continue;
}
// animate
DEBUG_LOG("animateGap from=" + oldPos + ", to=" + newPos);
final Rect oldRect = getRectByPosition(oldPos);
final Rect newRect = getRectByPosition(newPos);
oldRect.offset(-v.getLeft(), -v.getTop());
newRect.offset(-v.getLeft(), -v.getTop());
TranslateAnimation translate = new TranslateAnimation(
oldRect.left, newRect.left,
oldRect.top, newRect.top);
translate.setDuration(ANIMATION_DURATION);
translate.setFillEnabled(true);
translate.setFillAfter(true);
v.clearAnimation();
v.startAnimation(translate);
newPositions.set(i, newPos);
}
}
private void animateGap(int target) {
for (int i = 0; i < getChildCount(); i++) {
View v = getChildAt(i);
if (i == mLastDragged) {
continue;
}
int newPos = i;
if (mLastDragged < target && i >= mLastDragged + 1 && i <= target) {
newPos--;
} else if (target < mLastDragged && i >= target && i < mLastDragged) {
newPos++;
}
int oldPos = i;
if (newPositions.get(i) != -1) {
oldPos = newPositions.get(i);
}
if (oldPos == newPos) {
continue;
}
// animate
DEBUG_LOG("animateGap from=" + oldPos + ", to=" + newPos);
final Rect oldRect = getRectByPosition(oldPos);
final Rect newRect = getRectByPosition(newPos);
oldRect.offset(-v.getLeft(), -v.getTop());
newRect.offset(-v.getLeft(), -v.getTop());
TranslateAnimation translate = new TranslateAnimation(
oldRect.left, newRect.left,
oldRect.top, newRect.top);
translate.setDuration(ANIMATION_DURATION);
translate.setFillEnabled(true);
translate.setFillAfter(true);
v.clearAnimation();
v.startAnimation(translate);
newPositions.set(i, newPos);
}
}