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

相关文章

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

MySQL 事务的概念及ACID属性和使用详解

《MySQL事务的概念及ACID属性和使用详解》MySQL通过多线程实现存储工作,因此在并发访问场景中,事务确保了数据操作的一致性和可靠性,下面通过本文给大家介绍MySQL事务的概念及ACID属性和... 目录一、什么是事务二、事务的属性及使用2.1 事务的 ACID 属性2.2 为什么存在事务2.3 事务

Spring Cache注解@Cacheable的九个属性详解

《SpringCache注解@Cacheable的九个属性详解》在@Cacheable注解的使用中,共有9个属性供我们来使用,这9个属性分别是:value、cacheNames、key、key... 目录1.value/cacheNames 属性2.key属性3.keyGeneratjavascriptor

Spring Boot 事务详解(事务传播行为、事务属性)

《SpringBoot事务详解(事务传播行为、事务属性)》SpringBoot提供了强大的事务管理功能,通过@Transactional注解可以方便地配置事务的传播行为和属性,本文将详细介绍Spr... 目录Spring Boot 事务详解引言声明式事务管理示例编程式事务管理示例事务传播行为1. REQUI

Android NDK版本迭代与FFmpeg交叉编译完全指南

《AndroidNDK版本迭代与FFmpeg交叉编译完全指南》在Android开发中,使用NDK进行原生代码开发是一项常见需求,特别是当我们需要集成FFmpeg这样的多媒体处理库时,本文将深入分析A... 目录一、android NDK版本迭代分界线二、FFmpeg交叉编译关键注意事项三、完整编译脚本示例四

Android与iOS设备MAC地址生成原理及Java实现详解

《Android与iOS设备MAC地址生成原理及Java实现详解》在无线网络通信中,MAC(MediaAccessControl)地址是设备的唯一网络标识符,本文主要介绍了Android与iOS设备M... 目录引言1. MAC地址基础1.1 MAC地址的组成1.2 MAC地址的分类2. android与I

Android 实现一个隐私弹窗功能

《Android实现一个隐私弹窗功能》:本文主要介绍Android实现一个隐私弹窗功能,本文通过实例代码给大家介绍的非常详细,感兴趣的朋友一起看看吧... 效果图如下:1. 设置同意、退出、点击用户协议、点击隐私协议的函数参数2. 《用户协议》、《隐私政策》设置成可点击的,且颜色要区分出来res/l