AndroidTV开发3实现ImageView的焦点效果

        AndroidTV开发中TextView、Button、Recyclerview实现焦点效果很简单,设置一个badkground就可以了,shape里面设置两种状态,获取焦点时的图片或者背景,没有焦点时的图片或背景,代码如下:

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:drawable="@drawable/shape_vip_focus_bg" android:state_selected="true"/>
    <item android:drawable="@drawable/shape_vip_focus_bg" android:state_focused="true" />
    <item android:drawable="@drawable/shape_vip_focus_bg" android:state_pressed="true" />
    <item android:drawable="@color/transparent"/>
</selector>

1.获取焦点时的效果:

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android" android:shape="rectangle">
    <stroke android:color="@color/colorPrimary" android:width="4dp"/>
</shape>

2.但是ImageView设置焦点效果时会出现一个问题,由于ImageView有一个src属性,所以在设置background会被遮挡,这时不是focus无效,也不是焦点效果无用,网上的一般的方法是让UI切一张带效果的图,本文这里想了一个折中的办法,用一个父布局设置一个背景,然后把图片放到这个父布局中,图片的大小根据父布局来计算或者固定写死都行,具体看需求,代码如下:

3.完整布局代码如下:

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity">

    <TextView
        android:id="@+id/textView"
        android:layout_width="200dp"
        android:layout_height="40dp"
        android:background="@drawable/shape_vip_select_bg"
        android:focusable="true"
        android:focusableInTouchMode="true"
        android:gravity="center"
        android:text="文本"
        android:textColor="@color/colorPrimary"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintLeft_toRightOf="parent"
        app:layout_constraintRight_toLeftOf="parent"/>

    <Button
        android:id="@+id/button"
        android:layout_width="200dp"
        android:layout_height="40dp"
        android:layout_marginTop="20dp"
        android:background="@drawable/shape_vip_select_bg"
        android:focusable="true"
        android:focusableInTouchMode="true"
        android:gravity="center"
        android:text="按钮"
        android:textColor="@color/colorPrimary"
        app:layout_constraintLeft_toRightOf="parent"
        app:layout_constraintRight_toLeftOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/textView" />

    <RelativeLayout
        android:layout_width="100dp"
        android:layout_height="100dp"
        android:background="@drawable/shape_vip_select_bg"
        android:focusable="true"
        android:focusableInTouchMode="true"
        android:layout_marginTop="20dp"
        app:layout_constraintTop_toBottomOf="@+id/button"
        app:layout_constraintLeft_toRightOf="parent"
        app:layout_constraintRight_toLeftOf="parent">

        <ImageView
            android:layout_width="90dp"
            android:layout_height="90dp"
            android:scaleType="fitXY"
            android:src="@mipmap/index_play_3"
            android:layout_centerInParent="true"/>
    </RelativeLayout>
</androidx.constraintlayout.widget.ConstraintLayout>

4.实现的效果如下:

5.代码很简单,但是这个过程很痛苦,找了很多资料和第三方库,由于时间问题和局限性,所以没有采取第三方,ui也比较忙,想了这么一个笨办法,暂时先这样吧!下一篇讲解实现ImageView待阴影特性的焦点效果,也是用shape实现的.如果小伙伴们有更好地方法可以给我留言,我会积极采纳.

最后例子完整地址如下:

smile drily/ImagViewFocus

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值