TV首页Menu指定父布局选中子View后绘制在最上层

1,父布局内添加view的顺序是最后添加的位于最上层,效果为如下:
原始效果
现在要实现选中的view位于最上层
最终效果
自定义HomeMenuView继承LinearLayout,重写getChildDrawingOrder方法,指定view绘制的顺序:

   @Override
    protected int getChildDrawingOrder(int childCount, int i) {
        View focusedChild = this.getFocusedChild();
        int focusViewIndex = this.indexOfChild(focusedChild)
        if (focusViewIndex < 0) {
            return i;
        }
        if (focusViewIndex == i) {
            return childCount - 1;
        } else if (i == childCount - 1) {
            return focusViewIndex;
        } else {
            return i;
        }
//        return super.getChildDrawingOrder(childCount, i);
    }

并在自定义view初始化时设置

this.setChildrenDrawingOrderEnabled(true);//启用子视图排序功能

完整代码:

public class HomeMenuView extends LinearLayout {
  
    private String TAG = "HomeMenuView";
    private List<HomeMenuBeen> menuBeens;

    public HomeMenuView(Context context) {
        this(context, null);
        initView(context);
    }

    public HomeMenuView(Context context, AttributeSet attrs) {
        this(context, attrs, 0);
        initView(context);
    }

    public HomeMenuView(Context context, AttributeSet attrs, int defaultValue) {
        super(context, attrs, defaultValue);
    }

    private void initView(Context context) {
        this.setOrientation(HORIZONTAL);
        this.setGravity(Gravity.CENTER);
        this.setChildrenDrawingOrderEnabled(true);
        initMenus();
        if (menuBeens == null) return;

        for (int i = 0; i < menuBeens.size(); i++) {
            View itemView = LayoutInflater.from(context).inflate(R.layout.menu_home_item, null);
            LinearLayout ll_smallItem = (LinearLayout) itemView.findViewById(R.id.ll_smallItem);
            LinearLayout ll_bigItem = (LinearLayout) itemView.findViewById(R.id.ll_bigItem);
            LinearLayout ll_bigTv = (LinearLayout) itemView.findViewById(R.id.ll_bigTv);
            ImageView img_small = (ImageView) itemView.findViewById(R.id.img_small);
            ImageView img_big = (ImageView) itemView.findViewById(R.id.img_big);
            TextView tv_smallTv = (TextView) itemView.findViewById(R.id.tv_smallTv);
            TextView tv_bigTv = (TextView) itemView.findViewById(R.id.tv_bigTv);
            TextView tv_bigTvNote = (TextView) itemView.findViewById(R.id.tv_bigTvNote);
            img_small.setImageResource(menuBeens.get(i).getImgRes());
            img_big.setImageResource(menuBeens.get(i).getImgRes());
            tv_smallTv.setText(menuBeens.get(i).getMenuStr());
            tv_bigTv.setText(menuBeens.get(i).getMenuStr());
            tv_bigTvNote.setText(menuBeens.get(i).getNote());
            LinearLayout.LayoutParams layoutParams = new LayoutParams((int) getResources().getDimension(R.dimen.dp_1080p_180px),
                    (int) getResources().getDimension(R.dimen.dp_1080p_180px));
            itemView.setOnFocusChangeListener(new OnFocusChangeListener() {
                @Override
                public void onFocusChange(View v, boolean hasFocus) {
                    itemAnmi(v, hasFocus);
                }
            });
            addView(itemView, layoutParams);
        }
    }


    @Override
    protected int getChildDrawingOrder(int childCount, int i) {
        View focusedChild = this.getFocusedChild();
        int focusViewIndex = this.indexOfChild(focusedChild);
        logUtil.d_2("778899", "focusViewIndex=" + focusViewIndex);
        if (focusViewIndex < 0) {
            return i;
        }
        if (focusViewIndex == i) {
            return childCount - 1;
        } else if (i == childCount - 1) {
            return focusViewIndex;
        } else {
            return i;
        }
//        return super.getChildDrawingOrder(childCount, i);
    }

    private AnimatorSet bigAnimatorSet;
    private AnimatorSet smallAnimatorSet;
    private LayoutParams layoutParams;

    private void itemAnmi(final View v, boolean hasFocus) {//动画等对象可做复用等优化

        //做动画内容会扭曲
        invalidate();//必须得先调用,才会执行getChildDrawingOrder
        ObjectAnimator bigX = ObjectAnimator.ofFloat(v, "scaleX", 1f,  1.3333f);
        ObjectAnimator bigY = ObjectAnimator.ofFloat(v, "scaleY", 1f, 1.3333f);
        ObjectAnimator smallX = ObjectAnimator.ofFloat(v, "scaleX", 1.3333f, 1);
        ObjectAnimator smallY = ObjectAnimator.ofFloat(v, "scaleY", 1.3333f, 1);
        if (hasFocus) {
            bigAnimatorSet = new AnimatorSet();
            bigAnimatorSet.setDuration(500);
            bigAnimatorSet.playTogether(bigX, bigY);
            bigAnimatorSet.start();

            bigAnimatorSet.addListener(new Animator.AnimatorListener() {
                @Override
                public void onAnimationStart(Animator animation) {
                }

                @Override
                public void onAnimationEnd(Animator animation) {
//                    v.findViewById(R.id.ll_bigItem).setVisibility(VISIBLE);
//                    v.findViewById(R.id.ll_smallItem).setVisibility(GONE);
                }

                @Override
                public void onAnimationCancel(Animator animation) {
                }

                @Override
                public void onAnimationRepeat(Animator animation) {
                }
            });
        } else {
            smallAnimatorSet = new AnimatorSet();
            smallAnimatorSet.setDuration(500);
            smallAnimatorSet.playTogether(smallX, smallY);
            smallAnimatorSet.start();
            smallAnimatorSet.addListener(new Animator.AnimatorListener() {
                @Override
                public void onAnimationStart(Animator animation) { }

                @Override
                public void onAnimationEnd(Animator animation) {
//                    v.findViewById(R.id.ll_bigItem).setVisibility(GONE);
//                    v.findViewById(R.id.ll_smallItem).setVisibility(VISIBLE);
                }

                @Override
                public void onAnimationCancel(Animator animation) {   }

                @Override
                public void onAnimationRepeat(Animator animation) { }
            });
        }

//另一种实现方法,可以实现正方形变化成长方形,不过由于不断的重绘,导致其他的view有闪烁
//        Menu menuSmall = new Menu(getResources().getDimension(R.dimen.dp_1080p_180px), getResources().getDimension(R.dimen.dp_1080p_180px), 0);
//        Menu menuBig = new Menu(getResources().getDimension(R.dimen.dp_1080p_400px), getResources().getDimension(R.dimen.dp_1080p_240px), 1);
//        if (hasFocus) {//放大
            v.findViewById(R.id.ll_bigItem).setVisibility(VISIBLE);
            v.findViewById(R.id.ll_smallItem).setVisibility(GONE);
//            ValueAnimator anim = ValueAnimator.ofObject(new ScallEvaluator(), menuSmall, menuBig);
//            anim.setDuration(300);
//            anim.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
//                @Override
//                public void onAnimationUpdate(ValueAnimator animation) {
//                    Menu currentMenu = (Menu) animation.getAnimatedValue();
//                    // 获得改变后的值
                    logUtil.d_2(TAG, "w=" + currentMenu.getWith() + "------h=" + currentMenu.getHight());
//                    logUtil.d_2(TAG, "big_alph=" + currentMenu.getAlph());
//                    if (v.getLayoutParams() == null) {
//                        layoutParams = new LayoutParams((int) currentMenu.getWith(), (int) currentMenu.getHight());
//                    } else {
//                        layoutParams = (LayoutParams) v.getLayoutParams();
//                        layoutParams.width = (int) currentMenu.getWith();
//                        layoutParams.height = (int) currentMenu.getHight();
//                    }
//                    v.findViewById(R.id.ll_bigItem).setAlpha(currentMenu.getAlph());
//                    v.findViewById(R.id.ll_smallItem).setAlpha(1 - currentMenu.getAlph());
//                    v.setLayoutParams(layoutParams);
//                }
//            });
//            anim.start();
//
//        } else {
            v.findViewById(R.id.ll_bigItem).setVisibility(GONE);
            v.findViewById(R.id.ll_smallItem).setVisibility(VISIBLE);
//            ValueAnimator anim = ValueAnimator.ofObject(new ScallEvaluator(), menuBig, menuSmall);
//            anim.setDuration(300);
//            anim.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
//                @Override
//                public void onAnimationUpdate(ValueAnimator animation) {
//                    Menu currentMenu = (Menu) animation.getAnimatedValue();
//                    // 获得改变后的值
                    logUtil.d_2(TAG,"w="+currentMenu.getWith()+"------h="+currentMenu.getHight());
//                    logUtil.d_2(TAG, "small_alph=" + currentMenu.getAlph());
//                    if (v.getLayoutParams() == null) {
//                        layoutParams = new LayoutParams((int) currentMenu.getWith(), (int) currentMenu.getHight());
//                    } else {
//                        layoutParams = (LayoutParams) v.getLayoutParams();
//                        layoutParams.width = (int) currentMenu.getWith();
//                        layoutParams.height = (int) currentMenu.getHight();
//                    }
//                    v.findViewById(R.id.ll_bigItem).setAlpha(currentMenu.getAlph());
//                    v.findViewById(R.id.ll_smallItem).setAlpha(1 - currentMenu.getAlph());
//                    v.setLayoutParams(layoutParams);
//                }
//            });
//            anim.start();
//        }
    }


    private void initMenus() {
        menuBeens = new ArrayList<>();
        HomeMenuBeen albumMenu = new HomeMenuBeen();
        albumMenu.setImgRes(R.mipmap.home_menu_album);
        albumMenu.setMenuStr("家庭相册");
        albumMenu.setNote("特别时刻,用心纪念");
        menuBeens.add(albumMenu);

        HomeMenuBeen avcallMenu = new HomeMenuBeen();
        avcallMenu.setImgRes(R.mipmap.home_menu_album);
        avcallMenu.setMenuStr("家庭相册");
        avcallMenu.setNote("特别时刻,用心纪念");
        menuBeens.add(avcallMenu);

        HomeMenuBeen mallMenu = new HomeMenuBeen();
        mallMenu.setImgRes(R.mipmap.home_menu_album);
        mallMenu.setMenuStr("家庭相册");
        mallMenu.setNote("特别时刻,用心纪念");
        menuBeens.add(mallMenu);

        HomeMenuBeen familyMenu = new HomeMenuBeen();
        familyMenu.setImgRes(R.mipmap.home_menu_album);
        familyMenu.setMenuStr("家庭相册");
        familyMenu.setNote("特别时刻,用心纪念");
        menuBeens.add(familyMenu);

        HomeMenuBeen aboutUs = new HomeMenuBeen();
        aboutUs.setImgRes(R.mipmap.home_menu_album);
        aboutUs.setMenuStr("家庭相册");
        aboutUs.setNote("特别时刻,用心纪念");
        menuBeens.add(aboutUs);
    }
}

XML:R.layout.menu_home_item(这里做两种布局的切换,实际情况自行考虑)

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:focusable="true"
    android:clickable="true"
android:layout_width="@dimen/dp_1080p_180px"
android:layout_height="@dimen/dp_1080p_180px">

<LinearLayout
    android:id="@+id/ll_bigItem"
    android:layout_centerInParent="true"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@mipmap/home_menubg_album"
    android:gravity="center"
    android:orientation="horizontal"
    android:visibility="visible">

    <ImageView
        android:id="@+id/img_big"
        android:layout_width="@dimen/dp_72"
        android:layout_height="@dimen/dp_1080p_62px"
        android:layout_marginRight="@dimen/dp_1080p_20px"
        android:src="@mipmap/home_menu_album" />

    <LinearLayout
        android:id="@+id/ll_bigTv"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:orientation="vertical">

        <liyueyun.familytv.tv.ui.widget.TextViewNoPad
            android:id="@+id/tv_bigTv"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="家庭相册"
            android:textSize="@dimen/sp_1080p_50px" />

        <TextView
            android:id="@+id/tv_bigTvNote"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="特别时刻,用心纪念"
            android:textSize="@dimen/sp_1080p_20px" />
    </LinearLayout>
</LinearLayout>

<LinearLayout
    android:id="@+id/ll_smallItem"
    android:layout_centerInParent="true"
    android:background="@mipmap/home_menubg_album"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:gravity="center"
    android:orientation="vertical"
    android:visibility="visible">

    <ImageView
        android:id="@+id/img_small"
        android:layout_width="@dimen/dp_1080p_62px"
        android:layout_height="@dimen/dp_1080p_52px"
        android:src="@mipmap/home_menu_album" />

    <TextView
        android:id="@+id/tv_smallTv"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="家庭相册"
        android:textSize="@dimen/sp_1080p_20px" />

    </LinearLayout>
</RelativeLayout>
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值