getCompoundDrawables()和获取图片宽度和高度
2017-05-30 14:45
281 查看
getCompoundDrawables()返回一个包含四个数据的数组
在布局文件中设置四周图片,用getCompoundDrawables方法可以获取这4个位置的图片
该方法返回包含控件左,上,右,下四个位置的Drawable的数组
Drawable[] drawables=getCompoundDrawables();
在ImageView中的image,可以使用getWidth()和getHeight()来获取宽度和高度,但是获得的image宽度和高度不是很精确的;对于背景图片,你首先要获取背景的Drawable对象,然后将Drawable对象转换为BitmapDrawable,这样你就可以将背景图片作为Bitmap对象并获取其宽度和高度了。代码如下:Bitmap b = ((BitmapDrawble)imageView.getBackground()).getBitmap(); int w = b.getWidth(); int h = b.getHeight(); or do like this wayimageView.setDrawingCacheEnabled(true); Bitmap b = imageView.getDrawingCache(); int w = b.getWidth(); int h = b.getHeight(); 或者也可以像下面这样:imageView.setDrawingCacheEnabled(true); Bitmap b = imageView.getDrawingCache(); int w = b.getWidth(); int h = b.getHeight(); 上面的代码仅仅可以为你获取当前ImageView的大小:imageView.getWidth(); imageView.getHeight(); 如果你要获取Drawable image对象的大小,可用如下代码:Drawable d = getResources().getDrawable(R.drawable.yourimage); int h = d.getIntrinsicHeight(); int w = d.getIntrinsicWidth();
public void setCompoundDrawables (Drawable left, Drawable top, Drawable right, Drawable bottom);
这个方法呢,就是可以在Java代码动态的画 左上右下几个方向
类似于xml中的
android:drawableLeft="@drawable/icon"
android:drawableTop="@drawable/icon"
android:drawableRight="@drawable/icon"
android:drawableButtom="@drawable/icon"
具体在代码中的用法是:
void onFocusChange(View v,
boolean hasFocus)
当该控件获取焦点时hasfocus为true
在布局文件中设置四周图片,用getCompoundDrawables方法可以获取这4个位置的图片
该方法返回包含控件左,上,右,下四个位置的Drawable的数组
Drawable[] drawables=getCompoundDrawables();
在ImageView中的image,可以使用getWidth()和getHeight()来获取宽度和高度,但是获得的image宽度和高度不是很精确的;对于背景图片,你首先要获取背景的Drawable对象,然后将Drawable对象转换为BitmapDrawable,这样你就可以将背景图片作为Bitmap对象并获取其宽度和高度了。代码如下:Bitmap b = ((BitmapDrawble)imageView.getBackground()).getBitmap(); int w = b.getWidth(); int h = b.getHeight(); or do like this wayimageView.setDrawingCacheEnabled(true); Bitmap b = imageView.getDrawingCache(); int w = b.getWidth(); int h = b.getHeight(); 或者也可以像下面这样:imageView.setDrawingCacheEnabled(true); Bitmap b = imageView.getDrawingCache(); int w = b.getWidth(); int h = b.getHeight(); 上面的代码仅仅可以为你获取当前ImageView的大小:imageView.getWidth(); imageView.getHeight(); 如果你要获取Drawable image对象的大小,可用如下代码:Drawable d = getResources().getDrawable(R.drawable.yourimage); int h = d.getIntrinsicHeight(); int w = d.getIntrinsicWidth();
public void setCompoundDrawables (Drawable left, Drawable top, Drawable right, Drawable bottom);
这个方法呢,就是可以在Java代码动态的画 左上右下几个方向
类似于xml中的
android:drawableLeft="@drawable/icon"
android:drawableTop="@drawable/icon"
android:drawableRight="@drawable/icon"
android:drawableButtom="@drawable/icon"
具体在代码中的用法是:
Drawable drawable = getResources().getDrawable(R.drawable.spinner_checked); drawable.setBounds(0, 0, drawable.getMinimumWidth(), drawable.getMinimumHeight()); //设置边界 titleTv.setCompoundDrawables(null, null, drawable, null);//画在右边
void onFocusChange(View v,
boolean hasFocus)
当该控件获取焦点时hasfocus为true
相关文章推荐
- php getimagesize()函数获取图片宽度高度
- 获取图片宽度和高度的类,支持JPG,GIF,PNG,BMP
- js 获取网络图片的高度和宽度的实现方法(变通了下)
- jquery获取图片原始尺寸 (高度与宽度)示例
- 如何在页面加载完毕后获取图片高度或宽度问题
- js前台获取上传图片的宽度和高度
- 获取图片宽度和高度的类
- asp.net 获取图片的宽度和高度 (两方法)
- asp一个获取图片宽度和高度的类
- JS获取图片高度宽度的方法分享
- javascript获取动态加载图片的宽度和高度?
- 一个获取图片宽度和高度的类
- 获取图片高度和宽度
- 根据url获取图片的高度和宽度
- 解决chrome获取图片宽度和高度为0问题
- 一个获取图片宽度和高度的类
- 获取远程图片宽度高度的方法
- Android获取图片的宽度和高度
- javascript获取动态加载图片的宽度和高度?
- fso获取图片宽度和高度的类,支持JPG,GIF,PNG,BMP