2015-11-04 60 views
2

我想获得用户的缩写,对一个ImageView的设置位图与一个或两个characteres,一个圈子里面,像:如何使用画布从圆圈内的字符串文本创建位图?

enter image description here

我已经尝试了一些方法来画一个圆和文字在画布上,但它不能正常工作。我怎样才能做到这一点?

+0

它很简单,尝试使用画出圆圈,并设置为你的textview背景。有选择ImageView的任何具体原因? – Madhu

+1

@Madhu这是因为如果用户有一个图片网址,我必须显示它,否则我必须显示这样的首字母。 –

+0

你可以检查内部权利,如果用户返回网址,然后显示imageview,否则你cna显示文本视图 – Madhu

回答

3

我用TextDrawable库,并发现它是非常有用的。 这个轻量级的图书馆提供了像Gmail应用程序一样的带有字母/文字的图像。它扩展了Drawable类,因此可以与现有/自定义/网络ImageView类一起使用。还包括一个流畅的界面,用于创建绘图和可定制的ColorGenerator。

Click here for example on github

+0

你应该写一些关于图书馆的话。 – CSchulz

+0

真的很好的lib!谢啦。 –

2

可以使用

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; 
    } 
0

图片浏览设置圈的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; 
} 
相关问题