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;  
     }  
   
 }  


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值