android圖片上添加文字

來源:互聯網
上載者:User

1.

原圖:

2.主要代碼

public class MainActivity extends Activity {ImageView img;private Bitmap imgMarker;private int width,height;   //圖片的高度和寬頻private Bitmap imgTemp;  //臨時標記圖@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);img = (ImageView) findViewById(R.id.imt_test);imgMarker = BitmapFactory.decodeResource(getResources(), R.drawable.icon_tag);width = imgMarker.getWidth();height = imgMarker.getHeight();img.setBackgroundDrawable(createDrawable('A'));}// 穿件帶字母的標記圖片private Drawable createDrawable(char letter) {imgTemp = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888);Canvas canvas = new Canvas(imgTemp);Paint paint = new Paint(); // 建立畫筆paint.setDither(true);paint.setFilterBitmap(true);Rect src = new Rect(0, 0, width, height);Rect dst = new Rect(0, 0, width, height);canvas.drawBitmap(imgMarker, src, dst, paint);Paint textPaint = new Paint(Paint.ANTI_ALIAS_FLAG| Paint.DEV_KERN_TEXT_FLAG);textPaint.setTextSize(20.0f);textPaint.setTypeface(Typeface.DEFAULT_BOLD); // 採用預設的寬度textPaint.setColor(Color.WHITE);canvas.drawText(String.valueOf(letter), width /2-5, height/2+5,textPaint);canvas.save(Canvas.ALL_SAVE_FLAG);canvas.restore();return (Drawable) new BitmapDrawable(getResources(), imgTemp);}@Overridepublic boolean onCreateOptionsMenu(Menu menu) {// Inflate the menu; this adds items to the action bar if it is present.getMenuInflater().inflate(R.menu.activity_main, menu);return true;}}

測試通過,可以使用

源碼: 點擊開啟連結

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.