ImageView控件背景选择器

本文详细探讨了Android中ImageView如何使用bg.xml作为背景选择器,涵盖了选择器的XML结构、不同状态的应用以及在ImageView中的实际操作示例,帮助开发者更好地实现UI交互效果。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

bg.xml

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
    android:shape="oval">//设置背景的形状,此处为圆。
    <corners android:radius="10dp"/>
    //<solid android:color="#888"/>//设置图形填充色
    <stroke android:color="#000" android:width="2dp"/>//stroke设置边框的颜色,宽度
    <gradient android:type="linear"//设置背景渐变色
        android:startColor="#0f0"
        android:endColor="#00f"
        android:centerColor="#fff"
        android:angle="135"/>
    <size android:width="100dp" android:height="100dp"/>//设置圆的半径
    <padding android:bottom="10dp"//设置背景的内边距
        android:top="10dp"
        android:left="10dp"
        android:right="10dp"/>
</shape>
bg_1.xml

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
    android:shape="oval">
    <corners android:radius="10dp"/>
    <solid android:color="#888"/>
    <stroke android:color="#000" android:width="2dp"/>
    <gradient android:type="linear"
        android:startColor="#0f0"
        android:endColor="#f00"
        android:centerColor="#fff"
        android:angle="135"/>
    <size android:width="100dp" android:height="100dp"/>
    <padding android:bottom="10dp"
        android:top="10dp"
        android:left="10dp"
        android:right="10dp"/>
</shape>
sel.xml:设置背景选择器

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:state_pressed="true" android:drawable="@drawable/bg_1"/>//将bg_1.xml文件放入选择器,其中state_pressed表示被按下为true加载背景文件
    <item android:drawable="@drawable/bg"/>//没被按下是bg.xml背景,item不能互换。
</selector>
activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context="com.example.wwj_fly.image.MainActivity">

   <ImageView
       android:layout_width="200dp"
       android:layout_height="200dp"
       android:layout_gravity="center"
       android:tint="#88fff000"
       android:src="@mipmap/ic_launcher"//
       android:scaleType="center"//缩放类型
       android:adjustViewBounds="true"//当imageview控件的宽度或高度一个是定值,一个是不定值。用它实现等比例缩放图片
       android:tintMode="multiply"
       style="?attr/buttonStyle"//将imageview设置为按钮类型,并设置focusable为true
       android:focusable="true"
       android:clickable="true"//可以被点击的 
       android:background="@drawable/sel"//将选择器sel.xml设置为该控件的背景
       />
</FrameLayout>





评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值