android 特殊按钮drawable属性

2024-06-06 16:38

本文主要是介绍android 特殊按钮drawable属性,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

转自:http://blog.csdn.net/xgpww/article/details/7659781

android控件button,TextView,imageview等可以设置四周显示一个图片(drawable);

最简单的方法就是在XML里设置属性drawableLeft,drawableTop,drawableRight,drawableBottom 里来设置图片,一般是透明的比较好,

背景图片来做点击效果

但有时需求是要动态更换图片,可以参考下面的方法:

setCompoundDrawables(left, top, right, bottom);
setCompoundDrawablesWithIntrinsicBounds(left, top, right, bottom)
意思是设置Drawable显示在text的左、上、右、下位置。
(Textview、Button都可以)
但是两者有些区别:


setCompoundDrawables 画的drawable的宽高是按drawable.setBound()设置的宽高,所以才有The Drawables must already have had setBounds(Rect) called.意思是说使用之前必须使用Drawable.setBounds设置Drawable的长宽。
而setCompoundDrawablesWithIntrinsicBounds是画的drawable的宽高是按drawable固定的宽高,即通过getIntrinsicWidth()与getIntrinsicHeight()获得,所以才有The Drawables' bounds will be set to their intrinsic bounds.这句话之说!

eg:

online_themeDownButton.setCompoundDrawablesWithIntrinsicBounds(getResources().getDrawable(R.drawable.market_downloading_cancel), null, null, null);


用textview显示带图片的效果
转自:http://blog.csdn.net/linshutao/article/details/5797344
main.xml Java代码 1 <?xml version="1.0" encoding="utf-8"?>  2 <LinearLayout xmlns:Android="http://schemas.android.com/apk/res/android"  3     android:orientation="vertical"  4     android:layout_width="fill_parent"  5     android:layout_height="fill_parent"  6     >  7 <TextView    8     android:layout_width="fill_parent"   9     android:layout_height="wrap_content"   10     android:text="@string/hello"  11     />  12 <TextView android:text="TextView01" android:id="@+id/TextView01" android:layout_width="wrap_content" android:layout_height="wrap_content"></TextView>  13 <TextView android:text="TextView02" android:id="@+id/TextView02" android:layout_width="wrap_content" android:layout_height="wrap_content"></TextView>  14 </LinearLayout>  GridView.java package a.gridview;  import android.app.Activity;  import android.graphics.Bitmap;  import android.graphics.Canvas;  import android.graphics.Matrix;  import android.graphics.PixelFormat;  import android.graphics.Rect;  import android.graphics.drawable.BitmapDrawable;  import android.graphics.drawable.Drawable;  import android.os.Bundle;  import android.widget.TextView;  public class GridView extends Activity {  private TextView text;  private TextView text1;  /** Called when the activity is first created. */  @Override  public void onCreate(Bundle savedInstanceState) {  super.onCreate(savedInstanceState);  setContentView(R.layout.main);  text = (TextView) findViewById(R.id.TextView01);  Drawable draw = this.getResources().getDrawable(R.drawable.srvmng);     text.setCompoundDrawablesWithIntrinsicBounds(null, draw, null,null);  text.setText("应用");  text1 = (TextView) findViewById(R.id.TextView02);  Drawable draw1 = this.getResources().getDrawable(R.drawable.srvmng);    int w = draw1.getIntrinsicWidth();  int h = draw1.getIntrinsicHeight();  Rect rect = draw1.getBounds();  text1.setCompoundDrawablesWithIntrinsicBounds(null, zoomDrawable(draw1,32,32), null,null);  text1.setText("设置");  }  static Drawable zoomDrawable(Drawable drawable, int w, int h)  {  int width = drawable.getIntrinsicWidth();  int height= drawable.getIntrinsicHeight();  Bitmap oldbmp = drawableToBitmap(drawable); // drawable转换成bitmap  Matrix matrix = new Matrix();   // 创建操作图片用的Matrix对象  float scaleWidth = ((float)w / width);   // 计算缩放比例  float scaleHeight = ((float)h / height);  matrix.postScale(scaleWidth, scaleHeight);         // 设置缩放比例  Bitmap newbmp = Bitmap.createBitmap(oldbmp, 0, 0, width, height, matrix, true);       // 建立新的bitmap,其内容是对原bitmap的缩放后的图  return new BitmapDrawable(newbmp);       // 把bitmap转换成drawable并返回  }  static Bitmap drawableToBitmap(Drawable drawable) // drawable 转换成bitmap  {  int width = drawable.getIntrinsicWidth();   // 取drawable的长宽  int height = drawable.getIntrinsicHeight();  Bitmap.Config config = drawable.getOpacity() != PixelFormat.OPAQUE ? Bitmap.Config.ARGB_8888:Bitmap.Config.RGB_565;         // 取drawable的颜色格式  Bitmap bitmap = Bitmap.createBitmap(width, height, config);     // 建立对应bitmap  Canvas canvas = new Canvas(bitmap);         // 建立对应bitmap的画布  drawable.setBounds(0, 0, width, height);  drawable.draw(canvas);      // 把drawable内容画到画布中  return bitmap;  }  }  


这篇关于android 特殊按钮drawable属性的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



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

相关文章

Android实现在线预览office文档的示例详解

《Android实现在线预览office文档的示例详解》在移动端展示在线Office文档(如Word、Excel、PPT)是一项常见需求,这篇文章为大家重点介绍了两种方案的实现方法,希望对大家有一定的... 目录一、项目概述二、相关技术知识三、实现思路3.1 方案一:WebView + Office Onl

Android实现两台手机屏幕共享和远程控制功能

《Android实现两台手机屏幕共享和远程控制功能》在远程协助、在线教学、技术支持等多种场景下,实时获得另一部移动设备的屏幕画面,并对其进行操作,具有极高的应用价值,本项目旨在实现两台Android手... 目录一、项目概述二、相关知识2.1 MediaProjection API2.2 Socket 网络

Android实现悬浮按钮功能

《Android实现悬浮按钮功能》在很多场景中,我们希望在应用或系统任意界面上都能看到一个小的“悬浮按钮”(FloatingButton),用来快速启动工具、展示未读信息或快捷操作,所以本文给大家介绍... 目录一、项目概述二、相关技术知识三、实现思路四、整合代码4.1 Java 代码(MainActivi

Android Mainline基础简介

《AndroidMainline基础简介》AndroidMainline是通过模块化更新Android核心组件的框架,可能提高安全性,本文给大家介绍AndroidMainline基础简介,感兴趣的朋... 目录关键要点什么是 android Mainline?Android Mainline 的工作原理关键

如何解决idea的Module:‘:app‘platform‘android-32‘not found.问题

《如何解决idea的Module:‘:app‘platform‘android-32‘notfound.问题》:本文主要介绍如何解决idea的Module:‘:app‘platform‘andr... 目录idea的Module:‘:app‘pwww.chinasem.cnlatform‘android-32

Android实现打开本地pdf文件的两种方式

《Android实现打开本地pdf文件的两种方式》在现代应用中,PDF格式因其跨平台、稳定性好、展示内容一致等特点,在Android平台上,如何高效地打开本地PDF文件,不仅关系到用户体验,也直接影响... 目录一、项目概述二、相关知识2.1 PDF文件基本概述2.2 android 文件访问与存储权限2.

Android Studio 配置国内镜像源的实现步骤

《AndroidStudio配置国内镜像源的实现步骤》本文主要介绍了AndroidStudio配置国内镜像源的实现步骤,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,... 目录一、修改 hosts,解决 SDK 下载失败的问题二、修改 gradle 地址,解决 gradle

在Android平台上实现消息推送功能

《在Android平台上实现消息推送功能》随着移动互联网应用的飞速发展,消息推送已成为移动应用中不可或缺的功能,在Android平台上,实现消息推送涉及到服务端的消息发送、客户端的消息接收、通知渠道(... 目录一、项目概述二、相关知识介绍2.1 消息推送的基本原理2.2 Firebase Cloud Me

Spring Security基于数据库的ABAC属性权限模型实战开发教程

《SpringSecurity基于数据库的ABAC属性权限模型实战开发教程》:本文主要介绍SpringSecurity基于数据库的ABAC属性权限模型实战开发教程,本文给大家介绍的非常详细,对大... 目录1. 前言2. 权限决策依据RBACABAC综合对比3. 数据库表结构说明4. 实战开始5. MyBA

CSS will-change 属性示例详解

《CSSwill-change属性示例详解》will-change是一个CSS属性,用于告诉浏览器某个元素在未来可能会发生哪些变化,本文给大家介绍CSSwill-change属性详解,感... will-change 是一个 css 属性,用于告诉浏览器某个元素在未来可能会发生哪些变化。这可以帮助浏览器优化