下面列出了android.view.View#startAnimation ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
@Override
public boolean onPopPageAnimation(
View oldPageView,
View newPageView,
AnimationDirection animationDirection) {
if (animationDirection == AnimationDirection.FROM_LEFT) {
oldPageView.startAnimation(mShrinkOutFromLeftAnimation);
if (newPageView != null) {
newPageView.startAnimation(mExpanndInFromLeftAnimation);
}
} else {
oldPageView.startAnimation(mShrinkOutFromRightAnimation);
if (newPageView != null) {
newPageView.startAnimation(mExpandInFromRightAnimation);
}
}
return true;
}
public static void expand(final View v) {
v.measure(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT);
final int targetHeight = v.getMeasuredHeight();
// Older versions of android (pre API 21) cancel animations for views with a height of 0.
v.getLayoutParams().height = 1;
v.setVisibility(View.VISIBLE);
Animation a = new Animation() {
@Override
protected void applyTransformation(float interpolatedTime, Transformation t) {
v.getLayoutParams().height = interpolatedTime == 1
? ViewGroup.LayoutParams.WRAP_CONTENT
: (int) (targetHeight * interpolatedTime);
v.requestLayout();
}
@Override
public boolean willChangeBounds() {
return true;
}
};
// 1dp/ms
a.setDuration((int) (targetHeight / v.getContext().getResources().getDisplayMetrics().density));
v.startAnimation(a);
}
@Override
public void onClick(View v) {
switch (v.getId()){
case R.id.page_divider_page:
delegate.showPageSelectDialog(pageNumber, maxPage);
break;
case R.id.page_divider_refresh:
delegate.refreshPage(pageNumber);
RotateAnimation rot = new RotateAnimation(0, 360, Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF, 0.5f);
rot.setRepeatMode(Animation.RESTART);
rot.setRepeatCount(Animation.INFINITE);
v.startAnimation(rot);
break;
case R.id.page_divider_scroll:
delegate.scrollToTop();
break;
}
}
public static void expand(final View v) {
v.measure(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT);
final int targtetHeight = v.getMeasuredHeight();
v.getLayoutParams().height = 0;
v.setVisibility(View.VISIBLE);
Animation a = new Animation()
{
@Override
protected void applyTransformation(float interpolatedTime, Transformation t) {
v.getLayoutParams().height = interpolatedTime == 1
? LayoutParams.WRAP_CONTENT
: (int)(targtetHeight * interpolatedTime);
v.requestLayout();
}
@Override
public boolean willChangeBounds() {
return true;
}
};
// 1dp/ms
a.setDuration(500);
v.startAnimation(a);
}
private void animate(View v, int duration, int visibility)
{
AlphaAnimation alphaAnimation = (visibility == View.VISIBLE) ? new AlphaAnimation(0f, 1f) : new AlphaAnimation(1f, 0f);
alphaAnimation.setDuration(duration);
alphaAnimation.setFillAfter(true);
v.startAnimation(alphaAnimation);
}
private void animateButtonIn(View button, int delay) {
AnimationSet animation = new AnimationSet(true);
Animation scale = new ScaleAnimation(0.0f, 1.0f, 0.0f, 1.0f,
Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF, 0.0f);
animation.addAnimation(scale);
animation.setInterpolator(new OvershootInterpolator(1));
animation.setDuration(ANIMATION_DURATION);
animation.setStartOffset(delay);
button.startAnimation(animation);
}
protected void setAnimation(View viewToAnimate, int position) {
if (position > lastPosition) {
Animation animation = AnimationUtils.loadAnimation(viewToAnimate.getContext(), R
.anim.item_bottom_in);
viewToAnimate.startAnimation(animation);
lastPosition = position;
}
}
private void fillUp(int pos, int nextBottom, boolean isAnim) {
int end = 0;
while (nextBottom > end && pos >= 0) {
View child = makeAndAddView(pos, nextBottom, false, mListPadding.left, false);
nextBottom = child.getTop() - mDividerHeight;
if (isAnim && mItemAnimForTopIn != 0 && child.getVisibility() == View.VISIBLE) {
child.startAnimation(AnimationUtils.loadAnimation(getContext(), mItemAnimForTopIn));
}
pos--;
}
mFirstPosition = pos + 1;
}
private void collapse(final View v)
{
final int initialHeight = v.getMeasuredHeight();
animation = new Animation()
{
@Override
protected void applyTransformation(float interpolatedTime, Transformation t) {
if(interpolatedTime == 1)
{
v.setVisibility(View.GONE);
isOpened = false;
}
else{
v.getLayoutParams().height = initialHeight - (int)(initialHeight * interpolatedTime);
v.requestLayout();
}
}
@Override
public boolean willChangeBounds() {
return true;
}
};
animation.setDuration(duration);
v.startAnimation(animation);
}
public void btnScaleSelf(View view) {
ScaleAnimation sa = new ScaleAnimation(0, 1, 0, 1,
Animation.RELATIVE_TO_SELF, 0.5F,
Animation.RELATIVE_TO_SELF, 0.5F);
sa.setDuration(1000);
view.startAnimation(sa);
}
public static void scaleY(View view, float f) {
if (view == null)
return;
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB) {
view.setScaleY(f);
} else {
ScaleAnimation animation =new ScaleAnimation(f, f, f, f,
Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF, 0.5f);
animation.setDuration(0);
animation.setFillAfter(true);
view.startAnimation(animation);
}
}
public static void expand(final View v) {
v.measure(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT);
final int targetHeight = v.getMeasuredHeight();
// Older versions of android (pre API 21) cancel animations for views with a height of 0.
v.getLayoutParams().height = 1;
v.setVisibility(View.VISIBLE);
Animation a = new Animation() {
@Override
public boolean willChangeBounds() {
return true;
} @Override
protected void applyTransformation(float interpolatedTime, Transformation t) {
v.getLayoutParams().height = interpolatedTime == 1
? ViewGroup.LayoutParams.WRAP_CONTENT
: (int) (targetHeight * interpolatedTime);
v.requestLayout();
}
};
// 1dp/ms
a.setDuration((int) (targetHeight / v.getContext().getResources().getDisplayMetrics().density));
a.setInterpolator(new AccelerateInterpolator(3f));
v.startAnimation(a);
}
private void setAnimation(View viewToAnimate, int position) {
if (position > lastPosition) {
Animation animation = AnimationUtils.loadAnimation(viewToAnimate.getContext(), R
.anim.item_bottom_in);
viewToAnimate.startAnimation(animation);
lastPosition = position;
}
}
protected void showViewById(int viewId, boolean animate) {
View content = mFragmentView.findViewById(viewId);
if (animate) {
mAnimationIn.reset();
content.startAnimation(mAnimationIn);
} else {
content.clearAnimation();
}
content.setVisibility(View.VISIBLE);
}
private void changeEmptyType() {
setDefaultValues();
refreshMessages();
// insert views in the root view
if (!mViewsAdded) {
RelativeLayout.LayoutParams lp = new RelativeLayout.LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT);
lp.addRule(RelativeLayout.CENTER_IN_PARENT);
mEmptyRelativeLayout = new RelativeLayout(getContext());
mEmptyRelativeLayout.setGravity(Gravity.CENTER);
mEmptyRelativeLayout.setLayoutParams(lp);
if (mEmptyView!=null) mEmptyRelativeLayout.addView(mEmptyView);
if (mLoadingView!=null) mEmptyRelativeLayout.addView(mLoadingView);
if (mErrorView!=null) mEmptyRelativeLayout.addView(mErrorView);
mViewsAdded = true;
mEmptyRelativeLayout.setVisibility(VISIBLE);
addView(mEmptyRelativeLayout);
}
// change empty type
View loadingAnimationView = null;
if (mLoadingAnimationViewId > 0) loadingAnimationView = findViewById(mLoadingAnimationViewId);
switch (mEmptyType) {
case TYPE_EMPTY:
if (mEmptyView!=null) mEmptyView.setVisibility(View.VISIBLE);
if (mErrorView!=null) mErrorView.setVisibility(View.GONE);
if (mLoadingView!=null) {
mLoadingView.setVisibility(View.GONE);
if (loadingAnimationView!=null && loadingAnimationView.getAnimation()!=null) loadingAnimationView.getAnimation().cancel();
}
break;
case TYPE_ERROR:
if (mEmptyView!=null) mEmptyView.setVisibility(View.GONE);
if (mErrorView!=null) mErrorView.setVisibility(View.VISIBLE);
if (mLoadingView!=null) {
mLoadingView.setVisibility(View.GONE);
if (loadingAnimationView!=null && loadingAnimationView.getAnimation()!=null) loadingAnimationView.getAnimation().cancel();
}
break;
case TYPE_LOADING:
if (mEmptyView!=null) mEmptyView.setVisibility(View.GONE);
if (mErrorView!=null) mErrorView.setVisibility(View.GONE);
if (mLoadingView!=null) {
mLoadingView.setVisibility(View.VISIBLE);
if (mLoadingAnimation != null && loadingAnimationView!=null) {
loadingAnimationView.startAnimation(mLoadingAnimation);
}
else if (loadingAnimationView!=null) {
loadingAnimationView.startAnimation(getRotateAnimation());
}
}
break;
default:
break;
}
}
public static void expand(final View v) {
Animation a = expandAction(v);
v.startAnimation(a);
}
/**
* Assign a "slide in from left" animation to the given view
* @param view The view which we want to slide
* @param duration the duration of the animation in milliseconds
*/
public static void setSlideInFromLeftAnimation(View view, long duration){
Animation slide = AnimationUtils.loadAnimation(view.getContext(), R.anim.slide_in_from_left);
slide.setDuration(duration);
view.startAnimation(slide);
}
private void animateFromRightPaneOffscreen(View view) {
Animation animation = new LeftMarginAnimation(view, mLeftPaneWidth, mScreenWidth+1);
view.startAnimation(animation);
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
SearchResult option = getItem(position);
if (convertView == null) {
convertView = LayoutInflater.from(getContext()).inflate(
R.layout.search_option, parent, false);
if (animate) {
Animation anim = AnimationUtils.loadAnimation(context,
R.anim.anim_down);
anim.setDuration(400);
convertView.startAnimation(anim);
if (count == this.getCount()) {
animate = false;
}
count++;
}
}
View border = convertView.findViewById(R.id.border);
if (position == 0) {
border.setVisibility(View.VISIBLE);
} else {
border.setVisibility(View.GONE);
}
final TextView title = (TextView) convertView
.findViewById(R.id.title);
title.setText(option.title);
ImageView icon = (ImageView) convertView.findViewById(R.id.icon);
if (option.icon != null) {
icon.setImageDrawable(option.icon);
} else {
icon.setImageResource(option.drawableResId);
}
ImageView up = (ImageView) convertView.findViewById(R.id.up);
up.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
setSearchString(title.getText().toString());
search.setSelection(search.getText().length());
}
});
return convertView;
}
private void setFadeAnimation(View view) {
AlphaAnimation anim = new AlphaAnimation(0.0f, 1.0f);
anim.setDuration(FADE_DURATION_MS);
view.startAnimation(anim);
}