Android图片显示ICON――基础编
16lz
2021-01-26
今天写了一编小的图片显示代码,是从ICON的图片的显示结果来说明如果从以后的过程中更加适合里面去。先让我们看一下:图:
让我们看一下类中是代码:
- packagecom.smart.widget;
- importandroid.content.Context;
- importandroid.graphics.Bitmap;
- importandroid.graphics.BitmapFactory;
- importandroid.graphics.Canvas;
- importandroid.graphics.Rect;
- importandroid.util.AttributeSet;
- importandroid.widget.TextView;
- publicclassIconTextextendsTextView{
- //命名空间的值
- privatefinalStringnamespace="http://net.blogjava.mobile";
- //图像资源ID
- privateintresourceId=0;
- privateBitmapbitmap;
- publicIconText(Contextcontext,AttributeSetattrs)
- {
- super(context,attrs);
- resourceId=attrs.getAttributeResourceValue(namespace,"iconSrc",0);
- if(resourceId>0)
- bitmap=BitmapFactory.decodeResource(getResources(),resourceId);
- }
- @Override
- protectedvoidonDraw(Canvascanvas)
- {
- if(bitmap!=null)
- {
- //从原图上截取图像的区域,在本例中为整个图像
- Rectsrc=newRect();
- //将截取的图像复制到bitmap上的目标区域,在本例中与复制区域相同
- Recttarget=newRect();
- src.left=0;
- src.top=0;
- src.right=bitmap.getWidth();
- src.bottom=bitmap.getHeight();
- inttextHeight=(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代码:
- <?xmlversion="1.0"encoding="utf-8"?>
- <LinearLayoutxmlns: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照相机竖屏显示的图像出现旋转的问题,但成像被拉伸了
- android,自定义ViewGroup,tagView,横竖摆放view不重叠
- Bitmap,使用Matrix对图像几何变换
- Android多媒体开发高级编程——为智能手机和平板电脑开发图形、
- 【Android】图像变换
- ImageView android:scaleType="centerCrop"
- Android(安卓)ImageReader使用
- android图形图像函数汇集
- android中图形图像处理之drawable用法分析