最近在做AndroidTV端的开发,TV端和手机端最大的不同就是交互方式,尤其是焦点的获取动画。

之前为了焦点的放大动画又是导库又是自定义的,现在几行代码就可以搞定了:

ViewCompat.animate(v)
        .scaleX(1.10f)
        .scaleY(1.10f)
        .translationZ(1)
        .start();

比如,如果想在RecyclerView中让获取焦点的item有放大动画。可以在onCreateViewHolder方法中的焦点改变监听中加入该代码:

   FrameLayout layout= createView.findViewById(R.id.id_layout);

 layout.setOnFocusChangeListener(new View.
                OnFocusChangeListener() {
            @Override
            public void onFocusChange(View v, boolean hasFocus) {
                if (hasFocus) {
                    // 此处为得到焦点时的处理内容
                    ImageView id_iv_unselect = v.findViewById(R.id.id_iv_unselect);
                    id_iv_unselect.setBackgroundResource(R.drawable.ic_room_selectyuan);
                    ViewCompat.animate(v)
                            .scaleX(1.10f)
                            .scaleY(1.10f)
                            .translationZ(1)
                            .start();

                } else {
                    // 此处为失去焦点时的处理内容
                   ImageView id_iv_unselect = v.findViewById(R.id.id_iv_unselect);
                   id_iv_unselect.setBackgroundResource(R.drawable.ic_room_unselectyuan);
                    ViewCompat.animate(v)
                            .scaleX(1)
                            .scaleY(1)
                            .translationZ(1)
                            .start();
                }
            }
        });

注意:translationZ(即Z轴的动画效果)是在API21(android5.0)之后才有的。

Logo

智屏生态联盟致力于大屏生态发展,利用大屏快应用技术降低开发者开发、发布大屏应用门槛

更多推荐