Android TV 맞춤형 포커스 컨트롤

안드로이드 스튜디오에는 안드로이드 tv 프로젝트 템플릿이 있고 규칙이 있습니다. TV 버전 프로젝트를 개발하려면 그 템플릿에 따라 할 수 있지만 그 템플릿은 맞춤형 제작이 너무 강합니다.tv판 앱은 다른 앱과 조금 다르다. 리모컨으로 조작하기 때문에 자신의 수요에 따라 개발하려면 초점 사건을 잘 처리해야 한다.사실 복잡하지 않아요. 기존의 컨트롤을 계승하고 onFocusChanged 이벤트에서 자신의 수요를 맞춤형으로 설정하면 돼요.몇 가지 예를 제공하다.그림을 표시하는 컨트롤, 기본 그림을 불러옵니다. 초점을 얻은 후 선택한 그림을 전환합니다. 초점을 잃고 기본 그림을 전환합니다.
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를 정의할 수 있습니다

<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

<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

<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">

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

3. 인터페이스에 들어가서 어떤 컨트롤러가 수요에 따라 초점을 잡게 하고 인터페이스에 들어갈 수 있다. 먼저 어떤 컨트롤러를 선택하면 레이아웃 파일에서 사용할 수 있다
  .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"
            >
                
              .widget.focusview.FocusImageView>

좋은 웹페이지 즐겨찾기