Code example for TextPaint

Methods: setColorsetShadowLayersetTextSizesetTypefacegetDesiredWidth

0
		if (valuePaint == null) {
			if (typefaceWeight == Typeface.BOLD) {
				valuePaint = new TextPaint(Paint.ANTI_ALIAS_FLAG
						| Paint.FAKE_BOLD_TEXT_FLAG | Paint.DITHER_FLAG);
			} else { 
				valuePaint = new TextPaint(Paint.ANTI_ALIAS_FLAG
						| Paint.DITHER_FLAG);
			} 
			//valuePaint.density = getResources().getDisplayMetrics().density; 
			valuePaint.setTextSize(textSize);
			valuePaint.setShadowLayer(0.5f, 0, 0.5f, 0xFFFFFFFF);
			valuePaint.setTypeface(typeface);
			valuePaint.setColor((textColor == NOVAL) ? ITEMS_TEXT_COLOR : textColor);
		} 
 
		if (centerDrawable == null) {
			centerDrawable = getWheelValDrawable(); //getContext().getResources().getDrawable(R.drawable.wheel_val);
		} 
 
		if (topShadow == null) {
			topShadow = new GradientDrawable(Orientation.TOP_BOTTOM, SHADOWS_COLORS);
		} 
 
		if (bottomShadow == null) {
			bottomShadow = new GradientDrawable(Orientation.BOTTOM_TOP, SHADOWS_COLORS);
		} 
 
		//setBackgroundResource(R.drawable.wheel_bg); 
		setBackgroundDrawable(getWheelBackground());
	} 
	 
	/** 
	 * Direct programmatic creation of drawables (instead of R & res files) 
	 */ 
	private int dipToInt(float dips) 
	{ 
		return Math.round(TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, dips, getResources().getDisplayMetrics()));
	} 
	private GradientDrawable makeGradientDrawable(Orientation orientation, 
			int startColor, int centerColor, int endColor, float strokeDips, int strokeColor)
	{ 
		GradientDrawable gd = makeGradientDrawable(orientation, startColor, centerColor, endColor);
		gd.setStroke(dipToInt(strokeDips), strokeColor);
		return gd;
	} 
	 
	private GradientDrawable makeGradientDrawable(Orientation orientation, 
			int startColor, int centerColor, int endColor)
	{ 
		int[] colors = new int[]{startColor, centerColor, endColor};
		GradientDrawable gd = new GradientDrawable(orientation, colors);
		return gd;
	} 
	 
	private Drawable getWheelValDrawable()
	{ 
		return makeGradientDrawable(Orientation.BOTTOM_TOP, 
				Color.parseColor("#70222222"), Color.parseColor("#70222222"), Color.parseColor("#70EEEEEE"), 1f, Color.parseColor("#70333333"));
	} 
	 
	private Drawable getWheelBackground()
	{ 
		Drawable item0 = makeGradientDrawable(Orientation.BOTTOM_TOP, Color.parseColor("#333333"), Color.parseColor("#DDDDDD"), Color.parseColor("#333333"), 1f, Color.parseColor("#FF333333"));
		Drawable item1 = makeGradientDrawable(Orientation.BOTTOM_TOP, Color.parseColor("#AAAAAA"), Color.parseColor("#FFFFFF"), Color.parseColor("#AAAAAA"));
		LayerDrawable ld = new LayerDrawable(new Drawable[]{item0, item1});
		ld.setLayerInset(1, dipToInt(4f), dipToInt(1f), dipToInt(4f), dipToInt(1f));
		return ld;
	} 
 
	/** 
	 * Calculates desired height for layout 
	 *  
	 * @param layout 
	 *            the source layout 
	 * @return the desired layout height 
	 */ 
	private int getDesiredHeight(Layout layout) {
		if (layout == null) {
			return 0; 
		} 
 
		int linecount = layout.getLineCount();
		int desired = layout.getLineTop(linecount) - getItemOffset() * 2
				- getAdditionalItemHeight(); 
 
		// Check against our minimum height 
		desired = Math.max(desired, getSuggestedMinimumHeight());
 
		return desired;
	} 
 
	/** 
	 * Builds text depending on current value 
	 *  
	 * @return the text 
	 */ 
	private String buildText() {
		WheelAdapter adapter = getAdapter();
		StringBuilder itemsText = new StringBuilder();
		int addItems = visibleItems / 2;
		for (int i = currentItem - addItems; i < currentItem; i++) {
			if (i >= 0 && adapter != null) {
				String text = adapter.getItem(i);
				if (text != null) {
					itemsText.append(text);
				} 
			} 
			itemsText.append("\n");
		} 
		 
		itemsText.append("\n"); // here will be current value
		 
		for (int i = currentItem + 1; i <= currentItem + addItems; i++) {
			if (adapter != null && i < adapter.getItemsCount()) {
				String text = adapter.getItem(i);
				if (text != null) {
					itemsText.append(text);
				} 
			} 
			if (i < currentItem + addItems) {
				itemsText.append("\n");
			} 
		} 
		return itemsText.toString();
	} 
 
	/** 
	 * Returns the max item length that can be present 
	 * @return the max length 
	 */ 
	private int getMaxTextLength() { 
		WheelAdapter adapter = getAdapter();
		if (adapter == null) {
			return 0; 
		} 
		 
		int adapterLength = adapter.getMaximumLength();
		if (adapterLength > 0) {
			return adapterLength;
		} 
 
		String maxText = null;
		int addItems = visibleItems / 2;
		for (int i = Math.max(currentItem - addItems, 0);
				i < Math.min(currentItem + visibleItems, adapter.getItemsCount()); i++) {
			String text = adapter.getItem(i);
			if (text != null && (maxText == null || maxText.length() < text.length())) {
				maxText = text;
			} 
		} 
 
		return maxText != null ? maxText.length() : 0;
	} 
 
	/** 
	 * Calculates control width and creates text layouts 
	 * @param widthSize the input layout width 
	 * @param mode the layout mode 
	 * @return the calculated control width 
	 */ 
	private int calculateLayoutWidth(int widthSize, int mode) {
		initResourcesIfNecessary(); 
 
		int width = widthSize;
 
		int maxLength = getMaxTextLength();
		if (maxLength > 0) {
			float textWidth = FloatMath.ceil(Layout.getDesiredWidth("0", itemsPaint));
			itemsWidth = (int) (maxLength * textWidth);
		} else { 
			itemsWidth = 0;
		} 
		itemsWidth += ADDITIONAL_ITEMS_SPACE; // make it some more
 
		labelWidth = 0;
		if (label != null && label.length() > 0) {
			labelWidth = (int) FloatMath.ceil(Layout.getDesiredWidth(label, valuePaint));
		} 
 
		boolean recalculate = false;
		if (mode == MeasureSpec.EXACTLY) {
			width = widthSize;
Contextual code suggestions in your IDE  Get Codota for Java