AutoTransition(主要用于width和height的变化时的过渡动画效果)
用法:
private void reduce() {
//设置收缩状态时的布局
RelativeLayout.LayoutParams layoutParams = (RelativeLayout.LayoutParams) llSearch.getLayoutParams();
layoutParams.width = dip2px(80);
layoutParams.setMargins(dip2px(10), dip2px(10), dip2px(10), dip2px(10));
llSearch.setLayoutParams(layoutParams);
//开始动画
beginDelayedTransition(llSearch);
}
/**
* 开始动画
*/
private void beginDelayedTransition(ViewGroup view) {
AutoTransition autoTransition = new AutoTransition();
autoTransition.setDuration(300);
TransitionManager.beginDelayedTransition(view, autoTransition);
}
private int dip2px(float dpVale) {
final float scale = getResources().getDisplayMetrics().density;
return (int) (dpVale * scale + 0.5f);
}
ChangeImageTransform() (主要用于改变 ImageView 的 scaleType 属性时)
TransitionManager.beginDelayedTransition(transitionsContainer, new TransitionSet()
.addTransition(new ChangeBounds())
.addTransition(new ChangeImageTransform()));
ViewGroup.LayoutParams params = imageView.getLayoutParams();
params.height = expanded ? ViewGroup.LayoutParams.MATCH_PARENT :
ViewGroup.LayoutParams.WRAP_CONTENT;
imageView.setLayoutParams(params);
imageView.setScaleType(expanded ? ImageView.ScaleType.CENTER_CROP :
ImageView.ScaleType.FIT_CENTER);
自定义ProgressTransition
private class ProgressTransition extends Transition {
/**
* Property is like a helper that contain setter and getter in one place
*/
private static final Property<ProgressBar, Integer> PROGRESS_PROPERTY =
new IntProperty<ProgressBar>() {
@Override
public void setValue(ProgressBar progressBar, int value) {
progressBar.setProgress(value);
}
@Override
public Integer get(ProgressBar progressBar) {
return progressBar.getProgress();
}
};
/**
* Internal name of property. Like a intent bundles
*/
private static final String PROPNAME_PROGRESS = "ProgressTransition:progress";
@Override
public void captureStartValues(TransitionValues transitionValues) {
captureValues(transitionValues);
}
@Override
public void captureEndValues(TransitionValues transitionValues) {
captureValues(transitionValues);
}
private void captureValues(TransitionValues transitionValues) {
if (transitionValues.view instanceof ProgressBar) {
// save current progress in the values map
ProgressBar progressBar = ((ProgressBar) transitionValues.view);
transitionValues.values.put(PROPNAME_PROGRESS, progressBar.getProgress());
}
}
@Override
public Animator createAnimator(ViewGroup sceneRoot, TransitionValues startValues,
TransitionValues endValues) {
if (startValues != null && endValues != null && endValues.view instanceof ProgressBar) {
ProgressBar progressBar = (ProgressBar) endValues.view;
int start = (Integer) startValues.values.get(PROPNAME_PROGRESS);
int end = (Integer) endValues.values.get(PROPNAME_PROGRESS);
if (start != end) {
// first of all we need to apply the start value, because right now
// the view is have end value
progressBar.setProgress(start);
// create animator with our progressBar, property and end value
return ObjectAnimator.ofInt(progressBar, PROGRESS_PROPERTY, end);
}
}
return null;
}
用法
private void setProgress(int value) {
TransitionManager.beginDelayedTransition(mTransitionsContainer, new ProgressTransition());
value = Math.max(0, Math.min(100, value));
mProgressBar.setProgress(value);
}