(android:点击换图系列一)通过ImageView点击更换图片

本文通过一个简单的Android应用实例,演示如何使用XML和MainActivity.java实现点击ImageView切换灯泡亮灭状态。主要涉及XML布局文件设置ImageView,以及在MainActivity中监听点击事件,根据布尔值判断切换不同图片资源,达到灯泡亮灭的效果。
摘要由CSDN通过智能技术生成

大家好,我是豹豹哥,友爱互融,共同进步😘🤞

项目背景:

要完成老师提出的一个需求(对象是一个灯,要实现按一下切换一下”亮灭“状态,分别用两张图片表示)
本文以ImageView为例解决

解决方案:

XML部分文字描述:

对于main.xml(主UI)文件来说,里面的ImageView控件肯定是少不了的,而且只要一个ImageView就够了

XML代码:

<RelativeLayout 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=".MainActivity" >

    <ImageView
        android:id="@+id/ima_lamp"
        android:layout_width="110dp"
        android:layout_height="180dp"
        android:layout_centerHorizontal="true"
        android:layout_centerVertical="true"
        android:src="@drawable/lamp" />

</RelativeLayout>

MainActivity部分文字描述:

首先要求中说了要变换亮灭样式,所以我们肯定要定义一个Boolean值来表示亮灭状态,然后就是重写onClick()方法了,用一个if判断,如果传入的Boolean值是真,就调用表示亮的图片:反之就调用表示灭的图片

MainActivity.java代码:

(尽早适应英文注释,有益无害,博主英文水准欠佳,请将就一下)

package com.example.admin.bilibili;

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.Window;
import android.widget.ImageView;


public class MainActivity extends Activity {

    private ImageView lamp;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        requestWindowFeature(Window.FEATURE_NO_TITLE);//this method can cancel the  tittle bar of APP
        setContentView(R.layout.activity_main);//make the ID(activity_main) load in the MainActivity

        lamp = findViewById(R.id.ima_lamp);//bind
        lamp.setOnClickListener(new View.OnClickListener() {//monitor

            private boolean OPEN = false;//the flag to judge weather the lamp is open

            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub
                if (!OPEN) {//if !open ,invoking the picture(co_lighting_close),and turn the flag to true,than next click will be false and invoking the picture(co_light_open)
                    lamp.setImageResource(R.drawable.co_lighting_close);
                    OPEN = true;
                } else {//if open,invoking the picture(co_light_open),and turn the flag to false,than next click will be false and invoking the picture(co_light_close)
                    lamp.setImageResource(R.drawable.co_lighting_open);
                    OPEN = false;
                }
            }
        });
    }
}

###本文的图片是co_light_close和co_light_open(以防万一,提一下比较好)

效果:点一下,换个图

(相信你们的想象力)

图片资源:

搜索阿里巴巴矢量图
https://www.iconfont.cn
里面有超多的资源
独学而无友,则孤陋而寡闻。欢迎大家评论与私信,一起变强!😘🤞

评论 7
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

豹豹-Boss成

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值