ImageButton点击按钮改变图片

2023-11-29 09:58

本文主要是介绍ImageButton点击按钮改变图片,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

一.ImageButton点击按钮改变图片

**************************ImageButton点击按钮改变图片 有两种写法*****************************

*****第一种:在代码中*****

**MaActivity.class**

 

import android.os.Build;
import android.os.Bundle;
import android.annotation.SuppressLint;
import android.annotation.TargetApi;
import android.app.Activity;
import android.view.Menu;
import android.view.MotionEvent;
import android.view.View;
import android.widget.ImageButton;
import android.widget.Toast;public class MainActivity extends Activity {private ImageButton imageButton;@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);//发现控件imageButton = (ImageButton) findViewById(R.id.imageButton);//触摸监听控件imageButton.setOnTouchListener(new View.OnTouchListener(){            public boolean onTouch(View v, MotionEvent event) {     //点击if(event.getAction() == MotionEvent.ACTION_DOWN){       //重新设置按下时的背景图片  ((ImageButton)v).setImageDrawable(getResources().getDrawable(R.drawable.ae));   Toast.makeText(MainActivity.this, "点击了", 0).show();}else if(event.getAction() == MotionEvent.ACTION_UP){ //松开      //再修改为抬起时的正常图片  ((ImageButton)v).setImageDrawable(getResources().getDrawable(R.drawable.as));  Toast.makeText(MainActivity.this, "松开了", 0).show();}  return false;       }       });  }}

 

 

**布局**

 

<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"><ImageButtonandroid:id="@+id/imageButton"android:layout_width="100dp"android:layout_height="100dp"android:layout_centerHorizontal="true"android:layout_centerVertical="true"/></RelativeLayout>

 

 

***********************第二种写法:通过给按钮配置XML文件来实现图片按钮的背景切换效果,方法如下***********************

1) 在Layout或drawable下增加一个image_btn_press.xml文件,内容如下:图片自己选择添加

 

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

 

****************布局********************

2) 在main.xml中设置图片按钮的属性,装上面的xml文件增加到图片按钮中,内容如下:

 

<ImageButton  android:id="@+id/imageButton2"  android:layout_width="wrap_content"  android:layout_height="wrap_content"  android:background="@layout/image_btn_press" />  

 

 

在ImageButtonk中指定background的属性值:@layout/image_btn_press,其中image_btn_press就是上面为该图片铵钮创建的XML配置文件,文件名不要写错了。同时,你也可以将image_btn_press.xml文件放到drawable的目录下,此时,就要将它的属性值修改为:@drawable/image_btn_press即可,也就是说指定它的XML文件的正确路经就行了。

另外,需要特别注意的是在ImageButton中,如果使用XML配置文件来设置图片的效果的话,就不要再指定它的android:src=""属性值了,否则图片的按下效果就出不来了。

这两种方法各有各的好处,在实际运用过种当种可以根据自己的需要进行选择。

 

二.Button、TextView等布局点击按钮改变背景颜色

1.布局使用:(设置圆角状态选择器)

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"android:layout_width="match_parent"android:layout_height="wrap_content"android:background="#ffffff"><!--android:background="?attr/selectableItemBackground"--><LinearLayoutandroid:id="@+id/ll"android:layout_marginTop="30dp"android:layout_width="match_parent"android:layout_height="wrap_content"android:background="@drawable/course_list_selector"android:orientation="horizontal"android:paddingTop="15dp"android:paddingBottom="15dp"><LinearLayoutandroid:id="@+id/ll_data"android:paddingTop="15dp"android:paddingBottom="15dp"android:background="@drawable/selector_button"android:orientation="horizontal"android:layout_width="match_parent"android:layout_height="wrap_content"><ImageViewandroid:background="@drawable/bg_yuanjiao"android:layout_gravity="center"android:layout_width="50dp"android:visibility="gone"android:layout_height="50dp" /><TextViewandroid:id="@+id/tv_num"android:text="1"android:layout_marginLeft="20dp"android:textColor="@color/white"android:textSize="14sp"android:gravity="center|left"android:layout_gravity="center"android:layout_width="wrap_content"android:layout_height="wrap_content" /><TextViewandroid:id="@+id/popup_tv"android:layout_width="wrap_content"android:layout_height="wrap_content"android:gravity="center|left"android:singleLine="true"android:ellipsize="marquee"android:marqueeRepeatLimit="marquee_forever"android:layout_gravity="center"android:layout_marginLeft="10dp"android:text="KING"android:textColor="@color/white"android:textSize="14sp" /></LinearLayout></LinearLayout><TextViewandroid:layout_width="match_parent"android:layout_height="0.5dp"android:layout_below="@+id/ll"android:background="@color/colorPrimary" /></RelativeLayout>
android:background="@drawable/course_list_selector"
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android"><!-- 这个是按下去的时候item显示背景色 -->;<item android:drawable="@color/pink" android:state_pressed="true"></item><!-- 这个是默认的item背景色,设为了透明 --><item android:drawable="@android:color/transparent" android:state_pressed="false"></item></selector>

 或

android:background="@drawable/selector_button"

 

<?xml version="1.0" encoding="utf-8"?>
<!-- 默认圆角按钮样式 -->
<selector xmlns:android="http://schemas.android.com/apk/res/android"><!-- 禁用状态 --><item android:state_enabled="false"><shape android:shape="rectangle"><corners android:radius="@dimen/button_round_size" /><solid android:color="@color/colorButtonDisable" /></shape></item><!-- 按压状态 --><item android:state_pressed="true"><shape android:shape="rectangle"><corners android:radius="@dimen/button_round_size" /><solid android:color="@color/colorButtonPressed" /></shape></item><!-- 焦点状态 --><item android:state_focused="true"><shape android:shape="rectangle"><corners android:radius="@dimen/button_round_size" /><solid android:color="@color/colorButtonPressed" /></shape></item><!-- 默认状态 --><item><shape android:shape="rectangle"><corners android:radius="@dimen/button_round_size" /><solid android:color="@color/colorAccent" /></shape></item></selector>

 

 

 

 

 

 

这篇关于ImageButton点击按钮改变图片的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/432391

相关文章

Python实现AVIF图片与其他图片格式间的批量转换

《Python实现AVIF图片与其他图片格式间的批量转换》这篇文章主要为大家详细介绍了如何使用Pillow库实现AVIF与其他格式的相互转换,即将AVIF转换为常见的格式,比如JPG或PNG,需要的小... 目录环境配置1.将单个 AVIF 图片转换为 JPG 和 PNG2.批量转换目录下所有 AVIF 图

详解如何通过Python批量转换图片为PDF

《详解如何通过Python批量转换图片为PDF》:本文主要介绍如何基于Python+Tkinter开发的图片批量转PDF工具,可以支持批量添加图片,拖拽等操作,感兴趣的小伙伴可以参考一下... 目录1. 概述2. 功能亮点2.1 主要功能2.2 界面设计3. 使用指南3.1 运行环境3.2 使用步骤4. 核

Java图片压缩三种高效压缩方案详细解析

《Java图片压缩三种高效压缩方案详细解析》图片压缩通常涉及减少图片的尺寸缩放、调整图片的质量(针对JPEG、PNG等)、使用特定的算法来减少图片的数据量等,:本文主要介绍Java图片压缩三种高效... 目录一、基于OpenCV的智能尺寸压缩技术亮点:适用场景:二、JPEG质量参数压缩关键技术:压缩效果对比

使用Python开发一个简单的本地图片服务器

《使用Python开发一个简单的本地图片服务器》本文介绍了如何结合wxPython构建的图形用户界面GUI和Python内建的Web服务器功能,在本地网络中搭建一个私人的,即开即用的网页相册,文中的示... 目录项目目标核心技术栈代码深度解析完整代码工作流程主要功能与优势潜在改进与思考运行结果总结你是否曾经

Python FastAPI+Celery+RabbitMQ实现分布式图片水印处理系统

《PythonFastAPI+Celery+RabbitMQ实现分布式图片水印处理系统》这篇文章主要为大家详细介绍了PythonFastAPI如何结合Celery以及RabbitMQ实现简单的分布式... 实现思路FastAPI 服务器Celery 任务队列RabbitMQ 作为消息代理定时任务处理完整

使用C#代码在PDF文档中添加、删除和替换图片

《使用C#代码在PDF文档中添加、删除和替换图片》在当今数字化文档处理场景中,动态操作PDF文档中的图像已成为企业级应用开发的核心需求之一,本文将介绍如何在.NET平台使用C#代码在PDF文档中添加、... 目录引言用C#添加图片到PDF文档用C#删除PDF文档中的图片用C#替换PDF文档中的图片引言在当

详解C#如何提取PDF文档中的图片

《详解C#如何提取PDF文档中的图片》提取图片可以将这些图像资源进行单独保存,方便后续在不同的项目中使用,下面我们就来看看如何使用C#通过代码从PDF文档中提取图片吧... 当 PDF 文件中包含有价值的图片,如艺术画作、设计素材、报告图表等,提取图片可以将这些图像资源进行单独保存,方便后续在不同的项目中使

Java实现文件图片的预览和下载功能

《Java实现文件图片的预览和下载功能》这篇文章主要为大家详细介绍了如何使用Java实现文件图片的预览和下载功能,文中的示例代码讲解详细,感兴趣的小伙伴可以跟随小编一起学习一下... Java实现文件(图片)的预览和下载 @ApiOperation("访问文件") @GetMapping("

基于Python开发批量提取Excel图片的小工具

《基于Python开发批量提取Excel图片的小工具》这篇文章主要为大家详细介绍了如何使用Python中的openpyxl库开发一个小工具,可以实现批量提取Excel图片,有需要的小伙伴可以参考一下... 目前有一个需求,就是批量读取当前目录下所有文件夹里的Excel文件,去获取出Excel文件中的图片,并

Java实现数据库图片上传与存储功能

《Java实现数据库图片上传与存储功能》在现代的Web开发中,上传图片并将其存储在数据库中是常见的需求之一,本文将介绍如何通过Java实现图片上传,存储到数据库的完整过程,希望对大家有所帮助... 目录1. 项目结构2. 数据库表设计3. 实现图片上传功能3.1 文件上传控制器3.2 图片上传服务4. 实现