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

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

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

main代码:

            
  1. <?xmlversion="1.0"encoding="utf-8"?>
  2. <LinearLayoutxmlns: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. <com.smart.widget.IconText
  9. android:layout_width="fill_parent"
  10. android:layout_height="wrap_content"
  11. android:text="第一个笑脸"
  12. mobile:iconSrc="drawable/small"
  13. />
  14. <com.smart.widget.IconText
  15. android:layout_width="fill_parent"
  16. android:layout_height="wrap_content"
  17. android:text="第二个笑脸"
  18. android:textSize="24dp"
  19. mobile:iconSrc="drawable/small"
  20. />
  21. <com.smart.widget.IconText
  22. android:layout_width="fill_parent"
  23. android:layout_height="wrap_content"
  24. android:text="第三个笑脸"
  25. android:textSize="36dp"
  26. mobile:iconSrc="drawable/small"
  27. />
  28. <com.smart.widget.IconText
  29. android:layout_width="fill_parent"
  30. android:layout_height="wrap_content"
  31. android:text="第四个笑脸"
  32. android:textSize="48dp"
  33. mobile:iconSrc="drawable/small"
  34. />
  35. <com.smart.widget.IconText
  36. android:layout_width="fill_parent"
  37. android:layout_height="wrap_content"
  38. android:text="第五个笑脸"
  39. android:textSize="36dp"
  40. mobile:iconSrc="drawable/small"
  41. />
  42. <com.smart.widget.IconText
  43. android:layout_width="fill_parent"
  44. android:layout_height="wrap_content"
  45. android:text="第六个笑脸"
  46. android:textSize="24dp"
  47. mobile:iconSrc="drawable/small"
  48. />
  49. <com.smart.widget.IconText
  50. android:layout_width="fill_parent"
  51. android:layout_height="wrap_content"
  52. android:text="第七个笑脸"
  53. mobile:iconSrc="drawable/small"
  54. />
  55. </LinearLayout>


更多相关文章

  1. android照相机竖屏显示的图像出现旋转的问题,但成像被拉伸了
  2. android,自定义ViewGroup,tagView,横竖摆放view不重叠
  3. Bitmap,使用Matrix对图像几何变换
  4. Android多媒体开发高级编程——为智能手机和平板电脑开发图形、
  5. 【Android】图像变换
  6. ImageView android:scaleType="centerCrop"
  7. Android(安卓)ImageReader使用
  8. android图形图像函数汇集
  9. android中图形图像处理之drawable用法分析

随机推荐

  1. [置顶] Android学好Shape不再依赖美工
  2. Android(安卓)事件冒泡及捕获
  3. 在ListItem某组件添加响应事件
  4. Android从Linux系统启动
  5. Android系统信息获取 之十:移动网络相关信
  6. 「Android(安卓)Tips」解决 Mac OSX 无法
  7. Android应用商店——Splash页面的实现,And
  8. Android(安卓)Geocoder(位置解析)
  9. Android(安卓)使用grade实现Android(安卓
  10. Android开机自启动