一种TextView插入图片的途径(图片的显示高度和文字高度一致)
2015-03-03 15:43
387 查看
实现效果:
控件代码:
import java.lang.ref.WeakReference; import android.content.Context; import android.graphics.Canvas; import android.graphics.Paint; import android.graphics.Rect; import android.graphics.Paint.FontMetricsInt; import android.graphics.drawable.Drawable; import android.text.style.ReplacementSpan; import android.util.Log; public class AdaptiveImageSpan extends ReplacementSpan { private final static String LOG_TAG = "AdaptiveImageSpan"; private Context mContext; private int mResourceId; private int mHeight; private int mLeading; private WeakReference<Drawable> mDrawableRef; public AdaptiveImageSpan(Context context, int resourceId) { mResourceId = resourceId; mContext = context; } @Override public int getSize(Paint paint, CharSequence text, int start, int end, FontMetricsInt fm) { FontMetricsInt tempTm = fm; if (tempTm == null) { tempTm = new FontMetricsInt(); paint.getFontMetricsInt(tempTm); } if (tempTm != null) { Log.i(LOG_TAG, LOG_TAG + " --> getSize - fm.bottom:" + tempTm.bottom + " - fm.top: " + tempTm.top); mLeading = (tempTm.bottom - tempTm.descent) - (tempTm.top - tempTm.ascent); Log.i(LOG_TAG, LOG_TAG + " --> getSize - fm.ascent:" + tempTm.ascent + " - fm.descent: " + tempTm.descent + " - fm.leading: " + tempTm.leading + " - leading: " + mLeading); mHeight = tempTm.descent - tempTm.ascent - mLeading; // mHeight = tempTm.bottom - tempTm.top; if(tempTm.top * tempTm.bottom == 0){ return 0; } } Drawable d = getCachedDrawable(); Rect rect = d.getBounds(); Log.i(LOG_TAG, LOG_TAG + " --> getSize size:" + rect.right + " - height: " + mHeight); return rect.right; } @Override public void draw(Canvas canvas, CharSequence text, int start, int end, float x, int top, int y, int bottom, Paint paint) { Log.i(LOG_TAG, LOG_TAG + " --> draw - text:" + text + " - height: " + (bottom - top)); Drawable b = getCachedDrawable(); canvas.save(); int transY = bottom - b.getBounds().bottom; transY -= mLeading; canvas.translate(x, transY); b.draw(canvas); canvas.restore(); } private Drawable getCachedDrawable() { WeakReference<Drawable> wr = mDrawableRef; Drawable d = null; if (wr != null) d = wr.get(); if (d == null) { d = getDrawable(); mDrawableRef = new WeakReference<Drawable>(d); } return d; } public Drawable getDrawable() { Drawable drawable = null; drawable = mContext.getResources().getDrawable(mResourceId); int width = 0; if (drawable.getIntrinsicHeight() > 0 && mHeight > 0) { width = (int) ((drawable.getIntrinsicWidth() / (drawable.getIntrinsicHeight() * 1.0f)) * mHeight); } Log.i(LOG_TAG, LOG_TAG + " --> getDrawable - mHeight:" + mHeight + " - width: " + width); if (width > 0) { drawable.setBounds(0, 0, width, mHeight); } else { drawable.setBounds(0, 0, drawable.getIntrinsicWidth(), drawable.getIntrinsicHeight()); } return drawable; } }
参考:FL***IEN LAURENT
相关文章推荐
- TextView高度和文字高度不一致!美工给的尺寸在eclipse做完显示不一致的问题!
- 安卓TextView显示图片与文字作为底部菜单
- Android textview获取html文字 + 图片显示
- 用9path图片做背景 button和textview的文字不显示
- 在EditText中插入表情图片 (CharacterStyle&SpannableString),TextView中显示HTML和图片
- TextView中显示文字和图片
- android textview混合显示图片和文字
- Android中TextView显示插入的图片实现方法
- 让TextView中的图片和文字一起水平居中显示
- android TextView如何通过setCompoundDrawables调整文字和图片的位置(文字显示在图片内)?
- 图文混排中,在textView的指定光标下插入文字或图片
- TextView显示插入的图片
- Android常用UI编程_TextView显示图片和文字(包含超链接)
- android launcher主菜单显示图片和文字之BubbleTextView
- android中Textview 和图片同时显示时,文字省略号显示,图片自动靠到右边
- android开发之TextView显示加载的文字和图片
- TextView 文字加图片显示效果
- TextView中显示不同颜色的文字和图片
- TextView文本显示图片和文字,竖直滚动显示
- android 文字图片混排,在一个TextView中显示文字穿插图片,文字变色效果