Android TV自定义处理焦点控件

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>
  • 0
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
Android自定义View是指基于Android原生控件的一种扩展,可以根据自己的需求和设计规范来创建更加个性化和独特的控件。而歌词控件是一种针对音乐播放器或者视频播放器等应用场景中的需求,用于显示音乐或者视频的歌词的控件Android自定义View歌词控件的实现思路如下: 1. 首先需要自定义一个View,并继承自View或者其子类,如TextView。 2. 在自定义的View中重写onDraw方法,在其中实现绘制歌词的逻辑。 3. 在onDraw方法中,使用Canvas对象进行绘制,可以使用drawText方法绘制歌词文本,也可以使用drawBitmap方法绘制图片背景等。 4. 可以通过自定义属性,如字体大小、字体颜色、歌词滚动速度等,来对歌词控件进行配置。 5. 如果需要实现歌词的滚动效果,可以使用ValueAnimator或者Scroller来实现歌词的平滑滚动。 6. 如果需要实现点击歌词跳转播放进度的功能,可以通过添加点击事件监听器,在触摸事件中判断点击位置对应的歌词行,并根据歌词的时间戳跳转到指定的播放进度。 总结来说,Android自定义View歌词控件的实现需要重写onDraw方法进行绘制,可以通过Canvas对象进行绘制文本或者图像,通过自定义属性进行配置,使用动画或者滚动实现歌词的平滑滚动,通过监听触摸事件实现点击歌词跳转播放进度的功能。通过以上步骤,我们可以创建一个个性化的歌词控件,满足不同应用场景的需求。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值