Android ImageView Android ImageView的selector效果实例详解

软件发布|下载排行|最新软件

当前位置:首页IT学院IT技术

Android ImageView Android ImageView的selector效果实例详解

  2021-03-24 我要评论
想了解Android ImageView的selector效果实例详解的相关内容吗,在本文为您仔细讲解Android ImageView的相关知识和一些Code实例,欢迎阅读和指正,我们先划重点:Android,ImageView的selector,ImageView的selector效果实例,下面大家一起来学习吧。

Android ImageView的selector效果实例详解

在平时开发中如Button我们给它加上selector分别呈现pressed以及normal效果能给我们的用户体验上大大增色不少,可是我们当我们是用ImageView来”当作”一个一个”Button”的时候发现直接设置selector却不起作用,当然此时我们的应用就表现的暗淡了。那我们就只能找到方法来解决这种情况。

首先定义一个selector文件:

<selector xmlns:android="http://schemas.android.com/apk/res/android" >
  <item android:state_pressed="true">
    <shape android:shape="rectangle">
      <corners android:radius="5dp" />
      <solid android:color="#50000000"/>
    </shape>
  </item>

  <item >
    <shape android:shape="rectangle">
      <corners android:radius="5dp" />
      <solid android:color="#00000000"/>
    </shape>
  </item>

</selector>

第二步,给ImageView的src设置该selector。

<ImageView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_gravity="center_horizontal"
    android:contentDescription="@null"
    android:scaleType="centerCrop"
    android:src="@drawable/share_image_selector" />

然后我们的ImageVIew上要呈现的图片资源就是用

mImageView.setBackgroundResource(R.drawable.icon);

也就是说我们是给imageview设置backgroundResource,然后给src设置我们设置好的selector,在视觉上我们的selector是显示在ImageView的上方,当然我们点击ImageView的时候就是触发selector,这个时候就会有按下的效果了。

以上就是Android ImageView的自定义开发,对于Android很多东西都需要重写,不美观肯定不是好的APP,本站还有很多关于Android开发的文章,请大家参阅,谢谢大家对本站的支持!

Copyright 2022 版权所有 软件发布 访问手机版

声明:所有软件和文章来自软件开发商或者作者 如有异议 请与本站联系 联系我们