用TextView显示带图片的效果

为了实现在TextView中显示图片,有时对图片的宽度与高度有限制的话,可以对实现进行放大与缩小操作! 



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 
Java代码   收藏代码
  1. package a.gridview;  
  2.   
  3. import android.app.Activity;  
  4. import android.graphics.Bitmap;  
  5. import android.graphics.Canvas;  
  6. import android.graphics.Matrix;  
  7. import android.graphics.PixelFormat;  
  8. import android.graphics.Rect;  
  9. import android.graphics.drawable.BitmapDrawable;  
  10. import android.graphics.drawable.Drawable;  
  11. import android.os.Bundle;  
  12. import android.widget.TextView;  
  13.   
  14. public class GridView extends Activity {  
  15.           
  16.         private TextView text;  
  17.         private TextView text1;  
  18.     /** Called when the activity is first created. */  
  19.     @Override  
  20.     public void onCreate(Bundle savedInstanceState) {  
  21.         super.onCreate(savedInstanceState);  
  22.         setContentView(R.layout.main);  
  23.         text = (TextView) findViewById(R.id.TextView01);  
  24.         Drawable draw = this.getResources().getDrawable(R.drawable.srvmng);     
  25.         text.setCompoundDrawablesWithIntrinsicBounds(null, draw, null,null);  
  26.         text.setText("应用");  
  27.           
  28.         text1 = (TextView) findViewById(R.id.TextView02);  
  29.         Drawable draw1 = this.getResources().getDrawable(R.drawable.srvmng);    
  30.         int w = draw1.getIntrinsicWidth();  
  31.         int h = draw1.getIntrinsicHeight();  
  32.         Rect rect = draw1.getBounds();  
  33.         text1.setCompoundDrawablesWithIntrinsicBounds(null, zoomDrawable(draw1,32,32), null,null);  
  34.         text1.setText("设置");  
  35.     }  
  36.       
  37.     static Drawable zoomDrawable(Drawable drawable, int w, int h)  
  38.     {  
  39.               int width = drawable.getIntrinsicWidth();  
  40.               int height= drawable.getIntrinsicHeight();  
  41.               Bitmap oldbmp = drawableToBitmap(drawable); // drawable转换成bitmap  
  42.               Matrix matrix = new Matrix();   // 创建操作图片用的Matrix对象  
  43.               float scaleWidth = ((float)w / width);   // 计算缩放比例  
  44.               float scaleHeight = ((float)h / height);  
  45.               matrix.postScale(scaleWidth, scaleHeight);         // 设置缩放比例  
  46.               Bitmap newbmp = Bitmap.createBitmap(oldbmp, 00, width, height, matrix, true);       // 建立新的bitmap,其内容是对原bitmap的缩放后的图  
  47.               return new BitmapDrawable(newbmp);       // 把bitmap转换成drawable并返回  
  48.     }  
  49.   
  50.     static Bitmap drawableToBitmap(Drawable drawable) // drawable 转换成bitmap  
  51.     {  
  52.               int width = drawable.getIntrinsicWidth();   // 取drawable的长宽  
  53.               int height = drawable.getIntrinsicHeight();  
  54.               Bitmap.Config config = drawable.getOpacity() != PixelFormat.OPAQUE ? Bitmap.Config.ARGB_8888:Bitmap.Config.RGB_565;         // 取drawable的颜色格式  
  55.               Bitmap bitmap = Bitmap.createBitmap(width, height, config);     // 建立对应bitmap  
  56.               Canvas canvas = new Canvas(bitmap);         // 建立对应bitmap的画布  
  57.               drawable.setBounds(00, width, height);  
  58.               drawable.draw(canvas);      // 把drawable内容画到画布中  
  59.               return bitmap;  
  60.     }  
  61.   
  62. }  


显示效果: 

 

转自: http://qq361119908.iteye.com/blog/632837

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值