Android UI设计--新闻或信息条数的实现

例如短信或者新闻的条数经常做在图片之上,实现方式如下,可以根据不同的图片大小进行调节

    private Bitmap generateNumberIcon(int resId, int newsCount){

    	Bitmap icon = BitmapFactory.decodeResource(getResources(), resId);

    	int iconSize = icon.getWidth();

    	Bitmap mBitmap = Bitmap.createBitmap(iconSize, iconSize, Config.ARGB_8888);

    	Canvas canvas = new Canvas(mBitmap);

    	

    	Paint paint = new Paint();

    	paint.setDither(true);

    	paint.setFilterBitmap(true);

    	Rect src = new Rect(0, 0, iconSize, iconSize);

    	Rect dst = new Rect(0, 0,iconSize, iconSize);

    	canvas.drawBitmap(icon, src, dst, paint);	

    	Paint countPaint = new Paint(Paint.ANTI_ALIAS_FLAG|Paint.DEV_KERN_TEXT_FLAG);

    	countPaint.setColor(getResources().getColor(R.color.red));

    	countPaint.setStyle(Paint.Style.STROKE);

    	countPaint.setTypeface(Typeface.DEFAULT_BOLD);

    	

    	countPaint.setTextSize(iconSize*2/3f);

    	canvas.drawText(String.valueOf(newsCount),iconSize/4 , iconSize*5/6, countPaint);

    	return mBitmap;	

    }

  参考效果:

Android UI设计--新闻或信息条数的实现

你可能感兴趣的:(android ui)