原文地址
http://hencoder.com/ui-1-6/
1.translation移动
public class Sample01Translation extends RelativeLayout {
Button animateBt;
ImageView imageView;
int translationStateCount = SDK_INT > Build.VERSION_CODES.LOLLIPOP ? 6 : 4;
int translationState = 0;
public Sample01Translation(Context context) {
super(context);
}
public Sample01Translation(Context context, @Nullable AttributeSet attrs) {
super(context, attrs);
}
public Sample01Translation(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
}
@Override
protected void onAttachedToWindow() {
super.onAttachedToWindow();
animateBt = (Button) findViewById(R.id.animateBt);
imageView = (ImageView) findViewById(R.id.imageView);
if (SDK_INT > Build.VERSION_CODES.LOLLIPOP) {
// 给音乐图标加上合适的阴影
imageView.setOutlineProvider(new MusicOutlineProvider());
}
animateBt.setOnClickListener(new OnClickListener() {
@Override
public void onClick(final View v) {
switch (translationState) {
case 0:
imageView.animate().translationX(Utils.dpToPixel(100));
break;
case 1:
imageView.animate().translationX(0);
break;
case 2:
imageView.animate().translationY(Utils.dpToPixel(50));
break;
case 3:
imageView.animate().translationY(0);
break;
case 4:
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
imageView.animate().translationZ(Utils.dpToPixel(15));
}
break;
case 5:
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
imageView.animate().translationZ(0);
}
break;
}
translationState++;
if (translationState == translationStateCount) {
translationState = 0;
}
}
});
}
/**
* 为音乐图标设置三角形的 Outline。
*/
@RequiresApi(api = Build.VERSION_CODES.LOLLIPOP)
class MusicOutlineProvider extends ViewOutlineProvider {
Path path = new Path();
{
path.moveTo(0, dpToPixel(10));
path.lineTo(dpToPixel(7), dpToPixel(2));
path.lineTo(dpToPixel(116), dpToPixel(58));
path.lineTo(dpToPixel(116), dpToPixel(70));
path.lineTo(dpToPixel(7), dpToPixel(128));
path.lineTo(0, dpToPixel(120));
path.close();
}
@Override
public void getOutline(View view, Outline outline) {
outline.setConvexPath(path);
}
}
}
2.rotation
public class Sample02Rotation extends RelativeLayout {
Button animateBt;
ImageView imageView;
int state = 0;
public Sample02Rotation(Context context) {
super(context);
}
public Sample02Rotation(Context context, @Nullable AttributeSet attrs) {
super(context, attrs);
}
public Sample02Rotation(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
}
@Override
protected void onAttachedToWindow() {
super.onAttachedToWindow();
animateBt = (Button) findViewById(R.id.animateBt);
imageView = (ImageView) findViewById(R.id.imageView);
animateBt.setOnClickListener(new OnClickListener() {
@Override
public void onClick(final View v) {
switch (state) {
case 0:
imageView.animate().rotation(180);
break;
case 1:
imageView.animate().rotation(0);
break;
case 2:
imageView.animate().rotationX(180);
break;
case 3:
imageView.animate().rotationX(0);
break;
case 4:
imageView.animate().rotationY(180);
break;
case 5:
imageView.animate().rotationY(0);
break;
}
state++;
if (state == 6) {
state = 0;
}
}
});
}
}
3.scale 压缩
public class Sample03Scale extends RelativeLayout {
Button animateBt;
ImageView imageView;
int state = 0;
public Sample03Scale(Context context) {
super(context);
}
public Sample03Scale(Context context, @Nullable AttributeSet attrs) {
super(context, attrs);
}
public Sample03Scale(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
}
@Override
protected void onAttachedToWindow() {
super.onAttachedToWindow();
animateBt = (Button) findViewById(R.id.animateBt);
imageView = (ImageView) findViewById(R.id.imageView);
animateBt.setOnClickListener(new OnClickListener() {
@Override
public void onClick(final View v) {
switch (state) {
case 0:
imageView.animate().scaleX(1.5f);
break;
case 1:
imageView.animate().scaleX(1);
break;
case 2:
imageView.animate().scaleY(.5f);
break;
case 3:
imageView.animate().scaleY(1);
break;
}
state++;
if (state == 4) {
state = 0;
}
}
});
}
}
4.alpha 透明色
public class Sample04Alpha extends RelativeLayout {
Button animateBt;
ImageView imageView;
int state = 0;
public Sample04Alpha(Context context) {
super(context);
}
public Sample04Alpha(Context context, @Nullable AttributeSet attrs) {
super(context, attrs);
}
public Sample04Alpha(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
}
@Override
protected void onAttachedToWindow() {
super.onAttachedToWindow();
animateBt = (Button) findViewById(R.id.animateBt);
imageView = (ImageView) findViewById(R.id.imageView);
animateBt.setOnClickListener(new OnClickListener() {
@Override
public void onClick(final View v) {
switch (state) {
case 0:
imageView.animate().alpha(0);
break;
case 1:
imageView.animate().alpha(1);
break;
}
state++;
if (state == 2) {
state = 0;
}
}
});
}
}
5.ViewPropertyAnimator 多属性
public class Sample05MultiProperties extends ConstraintLayout {
Button animateBt;
ImageView imageView;
boolean animated;
public Sample05MultiProperties(Context context) {
super(context);
}
public Sample05MultiProperties(Context context, @Nullable AttributeSet attrs) {
super(context, attrs);
}
public Sample05MultiProperties(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
}
@Override
protected void onAttachedToWindow() {
super.onAttachedToWindow();
animateBt = (Button) findViewById(R.id.animateBt);
imageView = (ImageView) findViewById(R.id.imageView);
imageView.setScaleX(0);
imageView.setScaleY(0);
imageView.setAlpha(0f);
animateBt.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
if (!animated) {
imageView.animate()
.translationX(Utils.dpToPixel(200))
.rotation(360)
.scaleX(1)
.scaleY(1)
.alpha(1);
} else {
imageView.animate()
.translationX(0)
.rotation(0)
.scaleX(0)
.scaleY(0)
.alpha(0);
}
animated = !animated;
}
});
}
}
6.设置时间
public class Sample06Duration extends LinearLayout {
SeekBar durationSb;
TextView durationValueTv;
Button animateBt;
ImageView imageView;
int duration = 300;
int translationState = 0;
public Sample06Duration(Context context) {
super(context);
}
public Sample06Duration(Context context, @Nullable AttributeSet attrs) {
super(context, attrs);
}
public Sample06Duration(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
}
@Override
protected void onAttachedToWindow() {
super.onAttachedToWindow();
durationSb = (SeekBar) findViewById(R.id.durationSb);
durationValueTv = (TextView) findViewById(R.id.durationValueTv);
durationValueTv.setText(getContext().getString(R.string.ms_with_value, duration));
durationSb.setMax(10);
durationSb.setProgress(1);
durationSb.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
@Override
public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
duration = progress * 300;
durationValueTv.setText(getContext().getString(R.string.ms_with_value, duration));
}
@Override
public void onStartTrackingTouch(SeekBar seekBar) {
}
@Override
public void onStopTrackingTouch(SeekBar seekBar) {
}
});
animateBt = (Button) findViewById(R.id.animateBt);
imageView = (ImageView) findViewById(R.id.imageView);
animateBt.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
switch (translationState) {
case 0:
imageView.animate().translationX(Utils.dpToPixel(100)).setDuration(duration);
break;
case 1:
imageView.animate().translationX(0).setDuration(duration);
break;
}
if (translationState < 1) {
translationState++;
} else {
translationState = 0;
}
}
});
}
}
7.interpolator 速度设置器
public class Sample07Interpolator extends LinearLayout {
Spinner spinner;
Button animateBt;
ImageView imageView;
Interpolator[] interpolators = new Interpolator[13];
Path interpolatorPath;
public Sample07Interpolator(Context context) {
super(context);
}
public Sample07Interpolator(Context context, @Nullable AttributeSet attrs) {
super(context, attrs);
}
public Sample07Interpolator(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
}
{
interpolatorPath = new Path();
interpolatorPath.lineTo(0.25f, 0.25f);
interpolatorPath.moveTo(0.25f, 1.5f);
interpolatorPath.lineTo(1, 1);
interpolators[0] = new AccelerateDecelerateInterpolator();
interpolators[1] = new LinearInterpolator();
interpolators[2] = new AccelerateInterpolator();
interpolators[3] = new DecelerateInterpolator();
interpolators[4] = new AnticipateInterpolator();
interpolators[5] = new OvershootInterpolator();
interpolators[6] = new AnticipateOvershootInterpolator();
interpolators[7] = new BounceInterpolator();
interpolators[8] = new CycleInterpolator(0.5f);
interpolators[9] = PathInterpolatorCompat.create(interpolatorPath);
interpolators[10] = new FastOutLinearInInterpolator();
interpolators[11] = new FastOutSlowInInterpolator();
interpolators[12] = new LinearOutSlowInInterpolator();
}
@Override
protected void onAttachedToWindow() {
super.onAttachedToWindow();
spinner = (Spinner) findViewById(R.id.interpolatorSpinner);
animateBt = (Button) findViewById(R.id.animateBt);
imageView = (ImageView) findViewById(R.id.imageView);
animateBt.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
imageView.animate()
.translationX(Utils.dpToPixel(150))
.setDuration(600)
.setInterpolator(interpolators[spinner.getSelectedItemPosition()])
.withEndAction(new Runnable() {
@Override
public void run() {
imageView.postDelayed(new Runnable() {
@Override
public void run() {
imageView.setTranslationX(0);
}
}, 500);
}
});
}
});
}
}
感觉动画挺神奇的