Android的TextView中顯示圖片和映像的方法

來源:互聯網
上載者:User

實現效果如下:(感謝老羅視頻的講解)  視頻:猛擊這裡

沒事多看看 不同過的實現方案還是不錯的。就是在Textview中嵌套點圖片顯示出來。效果不錯。有人該說,你這傻逼,閑著蛋疼了一會用著用那的。呵呵。其實呢這種方法可以避免布局中混亂嵌套的問題,你只需要搞個Textview和複製點圖片進Drawable檔案夾下即可實現一些功能了。不需要那麼麻煩。要不然你得嵌很多布局也很麻煩。

 

代碼如下:

package lixiaodaoaaa.com.ui;import java.lang.reflect.Field;import android.app.Activity;import android.graphics.Color;import android.graphics.drawable.Drawable;import android.os.Bundle;import android.text.Html;import android.text.Html.ImageGetter;import android.widget.TextView;/*** * by lixiaodaoaaa Tecent_weibo: http://t.qq.com/lixiaodaoaaa * xinlang_Weibo:http://weibo.com/lixiaodaoaaa *  * @author lixiaodaoaaa *  */public class MainActivity extends Activity{/** * 此方法是通過資源的名字 獲得資源的id;比如在Drawable檔案夾下的face_01.png 獲得face_01.png的id;;;;; * 主要用到了R.drawable.class.getField 通過反射的方法。 *  * @param name * @return */public int gerResouceIdByName(String name){try{Field field = R.drawable.class.getField(name);return Integer.parseInt(field.get(null).toString());} catch (Exception e){System.out.println("parse fialed  return 0 is here");}return 0;}@Overridepublic void onCreate(Bundle savedInstanceState){super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);initTextView();}private void initTextView(){TextView textView = (TextView) findViewById(R.id.tv_test01);textView.setBackgroundColor(Color.WHITE);textView.setTextColor(Color.BLACK);textView.setTextSize(20);String html1 = "映像1 <img src='face_01' /><p>";html1 += "映像2 <img src='face_02' /><p>";html1 += "映像3 <img src='face_03' /><p><p>";// <p>這裡是段落標記。讓他在斷行符號換行下面顯示;;;html1 += "映像4 <a href='http://weibo.com/lixiaodaoaaa'><img src='face_04' /></a><p>";html1 += "映像5 <img src='face_05' />";CharSequence charSequence = Html.fromHtml(html1, new ImageGetter(){@Overridepublic Drawable getDrawable(String name){int currentDrawableId = gerResouceIdByName(name);// 通過名字得到資源的id;Drawable drawable = getResources().getDrawable(currentDrawableId);// 必須對返回的drawable 設定 bounds 要不然不會顯示;;;if (name.equals("face_03")){drawable.setBounds(0, 0, drawable.getIntrinsicWidth() / 3, drawable.getIntrinsicHeight() / 3);}// 如果是圖片03的話我們 將進行簡單的縮放,設定為它原來的一半大小;;;else if (name.equals("face_04")){drawable.setBounds(0, 0, drawable.getIntrinsicWidth() * 2, drawable.getIntrinsicHeight() * 2);// 如果是圖表4的話我們將他倍寬倍高 設定放大;;;} else{drawable.setBounds(0, 0, drawable.getIntrinsicWidth(), drawable.getIntrinsicHeight());}return drawable;}}, null);textView.setText(charSequence);}}

源碼:猛擊這裡下載

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.