Android图片显示ICON——基础编
2011-02-21 11:12
453 查看
今天写了一编小的图片显示代码,是从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"; // 图像资源ID private 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); } @Override protected 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——基础编
- Android图片显示ICON——基础编
- Android基础-获取网络上的图片并显示
- Android Http基础:图片下载并显示和WebView的应用
- Android 图片的高效加载Bitmap显示——开源框架基础核心代码
- 从零开始Android游戏编程(第二版) 第三章 显示文字和图片
- Android 使用ContentProvider扫描手机中的图片,仿微信显示本地图片效果
- android打开,保存图片到sd卡,显示图片
- android高仿微信UI点击头像显示大图片效果
- Android的TextView使用Html来处理图片显示、字体样式、超链接等
- Android 使用开源库StickyGridHeaders来实现带sections和headers的GridView显示本地图片效果
- 【android基础】之Android设置背景图片【原创】
- Android的ImageView如何显示网络图片资源
- Android textview字体颜色显示和图片显示
- Android 使用URL下载图片到本地并显示
- android保存图片到本地并可以在相册中显示出来
- Android中显示html标签或者带图片
- android 根据SD卡中图片路径读取并显示SD中的图片——源代码
- 让DuiLib支持Icon图片显示
- android 网络图片显示