Android图片显示ICON——基础编

今天写了一编小的图片显示代码,是从ICON的图片的显示结果来说明如果从以后的过程中更加适合里面去。先让我们看一下:图:

让我们看一下类中是代码:

 

 
 
  1. package com.smart.widget; 
  2.  
  3. import android.content.Context; 
  4. import android.graphics.Bitmap; 
  5. import android.graphics.BitmapFactory; 
  6. import android.graphics.Canvas; 
  7. import android.graphics.Rect; 
  8. import android.util.AttributeSet; 
  9. import android.widget.TextView; 
  10.  
  11. public class IconText  extends TextView{ 
  12.  
  13. //  命名空间的值 
  14.     private final String namespace = "http://net.blogjava.mobile"
  15.     //  图像资源ID 
  16.     private int resourceId = 0
  17.     private Bitmap bitmap; 
  18.  
  19.     public IconText(Context context, AttributeSet attrs) 
  20.     { 
  21.         super(context, attrs); 
  22.         resourceId = attrs.getAttributeResourceValue(namespace, "iconSrc"0); 
  23.         if (resourceId > 0
  24.             bitmap = BitmapFactory.decodeResource(getResources(), resourceId); 
  25.     } 
  26.  
  27.     @Override 
  28.     protected void onDraw(Canvas canvas) 
  29.     { 
  30.         if (bitmap != null
  31.         { 
  32.              
  33.             //  从原图上截取图像的区域,在本例中为整个图像 
  34.             Rect src = new Rect(); 
  35.             //  将截取的图像复制到bitmap上的目标区域,在本例中与复制区域相同 
  36.             Rect target = new Rect(); 
  37.             src.left = 0
  38.             src.top = 0
  39.             src.right = bitmap.getWidth(); 
  40.             src.bottom = bitmap.getHeight(); 
  41.  
  42.             int textHeight = (int) getTextSize(); 
  43.             target.left = 0
  44.             //  计算图像复制到目录区域的纵坐标。由于TextView中文本内容并不是从最顶端开始绘制的,因此,需要重新计算绘制图像的纵坐标 
  45.             target.top = (int) ((getMeasuredHeight() - getTextSize()) / 2) + 1
  46.             target.bottom = target.top + textHeight; 
  47.             //  为了保证图像不变形,需要根据图像高度重新计算图像的宽度 
  48.             target.right = (int) (textHeight * (bitmap.getWidth() / (float) bitmap 
  49.                     .getHeight())); 
  50.             //  开始绘制图像 
  51.             canvas.drawBitmap(bitmap, src, target, getPaint()); 
  52.             //  将TextView中的文本向右移动一定的距离(在本例中移动了图像宽度加2个象素点的位置) 
  53.              
  54.             canvas.translate(target.right + 20); 
  55.         } 
  56.         super.onDraw(canvas); 
  57.  
  58.     } 
  59.      
  60.      
  61.  

main代码:
 

 
 
  1. <?xml version="1.0" encoding="utf-8"?> 
  2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
  3.     xmlns:mobile="http://www.baidu.com" 
  4.     android:orientation="vertical" 
  5.     android:layout_width="fill_parent" 
  6.     android:layout_height="fill_parent" 
  7.     > 
  8.      
  9.      
  10.     <com.smart.widget.IconText 
  11.     android:layout_width="fill_parent" 
  12.     android:layout_height="wrap_content" 
  13.     android:text="第一个笑脸" 
  14.     mobile:iconSrc="drawable/small" 
  15.     /> 
  16.      
  17.     <com.smart.widget.IconText 
  18.     android:layout_width="fill_parent" 
  19.     android:layout_height="wrap_content" 
  20.     android:text="第二个笑脸" 
  21.     android:textSize="24dp" 
  22.     mobile:iconSrc="drawable/small" 
  23.     /> 
  24.      
  25.     <com.smart.widget.IconText 
  26.     android:layout_width="fill_parent" 
  27.     android:layout_height="wrap_content" 
  28.     android:text="第三个笑脸" 
  29.     android:textSize="36dp" 
  30.     mobile:iconSrc="drawable/small" 
  31.     /> 
  32.      
  33.     <com.smart.widget.IconText 
  34.     android:layout_width="fill_parent" 
  35.     android:layout_height="wrap_content" 
  36.     android:text="第四个笑脸" 
  37.     android:textSize="48dp" 
  38.     mobile:iconSrc="drawable/small" 
  39.     /> 
  40.      
  41.     <com.smart.widget.IconText 
  42.     android:layout_width="fill_parent" 
  43.     android:layout_height="wrap_content" 
  44.     android:text="第五个笑脸" 
  45.     android:textSize="36dp" 
  46.     mobile:iconSrc="drawable/small" 
  47.     /> 
  48.      
  49.     <com.smart.widget.IconText 
  50.     android:layout_width="fill_parent" 
  51.     android:layout_height="wrap_content" 
  52.     android:text="第六个笑脸" 
  53.     android:textSize="24dp" 
  54.     mobile:iconSrc="drawable/small" 
  55.     /> 
  56.     <com.smart.widget.IconText 
  57.     android:layout_width="fill_parent" 
  58.     android:layout_height="wrap_content" 
  59.     android:text="第七个笑脸" 
  60.     mobile:iconSrc="drawable/small" 
  61.     /> 
  62.      
  63.      
  64.      
  65. </LinearLayout> 


 

 

本文转自 llb988 51CTO博客,原文链接:http://blog.51cto.com/llb988/497220,如需转载请自行联系原作者

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值