滑动控件用的是NestedScrollView,可以监听滚动的过程。滚动的这个过程中,进度percent从0到1,完成整个效果。
- 透明度从0-1
- 宽高:从大图片到小图片变化,用的是view.setScaleX,view.setScaleY方法
- 位置:从小图片到小图片变化,用的是view.setTranslationX方法
- 进度完成旋转动画
布局:
复制代码
代码:
public class AnimIconActivity extends AppCompatActivity { private static final String TAG = "AnimIconActivity"; private float diatance; private float translationY; private float translationX; private int[] bigLocation; private FrameLayout fl; private float widthBig; private float heightBig; private float widthSmall; private float heightSmall; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_anim_icon); final ImageView ivSmall = (ImageView) findViewById(R.id.iv_small);//大图片 final ImageView ivBig = (ImageView) findViewById(R.id.iv_big);//小图片 final NestedScrollView nestScrollView = (NestedScrollView) findViewById(R.id.nest_scroll_view); //获取大图片和小图片的位置信息和宽高 ivBig.post(new Runnable() { @Override public void run() { widthBig = ivBig.getWidth(); heightBig = ivBig.getHeight(); widthSmall = ivSmall.getWidth(); heightSmall = ivSmall.getHeight(); int[] smallLocaion = new int[2]; ivSmall.getLocationInWindow(smallLocaion); bigLocation = new int[2]; ivBig.getLocationInWindow(bigLocation); diatance = Math.abs(smallLocaion[1]- bigLocation[1]);//y方向需滚动的距离 translationX = smallLocaion[0] - bigLocation[0]; translationY = smallLocaion[1] - bigLocation[1]; } }); ivSmall.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { ivBig.setTranslationX(translationX); ivBig.setTranslationY(translationY); } }); ivBig.setPivotX(0); ivBig.setPivotY(0); //头像栏 fl = (FrameLayout) findViewById(R.id.fl_bar); fl.setAlpha(0); nestScrollView.setOnScrollChangeListener(new NestedScrollView.OnScrollChangeListener() { @Override public void onScrollChange(NestedScrollView v, int scrollX, int scrollY, int oldScrollX, int oldScrollY) { View child = nestScrollView.getChildAt(0); int height = child.getHeight(); int height1 = nestScrollView.getHeight(); int top = Math.abs(nestScrollView.getScrollY()); if (height - height1 >= diatance) { if (top <= diatance) { Log.i(TAG, "onScrollChange: " + scrollY); //滚动的进度 float percent = top / diatance; //计算设置缩放比例 ivBig.setScaleX(percent * (widthSmall / widthBig - 1) + 1); ivBig.setScaleY(percent * (heightSmall / heightBig - 1) + 1); //设置透明度 fl.setAlpha(percent); ivBig.setAlpha(1-percent+0.2f); //x方向朝小图片平移,y方向不用管NestedScrollView会带着它飞 float x = (translationX) * percent; ivBig.setTranslationX(x); //滚动未完成隐藏小图片 ivSmall.setVisibility(View.INVISIBLE); }else {//滚动已完成 if(ivSmall.getVisibility()!=View.VISIBLE){ fl.setAlpha(1); ivSmall.setVisibility(View.VISIBLE); ObjectAnimator rotation = ObjectAnimator.ofFloat(ivSmall, "rotation", -60, 60, 0) .setDuration(400); rotation.setInterpolator(new OvershootInterpolator()); rotation.start(); } } } } }); }}复制代码