在 TextView 中顯示 html ,需要使用 ImageGetter 解析標簽。ImageGetter 是一個接口,需要實現里面的方法。
public DrawablegetDrawable(String source);
創建一個 Drawable 對象,保留引用并返回,異步請求網絡,獲取圖片,獲取到圖片后,轉換為 Bitmap ,交給 Drawable 對象進行繪制。– 自定義 Drawable 對象
static class MyDrawable extends BitmapDrawable{ BitmapmBitmap; public MyDrawable() { super(); } @Override public void draw(Canvascanvas) { super.draw(canvas); if(mBitmap != null) { canvas.drawBitmap(mBitmap,0,0,getPaint()); } } public void setBitmap(Bitmapbitmap) { mBitmap = bitmap; }}
實現 getDrawable() 方法
MyImageGetter 有一個 TextView 成員,用于保存用于圖片顯示的 TextView ,加載完成后,調用 TextView 的 invalidate() 方法刷新視圖。
public MyImageGetter(TextViewview){ mContainer = view;} @Overridepublic DrawablegetDrawable(String s){ // 初始化占位 Drawable final MyDrawabledrawable = new MyDrawable(); // 初始化請求對象 LocalHostModelmodel = new LocalHostModel(); // 設置回調函數 model.setImageListener(new LocalHostModel.OnRequestImageListener() { @Override public void onSuccess(Bitmapbitmap) { // 處理 bitmap 刷新視圖 drawable.setBitmap(bitmap); int height = bitmap.getHeight(); int width = bitmap.getWidth(); drawable.setBounds(0, 0, width, height); mContainer.invalidate(); } @Override public void onFailed(String msg) { Log.i(TAG, "onFailed: " + msg); } }); // 請求圖片 model.requestImage(s); return drawable;}
Drawable.setBounds(Rect) 方法
The setBounds(Rect) method must be called to tell the Drawable where it is drawn and how large it should be. All Drawables should respect the requested size, often simply by scaling their imagery. A client can find the preferred size for some Drawables with the getIntrinsicHeight() and getIntrinsicWidth() methods.
Drawable.setBounds(Rect) 用于設置繪圖的位置,和繪圖的區域。
Canvas.drawBitmap()bitmap 要通過 Drawable.draw(Canvas) 函數繪制,可以通過 Canvas.drawBitmap() 控制 bitmap 在Drawable 中的顯示位置。
private void handleBitmap(MyDrawabledrawable, BitmapsrcBitmap){ // 獲取原 bitmap 的大小 int srcHeight = srcBitmap.getHeight(); int srcWidth = srcBitmap.getWidth(); // drawable 可用的顯示寬度 int containerPadding = mContainer.getPaddingLeft() + mContainer.getPaddingRight(); int drawableWidth = mContainer.getMeasuredWidth() - containerPadding; int dstWidth = drawableWidth - mPadding * 2; // 圖片較小,不縮放 if (dstWidth >= srcWidth) { drawable.setBitmap(srcBitmap); // 設置 drawable 區域 int drawableHeight = srcHeight + 2 * mPadding; drawable.setBounds(0, 0, drawableWidth, drawableHeight); // 設置 bitmap 繪制區域 int left = (drawableWidth - srcWidth) / 2; int top = mPadding; Rectrect = new Rect(left, top, left + srcWidth, top + srcHeight); drawable.setDstRect(rect); } else { // 圖片縮放矩陣 Matrixmatrix = new Matrix(); float scale = (float) dstWidth / srcWidth; matrix.postScale(scale, scale); // 轉換 bitmap 為適合 drawable 的大小 BitmapdstBitmap = Bitmap.createBitmap(srcBitmap, 0, 0, srcWidth, srcHeight, matrix, false); drawable.setBitmap(dstBitmap); // 設置 drawable 區域 int dstHeight = srcHeight * drawableWidth / srcWidth; int drawableHeight = dstHeight + 2 * mPadding; drawable.setBounds(0, 0, drawableWidth, drawableHeight); // 設置 bitmap 繪制區域 int left = mPadding; int top = mPadding; Rectrect = new Rect(left, top, left + dstWidth, top + dstHeight); drawable.setDstRect(rect); }}static class MyDrawable extends BitmapDrawable{ BitmapmBitmap; RectmSrcRect; RectmDstRect; public MyDrawable(Resourcesres, Bitmapbitmap) { super(res, bitmap); } public void setSrcRect(RectsrcRect) { mSrcRect = srcRect; } public void setDstRect(RectdstRect) { mDstRect = dstRect; } @Override public void draw(Canvascanvas) { super.draw(canvas); // 繪制 bitmap if (mBitmap != null) { if (mSrcRect != null && mDstRect != null) { canvas.drawBitmap(mBitmap, mSrcRect, mDstRect, getPaint()); } else if (mDstRect != null) { int width = mBitmap.getWidth(); int height = mBitmap.getHeight(); RectsrcRect = new Rect(0, 0, width, height); canvas.drawBitmap(mBitmap, srcRect, mDstRect, getPaint()); } else { canvas.drawBitmap(mBitmap, 0, 0, getPaint()); } } } public void setBitmap(Bitmapbitmap) { mBitmap = bitmap; }}
聲明:本網頁內容旨在傳播知識,若有侵權等問題請及時與本網聯系,我們將在第一時間刪除處理。TEL:177 7030 7066 E-MAIL:11247931@qq.com