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实现任意版本设置默认的锁屏壁纸和桌面壁纸(两张壁纸可不一致)

客户有些需求需要设置默认壁纸和锁屏壁纸  在默认情况下 这两个壁纸是相同的  如果需要默认的锁屏壁纸和桌面壁纸不一样 需要额外修改 Android13实现 替换默认桌面壁纸: 将图片文件替换frameworks/base/core/res/res/drawable-nodpi/default_wallpaper.*  (注意不能是bmp格式) 替换默认锁屏壁纸: 将图片资源放入vendo

Android平台播放RTSP流的几种方案探究(VLC VS ExoPlayer VS SmartPlayer)

技术背景 好多开发者需要遴选Android平台RTSP直播播放器的时候,不知道如何选的好,本文针对常用的方案,做个大概的说明: 1. 使用VLC for Android VLC Media Player(VLC多媒体播放器),最初命名为VideoLAN客户端,是VideoLAN品牌产品,是VideoLAN计划的多媒体播放器。它支持众多音频与视频解码器及文件格式,并支持DVD影音光盘,VCD影

滚雪球学Java(87):Java事务处理:JDBC的ACID属性与实战技巧!真有两下子!

咦咦咦,各位小可爱,我是你们的好伙伴——bug菌,今天又来给大家普及Java SE啦,别躲起来啊,听我讲干货还不快点赞,赞多了我就有动力讲得更嗨啦!所以呀,养成先点赞后阅读的好习惯,别被干货淹没了哦~ 🏆本文收录于「滚雪球学Java」专栏,专业攻坚指数级提升,助你一臂之力,带你早日登顶🚀,欢迎大家关注&&收藏!持续更新中,up!up!up!! 环境说明:Windows 10

BUUCTF(34)特殊的 BASE64

使用pycharm时,如果想把代码撤销到之前的状态可以用 Ctrl+z 如果不小心撤销多了,可以用 Ctrl+Shift+Z 还原, 别傻傻的重新敲了 BUUCTF在线评测 (buuoj.cn) 查看字符串,想到base64的变表 这里用的c++的标准程序库中的string,头文件是#include<string> 这是base64的加密函数 std::string

android-opencv-jni

//------------------start opencv--------------------@Override public void onResume(){ super.onResume(); //通过OpenCV引擎服务加载并初始化OpenCV类库,所谓OpenCV引擎服务即是 //OpenCV_2.4.3.2_Manager_2.4_*.apk程序包,存

从状态管理到性能优化:全面解析 Android Compose

文章目录 引言一、Android Compose基本概念1.1 什么是Android Compose?1.2 Compose的优势1.3 如何在项目中使用Compose 二、Compose中的状态管理2.1 状态管理的重要性2.2 Compose中的状态和数据流2.3 使用State和MutableState处理状态2.4 通过ViewModel进行状态管理 三、Compose中的列表和滚动

Android 10.0 mtk平板camera2横屏预览旋转90度横屏拍照图片旋转90度功能实现

1.前言 在10.0的系统rom定制化开发中,在进行一些平板等默认横屏的设备开发的过程中,需要在进入camera2的 时候,默认预览图像也是需要横屏显示的,在上一篇已经实现了横屏预览功能,然后发现横屏预览后,拍照保存的图片 依然是竖屏的,所以说同样需要将图片也保存为横屏图标了,所以就需要看下mtk的camera2的相关横屏保存图片功能, 如何实现实现横屏保存图片功能 如图所示: 2.mtk

android应用中res目录说明

Android应用的res目录是一个特殊的项目,该项目里存放了Android应用所用的全部资源,包括图片、字符串、颜色、尺寸、样式等,类似于web开发中的public目录,js、css、image、style。。。。 Android按照约定,将不同的资源放在不同的文件夹中,这样可以方便的让AAPT(即Android Asset Packaging Tool , 在SDK的build-tools目

Android fill_parent、match_parent、wrap_content三者的作用及区别

这三个属性都是用来适应视图的水平或者垂直大小,以视图的内容或尺寸为基础的布局,比精确的指定视图的范围更加方便。 1、fill_parent 设置一个视图的布局为fill_parent将强制性的使视图扩展至它父元素的大小 2、match_parent 和fill_parent一样,从字面上的意思match_parent更贴切一些,于是从2.2开始,两个属性都可以使用,但2.3版本以后的建议使

Android Environment 获取的路径问题

1. 以获取 /System 路径为例 /*** Return root of the "system" partition holding the core Android OS.* Always present and mounted read-only.*/public static @NonNull File getRootDirectory() {return DIR_ANDR