Android TV自定义处理焦点控件

尹超
2023-12-01

Android stuido有android tv项目模板,有一套规则,如果想开发TV版项目可以按照那个模板来,不过那个模板定制性太强。tv版app和其他app有一点不同,就是用遥控器进行操作,所以要想按照自己需求来开发,就要处理好焦点事件。
其实也不复杂,只要继承现有的控件,在onFocusChanged事件中,对自己的需求进行定制就可以了。提供几个例子:
1.显示图片的控件,默认加载默认图片,当获得焦点以后,切换选中图片,失去焦点,切换默认图片

public class FocusImageView extends ImageButton
{
    private int defaultImageResources = -1;
    private int focusImageResources = -1;

    public FocusImageView(Context context)
    {
        super(context);
    }

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

    public FocusImageView(Context context, AttributeSet attrs, int defStyle)
    {
        super(context, attrs, defStyle);
        setAttributeSet(attrs);
    }

    @Override
    protected void onFocusChanged(boolean gainFocus, int direction, Rect previouslyFocusedRect)
    {
        super.onFocusChanged(gainFocus, direction, previouslyFocusedRect);
        if (gainFocus)
        {
            if (focusImageResources != -1)
            {
                setBackgroundResource(focusImageResources);
            }
        }
        else
        {
            if (defaultImageResources != -1)
            {
                setBackgroundResource(defaultImageResources);
            }
        }
    }

    private void setAttributeSet(AttributeSet attrs)
    {
        if (attrs != null)
        {
            TypedArray typeArray = getContext().obtainStyledAttributes(attrs, R.styleable.FocusImageView);
            defaultImageResources = typeArray.getResourceId(R.styleable.FocusImageView_defaultImageResources, -1);
            focusImageResources = typeArray.getResourceId(R.styleable.FocusImageView_focusImageResources, -1);
            if (defaultImageResources != -1)
            {
                setBackgroundResource(defaultImageResources);
            }
        }
    }
}

attrs.xml

<resources>

    <declare-styleable name="FocusImageView">
        <attr name="defaultImageResources" format="integer" />
        <attr name="focusImageResources" format="integer" />
    </declare-styleable>

</resources>

布局文件引用:

<项目的包名.widget.focusview.FocusImageView
                    android:id="@+id/iv_wulianwang"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    app:defaultImageResources="@mipmap/wulianwangdefault"
                    app:focusImageResources="@mipmap/wulianwangselected">
</项目的包名.widget.focusview.FocusImageView>

当然,如果只是在获取焦点和失去焦点,切换图片,可以定义selector,在drawable下,定义一个selector

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:drawable="@mipmap/bingliselected" android:state_focused="true" />
    <item android:drawable="@mipmap/binglidefault" />
</selector>

然后在布局中调用

<LinearLayout
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:layout_margin="6dp"
                    android:focusable="true"
                    android:background="@drawable/focus_selector">

2.一个布局,内部控件获得焦点,可以放大,失去焦点,内部控件可以缩小:

public class FocusRelativeLayout extends RelativeLayout {

    private Animation scaleSmallAnimation;
    private Animation scaleBigAnimation;

    public FocusRelativeLayout(Context context) {
        super(context);
    }

    public FocusRelativeLayout(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    public FocusRelativeLayout(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
    }

    @Override
    protected void onFocusChanged(boolean gainFocus, int direction, Rect previouslyFocusedRect) {
        super.onFocusChanged(gainFocus, direction, previouslyFocusedRect);
        if (gainFocus) {
            zoomOut();
        } else {
            zoomIn();
        }
    }

    private void zoomIn() {
        if (scaleSmallAnimation == null) {
            scaleSmallAnimation = AnimationUtils.loadAnimation(getContext(), R.anim.anim_scale_small);
        }
        startAnimation(scaleSmallAnimation);
    }

    private void zoomOut() {
        if (scaleBigAnimation == null) {
            scaleBigAnimation = AnimationUtils.loadAnimation(getContext(), R.anim.anim_scale_big);
        }
        startAnimation(scaleBigAnimation);
    }
}

anim_scale_big.xml

<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android"
    android:fillAfter="true"
    android:shareInterpolator="false">
    <scale
        android:duration="350"
        android:fromXScale="1.0"
        android:fromYScale="1.0"
        android:interpolator="@android:anim/accelerate_decelerate_interpolator"
        android:pivotX="50.0%"
        android:pivotY="50.0%"
        android:repeatCount="0"
        android:toXScale="1.08"
        android:toYScale="1.08" />
</set>

anim_scale_small.xml

<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android"
    android:fillAfter="true"
    android:shareInterpolator="false">
    <scale
        android:duration="350"
        android:fromXScale="1.08"
        android:fromYScale="1.08"
        android:interpolator="@android:anim/accelerate_decelerate_interpolator"
        android:pivotX="50.0%"
        android:pivotY="50.0%"
        android:repeatCount="0"
        android:toXScale="1.0"
        android:toYScale="1.0" />
</set>

布局文件引用:

 <包名.view.FocusRelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
                                                     android:layout_width="wrap_content"
                                                     android:layout_height="wrap_content"
                                                     android:background="#424242"
                                                     android:clipChildren="true"
                                                     android:focusable="true">

        <ImageView
                android:layout_width="100dp"
                android:layout_height="100dp"
                android:scaleType="fitXY"
                android:src="@drawable/pic2"
                android:layout_margin="5dp"/>
    </包名.view.FocusRelativeLayout>

3.进入界面,让某个控件获取焦点
根据需求,可能对进入界面,首先选中哪个控件有需求,可以在布局文件里边,使用<requestFocus/>属性

  <包名.widget.focusview.FocusImageView
                    android:id="@+id/iv_bingli"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    app:defaultImageResources="@mipmap/binglidefault"
                    app:focusImageResources="@mipmap/bingliselected"
            >
                <requestFocus></requestFocus>
            </包名.widget.focusview.FocusImageView>
 类似资料: