我想獲得用戶的縮寫,對一個ImageView的設置位圖與一個或兩個characteres,一個圈子裏面,像:如何使用畫布從圓圈內的字符串文本創建位圖?
我已經嘗試了一些方法來畫一個圓和文字在畫布上,但它不能正常工作。我怎樣才能做到這一點?
我想獲得用戶的縮寫,對一個ImageView的設置位圖與一個或兩個characteres,一個圈子裏面,像:如何使用畫布從圓圈內的字符串文本創建位圖?
我已經嘗試了一些方法來畫一個圓和文字在畫布上,但它不能正常工作。我怎樣才能做到這一點?
我用TextDrawable庫,並發現它是非常有用的。 這個輕量級的圖書館提供了像Gmail應用程序一樣的帶有字母/文字的圖像。它擴展了Drawable類,因此可以與現有/自定義/網絡ImageView類一起使用。還包括一個流暢的界面,用於創建繪圖和可定製的ColorGenerator。
你應該寫一些關於圖書館的話。 – CSchulz
真的很好的lib!謝啦。 –
可以使用
public Bitmap createImage(int width, int height, int color, String name) {
Bitmap bitmap = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888);
Canvas canvas = new Canvas(bitmap);
Paint paint2 = new Paint();
paint2.setColor(color);
canvas.drawRect(0F, 0F, (float) width, (float) height, paint2);
Paint paint = new Paint();
paint.setColor(Color.WHITE);
paint.setTextSize(72);
paint.setTextScaleX(1);
canvas.drawText(name, 75 - 25, 75 + 20, paint);
return bitmap;
}
圖片瀏覽設置圈的ImageView
ImageView imageView = (ImageView)findViewById(R.id.img);
imageView.setImageBitmap(ImageHelper.createImageRounded(getApplicationContext(), 150, 150, "S"));
使用畫布
回合映像創建方法public static Bitmap createImageRounded(Context context, int width, int height, String name)
{
Bitmap output = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888);
Canvas canvas = new Canvas(output);
Paint paintCicle = new Paint();
Paint paintText = new Paint();
Rect rect = new Rect(0, 0, width, height);
RectF rectF = new RectF(rect);
float density = context.getResources().getDisplayMetrics().density;
float roundPx = 100*density;
paintCicle.setColor(Color.LTGRAY);
paintCicle.setAntiAlias(true);
canvas.drawARGB(0, 0, 0, 0);
// Set Border For Circle
paintCicle.setStyle(Paint.Style.STROKE);
paintCicle.setStrokeWidth(4.0f);
canvas.drawRoundRect(rectF, roundPx, roundPx, paintCicle);
paintText.setColor(Color.GRAY);
paintText.setTextSize(72);
canvas.drawText(name, 75 - 23, 75 + 25, paintText);
return output;
}
它很簡單,嘗試使用畫出圓圈,並設置爲你的textview背景。有選擇ImageView的任何具體原因? –
Madhu
@Madhu這是因爲如果用戶有一個圖片網址,我必須顯示它,否則我必須顯示這樣的首字母。 –
你可以檢查內部權利,如果用戶返回網址,然後顯示imageview,否則你cna顯示文本視圖 – Madhu