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 kotlin中 Channel 和 Flow 的区别和选择使用场景分析

《Androidkotlin中Channel和Flow的区别和选择使用场景分析》Kotlin协程中,Flow是冷数据流,按需触发,适合响应式数据处理;Channel是热数据流,持续发送,支持... 目录一、基本概念界定FlowChannel二、核心特性对比数据生产触发条件生产与消费的关系背压处理机制生命周期

Android ClassLoader加载机制详解

《AndroidClassLoader加载机制详解》Android的ClassLoader负责加载.dex文件,基于双亲委派模型,支持热修复和插件化,需注意类冲突、内存泄漏和兼容性问题,本文给大家介... 目录一、ClassLoader概述1.1 类加载的基本概念1.2 android与Java Class

python删除xml中的w:ascii属性的步骤

《python删除xml中的w:ascii属性的步骤》使用xml.etree.ElementTree删除WordXML中w:ascii属性,需注册命名空间并定位rFonts元素,通过del操作删除属... 可以使用python的XML.etree.ElementTree模块通过以下步骤删除XML中的w:as

Android DataBinding 与 MVVM使用详解

《AndroidDataBinding与MVVM使用详解》本文介绍AndroidDataBinding库,其通过绑定UI组件与数据源实现自动更新,支持双向绑定和逻辑运算,减少模板代码,结合MV... 目录一、DataBinding 核心概念二、配置与基础使用1. 启用 DataBinding 2. 基础布局

Android ViewBinding使用流程

《AndroidViewBinding使用流程》AndroidViewBinding是Jetpack组件,替代findViewById,提供类型安全、空安全和编译时检查,代码简洁且性能优化,相比Da... 目录一、核心概念二、ViewBinding优点三、使用流程1. 启用 ViewBinding (模块级

Python打印对象所有属性和值的方法小结

《Python打印对象所有属性和值的方法小结》在Python开发过程中,调试代码时经常需要查看对象的当前状态,也就是对象的所有属性和对应的值,然而,Python并没有像PHP的print_r那样直接提... 目录python中打印对象所有属性和值的方法实现步骤1. 使用vars()和pprint()2. 使

CSS3中的字体及相关属性详解

《CSS3中的字体及相关属性详解》:本文主要介绍了CSS3中的字体及相关属性,详细内容请阅读本文,希望能对你有所帮助... 字体网页字体的三个来源:用户机器上安装的字体,放心使用。保存在第三方网站上的字体,例如Typekit和Google,可以link标签链接到你的页面上。保存在你自己Web服务器上的字

SpringBoot读取ZooKeeper(ZK)属性的方法实现

《SpringBoot读取ZooKeeper(ZK)属性的方法实现》本文主要介绍了SpringBoot读取ZooKeeper(ZK)属性的方法实现,强调使用@ConfigurationProperti... 目录1. 在配置文件中定义 ZK 属性application.propertiesapplicati

Java反射实现多属性去重与分组功能

《Java反射实现多属性去重与分组功能》在Java开发中,​​List是一种非常常用的数据结构,通常我们会遇到这样的问题:如何处理​​List​​​中的相同字段?无论是去重还是分组,合理的操作可以提高... 目录一、开发环境与基础组件准备1.环境配置:2. 代码结构说明:二、基础反射工具:BeanUtils

Android学习总结之Java和kotlin区别超详细分析

《Android学习总结之Java和kotlin区别超详细分析》Java和Kotlin都是用于Android开发的编程语言,它们各自具有独特的特点和优势,:本文主要介绍Android学习总结之Ja... 目录一、空安全机制真题 1:Kotlin 如何解决 Java 的 NullPointerExceptio