Android图片显示ICON——基础编
来源:互联网 发布:疑心暗鬼单人动作数据 编辑:程序博客网 时间:2024/04/28 18:50
Android图片显示ICON——基础编
今天写了一编小的图片显示代码,是从ICON的图片的显示结果来说明如果从以后的过程中更加适合里面去。先让我们看一下:图:
让我们看一下类中是代码:
package com.smart.widget;import android.content.Context;import android.graphics.Bitmap;import android.graphics.BitmapFactory;import android.graphics.Canvas;import android.graphics.Rect;import android.util.AttributeSet;import android.widget.TextView;public class IconText extends TextView{// 命名空间的值private final String namespace = "http://net.blogjava.mobile";// 图像资源IDprivate int resourceId = 0;private Bitmap bitmap;public IconText(Context context, AttributeSet attrs){super(context, attrs);resourceId = attrs.getAttributeResourceValue(namespace, "iconSrc", 0);if (resourceId > 0)bitmap = BitmapFactory.decodeResource(getResources(), resourceId);}@Overrideprotected void onDraw(Canvas canvas){if (bitmap != null){// 从原图上截取图像的区域,在本例中为整个图像Rect src = new Rect();// 将截取的图像复制到bitmap上的目标区域,在本例中与复制区域相同Rect target = new Rect();src.left = 0;src.top = 0;src.right = bitmap.getWidth();src.bottom = bitmap.getHeight();int textHeight = (int) getTextSize();target.left = 0;// 计算图像复制到目录区域的纵坐标。由于TextView中文本内容并不是从最顶端开始绘制的,因此,需要重新计算绘制图像的纵坐标target.top = (int) ((getMeasuredHeight() - getTextSize()) / 2) + 1;target.bottom = target.top + textHeight;// 为了保证图像不变形,需要根据图像高度重新计算图像的宽度target.right = (int) (textHeight * (bitmap.getWidth() / (float) bitmap.getHeight()));// 开始绘制图像canvas.drawBitmap(bitmap, src, target, getPaint());// 将TextView中的文本向右移动一定的距离(在本例中移动了图像宽度加2个象素点的位置)canvas.translate(target.right + 2, 0);}super.onDraw(canvas);}}
main代码:<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:mobile="http://www.baidu.com" android:orientation="vertical" android:layout_width="fill_parent" android:layout_height="fill_parent" > <com.smart.widget.IconText android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="第一个笑脸" mobile:iconSrc="drawable/small" /> <com.smart.widget.IconText android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="第二个笑脸" android:textSize="24dp" mobile:iconSrc="drawable/small" /> <com.smart.widget.IconText android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="第三个笑脸" android:textSize="36dp" mobile:iconSrc="drawable/small" /> <com.smart.widget.IconText android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="第四个笑脸" android:textSize="48dp" mobile:iconSrc="drawable/small" /> <com.smart.widget.IconText android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="第五个笑脸" android:textSize="36dp" mobile:iconSrc="drawable/small" /> <com.smart.widget.IconText android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="第六个笑脸" android:textSize="24dp" mobile:iconSrc="drawable/small" /> <com.smart.widget.IconText android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="第七个笑脸" mobile:iconSrc="drawable/small" /> </LinearLayout>
- Android图片显示ICON——基础编
- firefox icon图片显示异常
- Android隐藏Icon,条件启动显示icon
- JButton(Icon)图片按钮无法显示图片
- OpenCV基础篇——图片显示
- 让DuiLib支持Icon图片显示
- DuiLib改底层支持Icon图片显示
- 让DuiLib支持Icon图片显示
- DuiLib改底层支持Icon图片显示
- VC中显示ICON和BMP图片
- Android:PopupMenu显示item中的android:icon
- android图片编解码architecture
- 用LoadImage加载ICON图片,图片显示不正常
- android launcher 日历icon显示日期
- android launcher 日历icon显示星期
- Android菜单不显示ICON解决方案
- Android notification动态设置网络图片icon
- android使用icon font字体代替图片
- 7、Linux程序设计入门--线程操作
- 国内开放平台汇总
- poj2328简单模拟
- 2012-3-18日总结
- 8、Linux程序设计入门--网络编程
- Android图片显示ICON——基础编
- poj2590
- 9、Linux下C开发工具介绍
- 掐住吃内存大虫的喉咙---find.exe和cmd.exe和Rar.exe病毒
- struts2上传文件
- 黑马程序员 SQL SERVER 基础
- Objective-C中的meta-class
- 将输入中的制表符替换成适当数目的空格,使空格充满到下一个制表符终止的地方
- 将空格串替换为最小数量的制表符和空格,但要保持单词之间的间隔不变。