自定义TextView,实现图片在文字之前的富文本效果
来源:互联网 发布:mac查询端口使用情况 编辑:程序博客网 时间:2024/06/05 11:11
先放一个效果图
实现这个效果,我用了两种方法
1.第一种方法
Drawable drawable = getResources().getDrawable(图片资源);drawable.setBounds(0, 0, drawable.getMinimumWidth(), drawable.getMinimumHeight());
//这个是我自定义的适配器和viewHolder 可忽略 重点是给textView设置这个属性((TextView) viewHolder.getView(R.id.tv_name)).setCompoundDrawables(drawable, null, null, null);
既然有了这种简单的方法,为什么还要自定义一个view呢 是因为我在使用的时候发现这个方法只适用于图标和文字只有一行的情况
像这样:
如果超过一行了图片整体就会居于文字的左侧 不能实现我们要的效果
2.第二种方法
第二种方法就是自定义的一个控件了 直接上图
package com.lechuang.youleduo.view.defineView;import android.content.Context;import android.content.res.TypedArray;import android.graphics.Bitmap;import android.graphics.Canvas;import android.graphics.Paint;import android.graphics.Rect;import android.graphics.drawable.BitmapDrawable;import android.support.annotation.Nullable;import android.util.AttributeSet;import android.view.View;import android.view.ViewGroup;import com.lechuang.youleduo.R;/** * Author: guoning * Date: 2017/10/3 * Description: */public class SpannelTextView extends View { private int shopType;//店铺类型 private Bitmap bitmap; private Paint paint; private String drawText; private String first, second; float mTextWidth; boolean mNewLine = false; private Rect rect = new Rect(); private float scaleDenisty; public SpannelTextView(Context context) { this(context, null); } public SpannelTextView(Context context, @Nullable AttributeSet attrs) { this(context, attrs, 0); } public SpannelTextView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) { super(context, attrs, defStyleAttr); TypedArray typedArray = context.obtainStyledAttributes(attrs, R.styleable.SpannelTextView); shopType = typedArray.getInt(R.styleable.SpannelTextView_shopType, 1); drawText = typedArray.getString(R.styleable.SpannelTextView_drawText); typedArray.recycle(); scaleDenisty = getResources().getDisplayMetrics().scaledDensity; paint = new Paint(Paint.ANTI_ALIAS_FLAG); paint.setTextSize(12 * scaleDenisty); paint.setStyle(Paint.Style.FILL); mTextWidth = paint.measureText(drawText) * getResources().getDisplayMetrics().scaledDensity; //不要再onDraw中处理这一步,应该避免在onDraw中分配对象内存 BitmapDrawable drawable = (BitmapDrawable) getResources().getDrawable(shopType == 1 ? R.drawable.zhuan_taobao : R.drawable.zhuan_tmall); bitmap = drawable.getBitmap(); } @Override protected void onDraw(Canvas canvas) { super.onDraw(canvas); ViewGroup.MarginLayoutParams lp = (ViewGroup.MarginLayoutParams) getLayoutParams(); canvas.drawBitmap(bitmap, lp.leftMargin, lp.topMargin, paint); Paint.FontMetrics fontMetrics = paint.getFontMetrics(); if (mNewLine) { paint.getTextBounds(first, 0, first.length(), rect); canvas.drawText(first, 3 * scaleDenisty + lp.leftMargin + bitmap.getWidth(), (3.5f * scaleDenisty + 0.5f) + lp.topMargin + (fontMetrics.descent - fontMetrics.ascent) / 2, paint); canvas.drawText(second, lp.leftMargin, (5f * scaleDenisty + 0.5f) + lp.topMargin + bitmap.getHeight() + (fontMetrics.descent - fontMetrics.ascent) / 2, paint); } else { canvas.drawText(drawText, 3 * scaleDenisty + lp.leftMargin + bitmap.getWidth(), (3.5f * scaleDenisty + 0.5f) + lp.topMargin + (fontMetrics.descent - fontMetrics.ascent) / 2, paint); } } @Override protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) { super.onMeasure(widthMeasureSpec, heightMeasureSpec); int height = MeasureSpec.getSize(heightMeasureSpec); int width = MeasureSpec.getSize(widthMeasureSpec); ViewGroup.MarginLayoutParams lp = (ViewGroup.MarginLayoutParams) getLayoutParams(); if (bitmap.getWidth() + mTextWidth + lp.leftMargin + lp.rightMargin > width) { //换行 for (int x = 0, length = drawText.length(); x < length; x++) { float v = paint.measureText(drawText.substring(0, x + 1)); if (bitmap.getWidth() + v + lp.leftMargin + lp.rightMargin >= width) { first = drawText.substring(0, x); second = drawText.substring(x); mNewLine = true; break; } } } setMeasuredDimension(width, (int) (34 * scaleDenisty + 0.5f)); } //设置文字内容 public void setDrawText(String drawText) { this.drawText = drawText; mTextWidth = paint.measureText(drawText) * getResources().getDisplayMetrics().scaledDensity; invalidate(); } //设置图片 这里判断服务器给我的int值 1是淘宝的icon 2是天猫 public void setShopType(int shopType) { this.shopType = shopType; BitmapDrawable drawable = (BitmapDrawable) getResources().getDrawable(shopType == 1 ? R.drawable.zhuan_taobao : R.drawable.zhuan_tmall); bitmap = drawable.getBitmap(); invalidate(); }}
就这样
阅读全文
0 0
- 自定义TextView,实现图片在文字之前的富文本效果
- Android Spanned实现TextView富文本效果(1)--文字
- Android TextView富文本、超链接和添加文字背景等其他效果的实现
- 解决TextView富文本显示时emoj或图片和文字不对齐的问题
- TextView 部分文字高亮、点击、添加超链接、替换字体、富文本、图片替换文字等实现
- 文字+图片的混编(富文本)
- Textview富文本的其它实现方式
- 【Android UI】自定义TextView实现动态的文字闪动效果
- Android Spanned实现TextView富文本效果(2)--表情
- Android中TextView实现富文本展示效果
- 自定义textview控件实现文字滚动效果
- android TextView 实现自定义文字点击效果
- 自定义TextView 实现文字渐变效果
- 自定义TextView实现文字伞动效果
- Android自定义TextView闪烁文字的效果
- 用TextView实现Rich Text(富文本编辑器)---在同一个TextView中设置不同的字体风格
- 支持富文本插入图片,文本的自定义View
- 创建富文本(文本可以包含文字,图片,自定义节点),c++,cocos2dx
- CodeForces 869B The Eternal Immortality
- 欢迎使用CSDN-markdown编辑器
- 【python 数据处理】分组求和、合并
- 静态成员函数
- Java设计模式_(行为型)_责任链模式
- 自定义TextView,实现图片在文字之前的富文本效果
- mybatis中"#"和"$"的区别
- SpringBoot @Aspect注解详情
- 第一章 策略模式
- JVM中线程的状态转换图
- Maven使用之packing篇
- 认识STL(一):STL概述
- Hack for Cloud Beginner微软黑客松大赛
- 调试 ASP.NET Core 2.0 源代码