弧形菜单2(动画渐入)


前言前面文章<弧形菜单>加入动画渐入


效果:



开发环境:AndroidStudio2.2.1+gradle-2.14.1


涉及知识:1.自定义控件,2.事件分发等


部分代码:

Activity:

public class HomepageActivity extends AppCompatActivity {

    private HomePageMenuLayout homePageMenuLayout;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.homepage_layout);
        initLayout();
    }

    private void initLayout() {
        homePageMenuLayout = (HomePageMenuLayout) findViewById(R.id.homepage_layout);
        //加载菜单列表
        homePageMenuLayout.setMenuItemIconsAndTexts(Constants.MENUALL);
        //才点动画初始
        new SwitchAnimationUtil().startAnimation(homePageMenuLayout, SwitchAnimationUtil.AnimationType.ROTATE);
        //事件监听
        homePageMenuLayout.setOnMenuItemClickListener(new HomePageMenuLayout.OnMenuItemClickListener() {
            @Override
            public void itemClick(View view, int pos) {
                Toast.makeText(HomepageActivity.this, Constants.MENUALL[pos], Toast.LENGTH_SHORT).show();
            }
        });
    }

}

动画类:

public class SwitchAnimationUtil {
	private int mOrderIndex = 0;
	private int mDelay = 80;
	private int mDuration = 500;

	public SwitchAnimationUtil() {}

	public void startAnimation(View root, AnimationType type) {
		bindAnimation(root, 0, type);
	}

	private void bindAnimation(View view, int depth, AnimationType type) {

		if (view instanceof ViewGroup) {
		ViewGroup group = (ViewGroup) view;

		for (int i = 0; i < group.getChildCount(); i++) {
			bindAnimation(group.getChildAt(i), depth + 1, type);
		}

	} else {
		runAnimation(view, mDelay * mOrderIndex, type);
		mOrderIndex++;
	}
}
	private void runAnimation(View view, long delay, AnimationType type) {
		switch (type) {
		case ROTATE:
			runRotateAnimation(view, delay);
			break;
		case ALPHA:
			runAlphaAnimation(view, delay);
			break;
		default:
			break;
		}
	}

	private void runAlphaAnimation(View view, long delay) {
		view.setAlpha(0);
		ObjectAnimator objectAnimator = ObjectAnimator.ofFloat(view, "alpha", 0f, 1f);
		objectAnimator.setStartDelay(delay);
		objectAnimator.setDuration(mDuration);
		objectAnimator.setInterpolator(new LinearInterpolator());
		objectAnimator.start();
	}

	private void runRotateAnimation(View view, long delay) {
		view.setAlpha(0);
		AnimatorSet set = new AnimatorSet();
		ObjectAnimator objectAnimator = ObjectAnimator.ofFloat(view, "rotation", 0f, 0f);
		ObjectAnimator objectAnimator2 = ObjectAnimator.ofFloat(view, "scaleX", 0f, 1f);
		ObjectAnimator objectAnimator3 = ObjectAnimator.ofFloat(view, "scaleY", 0f, 1f);
		ObjectAnimator objectAnimator4 = ObjectAnimator.ofFloat(view, "alpha", 0f, 1f);

		objectAnimator2.setInterpolator(new AccelerateInterpolator(1.0f));
		objectAnimator3.setInterpolator(new AccelerateInterpolator(1.0f));

		set.setDuration(mDuration);
		set.playTogether(objectAnimator,objectAnimator2, objectAnimator3, objectAnimator4);
		set.setStartDelay(delay);
		set.start();
	}
	public enum AnimationType {
		ALPHA, ROTATE
	}
}


源码下载...



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值