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"
具体在代码中的用法是:
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