List of usage examples for android.graphics Path Path
public Path()
From source file:com.astuetz.PagerSlidingTabStripCustom.java
@Override protected void onDraw(Canvas canvas) { super.onDraw(canvas); if (isInEditMode() || tabCount == 0) { return;/*w w w . ja va 2s . co m*/ } final int height = getHeight(); // draw indicator line rectPaint.setColor(indicatorColor); // default: line below current tab View currentTab = tabsContainer.getChildAt(currentPosition); float lineLeft = currentTab.getLeft(); float lineRight = currentTab.getRight(); // if there is an offset, start interpolating left and right coordinates between current and next tab if (currentPositionOffset > 0f && currentPosition < tabCount - 1) { View nextTab = tabsContainer.getChildAt(currentPosition + 1); final float nextTabLeft = nextTab.getLeft(); final float nextTabRight = nextTab.getRight(); lineLeft = (currentPositionOffset * nextTabLeft + (1f - currentPositionOffset) * lineLeft); lineRight = (currentPositionOffset * nextTabRight + (1f - currentPositionOffset) * lineRight); } canvas.drawRect(lineLeft, height - indicatorHeight, lineRight, height, rectPaint); //draw triangle float bianchang = indicatorHeight + 10; float x1 = (lineRight + lineLeft) / 2; float y1 = height - bianchang; float x2 = x1 + bianchang * 2; float y2 = y1 + bianchang; float x3 = x1 - bianchang * 2; float y3 = y1 + bianchang; Path path = new Path(); path.moveTo(x1, y1); path.lineTo(x2, y2); path.lineTo(x3, y3); path.close(); canvas.drawPath(path, rectPaint); // draw underline tabcontent rectPaint.setColor(underlineColor); canvas.drawRect(0, height - underlineHeight, tabsContainer.getWidth(), height, rectPaint); // draw divider tab dividerPaint.setColor(dividerColor); for (int i = 0; i < tabCount - 1; i++) { View tab = tabsContainer.getChildAt(i); canvas.drawLine(tab.getRight(), dividerPadding, tab.getRight(), height - dividerPadding, dividerPaint); } }
From source file:net.networksaremadeofstring.rhybudd.RhybuddDock.java
private void drawGaugeNeedle(Canvas canvas, int count, int Scale) { canvas.save(Canvas.MATRIX_SAVE_FLAG); float divisor = 360.0f / Scale; canvas.rotate((float) (divisor * count), 100, 100); //Inside/*from w ww . j a va2 s. co m*/ Paint needleInsidePaint = new Paint(); needleInsidePaint.setStyle(Paint.Style.FILL_AND_STROKE); needleInsidePaint.setColor(Color.WHITE); needleInsidePaint.setStrokeWidth(4); needleInsidePaint.setAntiAlias(true); Paint needleEdgePaint = new Paint(); needleEdgePaint.setStyle(Paint.Style.STROKE); needleEdgePaint.setColor(Color.DKGRAY); needleEdgePaint.setStrokeWidth(0.5f); needleEdgePaint.setAntiAlias(true); canvas.drawOval(new RectF(95, 95, 105, 105), needleInsidePaint); canvas.drawOval(new RectF(95, 96, 105, 105), needleEdgePaint); Path needleInside = new Path(); needleInside.moveTo(98, 98); needleInside.lineTo(100, 20); needleInside.lineTo(102, 102); canvas.drawPath(needleInside, needleInsidePaint); Path needleEdge = new Path(); needleInside.moveTo(99, 99); needleInside.lineTo(99, 19); needleInside.lineTo(103, 103); canvas.drawPath(needleEdge, needleEdgePaint); canvas.restore(); }
From source file:com.gx.appstore.lib.PagerSlidingTabStrip.java
@Override protected void onDraw(Canvas canvas) { super.onDraw(canvas); if (isInEditMode() || tabCount == 0) { return;/*ww w . j a v a 2 s.c om*/ } final int height = getHeight(); // draw indicator line rectPaint.setColor(indicatorColor); // default: line below current tab View currentTab = tabsContainer.getChildAt(currentPosition); float lineLeft = currentTab.getLeft(); float lineRight = currentTab.getRight(); // if there is an offset, start interpolating left and right coordinates between current and next tab if (currentPositionOffset > 0f && currentPosition < tabCount - 1) { View nextTab = tabsContainer.getChildAt(currentPosition + 1); final float nextTabLeft = nextTab.getLeft(); final float nextTabRight = nextTab.getRight(); lineLeft = (currentPositionOffset * nextTabLeft + (1f - currentPositionOffset) * lineLeft); lineRight = (currentPositionOffset * nextTabRight + (1f - currentPositionOffset) * lineRight); } // // /** x1 = lineLeft+(lineRight-lineLeft)/2; y1 = height - indicatorHeight; x2 = x1 + triangleWidth/2; y2 = height; x3 = x1 - triangleWidth/2; y3 = height */ Path path = new Path(); float x1 = lineLeft + (lineRight - lineLeft) / 2; float y1 = height - indicatorHeight; int triangleWidth = 30; float x2 = x1 + triangleWidth / 2; float y2 = height; float x3 = x1 - triangleWidth / 2; float y3 = height; path.moveTo(x1, y1); path.lineTo(x2, y2); path.lineTo(x3, y3); path.lineTo(x1, y1); // canvas.drawPath(path, rectPaint); canvas.drawRect(lineLeft, height - indicatorHeight, lineRight, height, rectPaint); // draw underline rectPaint.setColor(underlineColor); canvas.drawRect(0, height - underlineHeight, tabsContainer.getWidth(), height, rectPaint); // draw divider dividerPaint.setColor(dividerColor); for (int i = 0; i < tabCount - 1; i++) { View tab = tabsContainer.getChildAt(i); canvas.drawLine(tab.getRight(), dividerPadding, tab.getRight(), height - dividerPadding, dividerPaint); } }
From source file:wm.xmwei.ui.view.indicator.PagerSlidingTabIndicator.java
@Override protected void onDraw(Canvas canvas) { super.onDraw(canvas); if (isInEditMode() || tabCount == 0) { return;// w w w . ja va2 s.co m } final int height = getHeight(); final int width = getWidth(); // draw indicator line //rectPaint.setColor(indicatorColor); // default: line below current tab View currentTab = tabsContainer.getChildAt(currentPosition); float lineLeft = currentTab.getLeft(); float lineRight = currentTab.getRight(); // if there is an offset, start interpolating left and right coordinates between current and next tab if (currentPositionOffset > 0f && currentPosition < tabCount - 1) { View nextTab = tabsContainer.getChildAt(currentPosition + 1); nextTab.setBackgroundResource(Color.TRANSPARENT); final float nextTabLeft = nextTab.getLeft(); final float nextTabRight = nextTab.getRight(); lineLeft = (currentPositionOffset * nextTabLeft + (1f - currentPositionOffset) * lineLeft); lineRight = (currentPositionOffset * nextTabRight + (1f - currentPositionOffset) * lineRight); } /* canvas.drawRect(lineLeft, height - indicatorHeight, lineRight, height, rectPaint);*/ float center = (lineLeft + lineRight) / 2; // ,?? // Path path = new Path(); //path.moveTo(center,height - indicatorHeight);// BigDecimal a = BigDecimal.valueOf(1.736); BigDecimal b = BigDecimal.valueOf(3); BigDecimal bigDecimal = a.divide(b, BigDecimal.ROUND_HALF_UP); float result = bigDecimal.floatValue() * indicatorHeight; //path.lineTo(center-result-2, height); // path.lineTo(center+result+2, height); //path.close(); // ?? // canvas.drawPath(path, rectPaint); Path path2 = new Path(); path2.moveTo(0, 0);// path2.lineTo(0, height); path2.lineTo(center - result - 5, height); path2.lineTo(center, height - indicatorHeight); path2.lineTo(center + result + 5, height); path2.lineTo(tabsContainer.getWidth(), height); path2.lineTo(tabsContainer.getWidth(), 0); path2.close(); // ?? canvas.drawPath(path2, bgPaint); // draw underline /*rectPaint.setColor(underlineColor); canvas.drawRect(0, height - underlineHeight, tabsContainer.getWidth(), height, rectPaint);*/ // draw divider /* dividerPaint.setColor(dividerColor); for (int i = 0; i < tabCount - 1; i++) { View tab = tabsContainer.getChildAt(i); canvas.drawLine(tab.getRight(), dividerPadding, tab.getRight(), height - dividerPadding, dividerPaint); }*/ }
From source file:com.jakewharton.android.viewpagerindicator.TitlePageIndicator.java
@Override protected void onDraw(Canvas canvas) { super.onDraw(canvas); //Calculate views bounds ArrayList<RectF> bounds = calculateAllBounds(mPaintText); final int count = mViewPager.getAdapter().getCount(); final int countMinusOne = count - 1; final float halfWidth = getWidth() / 2f; final int left = getLeft(); final float leftClip = left + mClipPadding; final int width = getWidth(); final int height = getHeight(); final int right = left + width; final float rightClip = right - mClipPadding; int page = mCurrentPage; float offsetPercent; if (mCurrentOffset <= halfWidth) { offsetPercent = 1.0f * mCurrentOffset / width; } else {//from w w w .j a v a2 s .com page += 1; offsetPercent = 1.0f * (width - mCurrentOffset) / width; } final boolean currentSelected = (offsetPercent <= SELECTION_FADE_PERCENTAGE); final boolean currentBold = (offsetPercent <= BOLD_FADE_PERCENTAGE); final float selectedPercent = (SELECTION_FADE_PERCENTAGE - offsetPercent) / SELECTION_FADE_PERCENTAGE; //Verify if the current view must be clipped to the screen RectF curPageBound = bounds.get(mCurrentPage); float curPageWidth = curPageBound.right - curPageBound.left; if (curPageBound.left < leftClip) { //Try to clip to the screen (left side) clipViewOnTheLeft(curPageBound, curPageWidth, left); } if (curPageBound.right > rightClip) { //Try to clip to the screen (right side) clipViewOnTheRight(curPageBound, curPageWidth, right); } //Left views starting from the current position if (mCurrentPage > 0) { for (int i = mCurrentPage - 1; i >= 0; i--) { RectF bound = bounds.get(i); //Is left side is outside the screen if (bound.left < leftClip) { float w = bound.right - bound.left; //Try to clip to the screen (left side) clipViewOnTheLeft(bound, w, left); //Except if there's an intersection with the right view RectF rightBound = bounds.get(i + 1); //Intersection if (bound.right + mTitlePadding > rightBound.left) { bound.left = rightBound.left - w - mTitlePadding; bound.right = bound.left + w; } } } } //Right views starting from the current position if (mCurrentPage < countMinusOne) { for (int i = mCurrentPage + 1; i < count; i++) { RectF bound = bounds.get(i); //If right side is outside the screen if (bound.right > rightClip) { float w = bound.right - bound.left; //Try to clip to the screen (right side) clipViewOnTheRight(bound, w, right); //Except if there's an intersection with the left view RectF leftBound = bounds.get(i - 1); //Intersection if (bound.left - mTitlePadding < leftBound.right) { bound.left = leftBound.right + mTitlePadding; bound.right = bound.left + w; } } } } //Now draw views for (int i = 0; i < count; i++) { //Get the title RectF bound = bounds.get(i); //Only if one side is visible if ((bound.left > left && bound.left < right) || (bound.right > left && bound.right < right)) { final boolean currentPage = (i == page); //Only set bold if we are within bounds mPaintText.setFakeBoldText(currentPage && currentBold && mBoldText); //Draw text as unselected mPaintText.setColor(mColorText); canvas.drawText(mTitleProvider.getTitle(i), bound.left, bound.bottom, mPaintText); //If we are within the selected bounds draw the selected text if (currentPage && currentSelected) { mPaintText.setColor(mColorSelected); mPaintText.setAlpha((int) ((mColorSelected >>> 24) * selectedPercent)); canvas.drawText(mTitleProvider.getTitle(i), bound.left, bound.bottom, mPaintText); } } } //Draw the footer line mPath = new Path(); mPath.moveTo(0, height - mFooterLineHeight); mPath.lineTo(width, height - mFooterLineHeight); mPath.close(); canvas.drawPath(mPath, mPaintFooterLine); switch (mFooterIndicatorStyle) { case Triangle: mPath = new Path(); mPath.moveTo(halfWidth, height - mFooterLineHeight - mFooterIndicatorHeight); mPath.lineTo(halfWidth + mFooterIndicatorHeight, height - mFooterLineHeight); mPath.lineTo(halfWidth - mFooterIndicatorHeight, height - mFooterLineHeight); mPath.close(); canvas.drawPath(mPath, mPaintFooterIndicator); break; case Underline: if (!currentSelected) { break; } RectF underlineBounds = bounds.get(page); mPath = new Path(); mPath.moveTo(underlineBounds.left - mFooterIndicatorUnderlinePadding, height - mFooterLineHeight); mPath.lineTo(underlineBounds.right + mFooterIndicatorUnderlinePadding, height - mFooterLineHeight); mPath.lineTo(underlineBounds.right + mFooterIndicatorUnderlinePadding, height - mFooterLineHeight - mFooterIndicatorHeight); mPath.lineTo(underlineBounds.left - mFooterIndicatorUnderlinePadding, height - mFooterLineHeight - mFooterIndicatorHeight); mPath.close(); mPaintFooterIndicator.setAlpha((int) (0xFF * selectedPercent)); canvas.drawPath(mPath, mPaintFooterIndicator); mPaintFooterIndicator.setAlpha(0xFF); break; } }
From source file:com.androtex.viewpagerindicator.TitlePageIndicator.java
@Override protected void onDraw(Canvas canvas) { super.onDraw(canvas); //Calculate views bounds ArrayList<RectF> bounds = calculateAllBounds(mPaintText); final int count = mViewPager.getAdapter().getCount(); final int countMinusOne = count - 1; final float halfWidth = getWidth() / 2f; final int left = getLeft(); final float leftClip = left + mClipPadding; final int width = getWidth(); final int height = getHeight(); final int right = left + width; final float rightClip = right - mClipPadding; int page = mCurrentPage; float offsetPercent; if (mCurrentOffset <= halfWidth) { offsetPercent = 1.0f * mCurrentOffset / width; } else {/*w w w .ja va 2 s. c om*/ page += 1; offsetPercent = 1.0f * (width - mCurrentOffset) / width; } final boolean currentSelected = (offsetPercent <= SELECTION_FADE_PERCENTAGE); final boolean currentBold = (offsetPercent <= BOLD_FADE_PERCENTAGE); final float selectedPercent = (SELECTION_FADE_PERCENTAGE - offsetPercent) / SELECTION_FADE_PERCENTAGE; //Verify if the current view must be clipped to the screen RectF curPageBound = bounds.get(mCurrentPage); float curPageWidth = curPageBound.right - curPageBound.left; if (curPageBound.left < leftClip) { //Try to clip to the screen (left side) clipViewOnTheLeft(curPageBound, curPageWidth, left); } if (curPageBound.right > rightClip) { //Try to clip to the screen (right side) clipViewOnTheRight(curPageBound, curPageWidth, right); } //Left views starting from the current position if (mCurrentPage > 0) { for (int i = mCurrentPage - 1; i >= 0; i--) { RectF bound = bounds.get(i); //Is left side is outside the screen if (bound.left < leftClip) { float w = bound.right - bound.left; //Try to clip to the screen (left side) clipViewOnTheLeft(bound, w, left); //Except if there's an intersection with the right view RectF rightBound = bounds.get(i + 1); //Intersection if (bound.right + mTitlePadding > rightBound.left) { bound.left = rightBound.left - w - mTitlePadding; bound.right = bound.left + w; } } } } //Right views starting from the current position if (mCurrentPage < countMinusOne) { for (int i = mCurrentPage + 1; i < count; i++) { RectF bound = bounds.get(i); //If right side is outside the screen if (bound.right > rightClip) { float w = bound.right - bound.left; //Try to clip to the screen (right side) clipViewOnTheRight(bound, w, right); //Except if there's an intersection with the left view RectF leftBound = bounds.get(i - 1); //Intersection if (bound.left - mTitlePadding < leftBound.right) { bound.left = leftBound.right + mTitlePadding; bound.right = bound.left + w; } } } } //Now draw views for (int i = 0; i < count; i++) { //Get the title RectF bound = bounds.get(i); //Only if one side is visible if ((bound.left > left && bound.left < right) || (bound.right > left && bound.right < right)) { final boolean currentPage = (i == page); //Only set bold if we are within bounds mPaintText.setFakeBoldText(currentPage && currentBold && mBoldText); //Draw text as unselected mPaintText.setColor(mColorText); canvas.drawText(mTitleProvider.getTitle(i), bound.left, bound.bottom + mTopPadding, mPaintText); //If we are within the selected bounds draw the selected text if (currentPage && currentSelected) { mPaintText.setColor(mColorSelected); mPaintText.setAlpha((int) ((mColorSelected >>> 24) * selectedPercent)); canvas.drawText(mTitleProvider.getTitle(i), bound.left, bound.bottom + mTopPadding, mPaintText); } } } //Draw the footer line mPath = new Path(); mPath.moveTo(0, height - mFooterLineHeight / 2f); mPath.lineTo(width, height - mFooterLineHeight / 2f); mPath.close(); canvas.drawPath(mPath, mPaintFooterLine); switch (mFooterIndicatorStyle) { case Triangle: mPath = new Path(); mPath.moveTo(halfWidth, height - mFooterLineHeight - mFooterIndicatorHeight); mPath.lineTo(halfWidth + mFooterIndicatorHeight, height - mFooterLineHeight); mPath.lineTo(halfWidth - mFooterIndicatorHeight, height - mFooterLineHeight); mPath.close(); canvas.drawPath(mPath, mPaintFooterIndicator); break; case Underline: if (!currentSelected) { break; } RectF underlineBounds = bounds.get(page); mPath = new Path(); mPath.moveTo(underlineBounds.left - mFooterIndicatorUnderlinePadding, height - mFooterLineHeight); mPath.lineTo(underlineBounds.right + mFooterIndicatorUnderlinePadding, height - mFooterLineHeight); mPath.lineTo(underlineBounds.right + mFooterIndicatorUnderlinePadding, height - mFooterLineHeight - mFooterIndicatorHeight); mPath.lineTo(underlineBounds.left - mFooterIndicatorUnderlinePadding, height - mFooterLineHeight - mFooterIndicatorHeight); mPath.close(); mPaintFooterIndicator.setAlpha((int) (0xFF * selectedPercent)); canvas.drawPath(mPath, mPaintFooterIndicator); mPaintFooterIndicator.setAlpha(0xFF); break; } }
From source file:com.example.SmartBoard.DrawingView.java
public boolean drawPoint(float x, float y, int action, int color, String mode, int brushSize, String clientID) { mX = x;//from w w w. j a v a 2 s. c o m mY = y; Path path = clientPaths.get(clientID); if (path == null) { path = new Path(); clientPaths.put(clientID, path); } drawPathRecv = path; setDrawPaintRecv(color, mode, brushSize); switch (action) { case MotionEvent.ACTION_DOWN: drawPathRecv.moveTo(x, y); break; case MotionEvent.ACTION_MOVE: drawPathRecv.lineTo(x, y); break; case MotionEvent.ACTION_UP: drawCanvas.drawPath(drawPathRecv, drawPaintSender); drawPathRecv.reset(); break; case CLEAR_SCREEN: drawCanvas.drawColor(Color.WHITE); break; default: //draw nothing } invalidate(); return true; }
From source file:org.florescu.android.rangeseekbar.RangeSeekBar.java
private void init(Context context, AttributeSet attrs) { float barHeight; int thumbNormal = R.drawable.seek_thumb; int thumbPressed = R.drawable.seek_thumb_pressed; int thumbDisabled = R.drawable.seek_thumb_disabled; int thumbShadowColor; int defaultShadowColor = Color.argb(75, 0, 0, 0); int defaultShadowYOffset = PixelUtil.dpToPx(context, 2); int defaultShadowXOffset = PixelUtil.dpToPx(context, 0); int defaultShadowBlur = PixelUtil.dpToPx(context, 2); offset = PixelUtil.dpToPx(context, TEXT_LATERAL_PADDING_IN_DP); textSeperation = PixelUtil.dpToPx(context, DEFAULT_TEXT_SEPERATION_IN_DP); step = DEFAULT_STEP;//www.j a v a 2s . c o m snapTolerance = DEFAULT_SNAP_TOLERANCE_PERCENT / 100; minimumDistance = DEFAULT_MINIMUM_DISTANCE; increments = DEFAULT_INCREMENTS; incrementRanges = DEFAULT_INCREMENT_RANGES; if (attrs == null) { rangeType = DEFAULT_RANGE_TYPE; setRangeToDefaultValues(); mInternalPad = PixelUtil.dpToPx(context, INITIAL_PADDING_IN_DP); barHeight = PixelUtil.dpToPx(context, LINE_HEIGHT_IN_DP); mActiveColor = ACTIVE_COLOR; mDefaultColor = Color.parseColor("#e5e8eb"); mAlwaysActive = true; mShowTextAboveThumbs = true; mTextAboveThumbsColor = Color.WHITE; thumbShadowColor = defaultShadowColor; mThumbShadowXOffset = defaultShadowXOffset; mThumbShadowYOffset = defaultShadowYOffset; mThumbShadowBlur = defaultShadowBlur; mActivateOnDefaultValues = true; } else { TypedArray a = getContext().obtainStyledAttributes(attrs, R.styleable.RangeSeekBar, 0, 0); try { switch (a.getInt(R.styleable.RangeSeekBar_rangeType, 0)) { case 0: rangeType = RangeType.LINEAR; break; case 1: rangeType = RangeType.PREDEFINED; break; case 2: rangeType = RangeType.CUBIC; break; default: rangeType = RangeType.LINEAR; } setRangeValues( extractNumericValueFromAttributes(a, R.styleable.RangeSeekBar_absoluteMinValue, DEFAULT_MINIMUM), extractNumericValueFromAttributes(a, R.styleable.RangeSeekBar_absoluteMaxValue, DEFAULT_MAXIMUM)); mShowTextAboveThumbs = a.getBoolean(R.styleable.RangeSeekBar_valuesAboveThumbs, true); mTextAboveThumbsColor = a.getColor(R.styleable.RangeSeekBar_textAboveThumbsColor, Color.WHITE); mSingleThumb = a.getBoolean(R.styleable.RangeSeekBar_singleThumb, false); mShowLabels = a.getBoolean(R.styleable.RangeSeekBar_showLabels, true); mInternalPad = PixelUtil.dpToPx(context, a.getInt(R.styleable.RangeSeekBar_internalPadding, INITIAL_PADDING_IN_DP)); barHeight = PixelUtil.dpToPx(context, a.getInt(R.styleable.RangeSeekBar_barHeight, LINE_HEIGHT_IN_DP)); mActiveColor = a.getColor(R.styleable.RangeSeekBar_activeColor, ACTIVE_COLOR); mDefaultColor = a.getColor(R.styleable.RangeSeekBar_defaultColor, Color.parseColor("#e5e8eb")); mAlwaysActive = a.getBoolean(R.styleable.RangeSeekBar_alwaysActive, true); customMinValueLabel = a.getString(R.styleable.RangeSeekBar_minValueLabel); customMaxValueLabel = a.getString(R.styleable.RangeSeekBar_maxValueLabel); Drawable normalDrawable = a.getDrawable(R.styleable.RangeSeekBar_thumbNormal); if (normalDrawable != null) { thumbImage = BitmapUtil.drawableToBitmap(normalDrawable); } Drawable disabledDrawable = a.getDrawable(R.styleable.RangeSeekBar_thumbDisabled); if (disabledDrawable != null) { thumbDisabledImage = BitmapUtil.drawableToBitmap(disabledDrawable); } Drawable pressedDrawable = a.getDrawable(R.styleable.RangeSeekBar_thumbPressed); if (pressedDrawable != null) { thumbPressedImage = BitmapUtil.drawableToBitmap(pressedDrawable); } mThumbShadow = a.getBoolean(R.styleable.RangeSeekBar_thumbShadow, false); thumbShadowColor = a.getColor(R.styleable.RangeSeekBar_thumbShadowColor, defaultShadowColor); mThumbShadowXOffset = a.getDimensionPixelSize(R.styleable.RangeSeekBar_thumbShadowXOffset, defaultShadowXOffset); mThumbShadowYOffset = a.getDimensionPixelSize(R.styleable.RangeSeekBar_thumbShadowYOffset, defaultShadowYOffset); mThumbShadowBlur = a.getDimensionPixelSize(R.styleable.RangeSeekBar_thumbShadowBlur, defaultShadowBlur); mActivateOnDefaultValues = a.getBoolean(R.styleable.RangeSeekBar_activateOnDefaultValues, true); } finally { a.recycle(); } } Resources resources = getResources(); int px = (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 18, resources.getDisplayMetrics()); if (thumbImage == null) { thumbImage = Bitmap.createBitmap(px, px, Bitmap.Config.ARGB_8888); Drawable thumbDrawableNormal = getResources().getDrawable(thumbNormal); thumbDrawableNormal.setBounds(0, 0, px, px); thumbDrawableNormal.draw(new Canvas(thumbImage)); } if (thumbPressedImage == null) { Drawable thumbDrawablePressed = getResources().getDrawable(thumbPressed); thumbDrawablePressed.setBounds(0, 0, px, px); thumbPressedImage = Bitmap.createBitmap(px, px, Bitmap.Config.ARGB_8888); thumbDrawablePressed.draw(new Canvas(thumbPressedImage)); } if (thumbDisabledImage == null) { Drawable thumbDrawableDisabled = getResources().getDrawable(thumbDisabled); thumbDrawableDisabled.setBounds(0, 0, px, px); thumbDisabledImage = Bitmap.createBitmap(px, px, Bitmap.Config.ARGB_8888); thumbDrawableDisabled.draw(new Canvas(thumbDisabledImage)); } mThumbHalfWidth = 0.5f * thumbImage.getWidth(); mThumbHalfHeight = 0.5f * thumbImage.getHeight(); setValuePrimAndNumberType(); mTextSize = PixelUtil.spToPx(context, DEFAULT_TEXT_SIZE_IN_SP); mDistanceToTop = PixelUtil.dpToPx(context, DEFAULT_TEXT_DISTANCE_TO_TOP_IN_DP); mTextOffset = !mShowTextAboveThumbs ? 0 : this.mTextSize + PixelUtil.dpToPx(context, DEFAULT_TEXT_DISTANCE_TO_BUTTON_IN_DP) + this.mDistanceToTop; mRect = new RectF(padding, mTextOffset + mThumbHalfHeight - barHeight / 2, getWidth() - padding, mTextOffset + mThumbHalfHeight + barHeight / 2); // make RangeSeekBar focusable. This solves focus handling issues in case EditText widgets are being used along with the RangeSeekBar within ScrollViews. setFocusable(true); setFocusableInTouchMode(true); mScaledTouchSlop = ViewConfiguration.get(getContext()).getScaledTouchSlop(); if (mThumbShadow) { // We need to remove hardware acceleration in order to blur the shadow setLayerType(LAYER_TYPE_SOFTWARE, null); shadowPaint.setColor(thumbShadowColor); shadowPaint.setMaskFilter(new BlurMaskFilter(mThumbShadowBlur, BlurMaskFilter.Blur.NORMAL)); mThumbShadowPath = new Path(); mThumbShadowPath.addCircle(0, 0, mThumbHalfHeight, Path.Direction.CW); } }
From source file:org.onebusaway.android.map.googlemapsv2.StopOverlay.java
/** * Creates a bus stop icon with the given direction arrow, or without a direction arrow if * the direction is NO_DIRECTION// www.jav a 2 s . c o m * * @param direction Bus stop direction, obtained from ObaStop.getDirection() and defined in * constants in this class, or NO_DIRECTION if the stop icon shouldn't have a * direction arrow * @return a bus stop icon bitmap with the arrow pointing the given direction, or with no arrow * if direction is NO_DIRECTION */ private static Bitmap createBusStopIcon(String direction) throws NullPointerException { if (direction == null) { throw new IllegalArgumentException(direction); } Resources r = Application.get().getResources(); Context context = Application.get(); Float directionAngle = null; // 0-360 degrees Bitmap bm; Canvas c; Drawable shape; Float rotationX = null, rotationY = null; // Point around which to rotate the arrow Paint arrowPaintFill = new Paint(); arrowPaintFill.setStyle(Paint.Style.FILL); arrowPaintFill.setAntiAlias(true); if (direction.equals(NO_DIRECTION)) { // Don't draw the arrow bm = Bitmap.createBitmap(mPx, mPx, Bitmap.Config.ARGB_8888); c = new Canvas(bm); shape = ContextCompat.getDrawable(context, R.drawable.map_stop_icon); shape.setBounds(0, 0, bm.getWidth(), bm.getHeight()); } else if (direction.equals(NORTH)) { directionAngle = 0f; bm = Bitmap.createBitmap(mPx, (int) (mPx + mBuffer), Bitmap.Config.ARGB_8888); c = new Canvas(bm); shape = ContextCompat.getDrawable(context, R.drawable.map_stop_icon); shape.setBounds(0, (int) mBuffer, mPx, bm.getHeight()); // Shade with darkest color at tip of arrow arrowPaintFill.setShader(new LinearGradient(bm.getWidth() / 2, 0, bm.getWidth() / 2, mArrowHeightPx, r.getColor(R.color.theme_primary), r.getColor(R.color.theme_accent), Shader.TileMode.MIRROR)); // For NORTH, no rotation occurs - use center of image anyway so we have some value rotationX = bm.getWidth() / 2f; rotationY = bm.getHeight() / 2f; } else if (direction.equals(NORTH_WEST)) { directionAngle = 315f; // Arrow is drawn N, rotate 315 degrees bm = Bitmap.createBitmap((int) (mPx + mBuffer), (int) (mPx + mBuffer), Bitmap.Config.ARGB_8888); c = new Canvas(bm); shape = ContextCompat.getDrawable(context, R.drawable.map_stop_icon); shape.setBounds((int) mBuffer, (int) mBuffer, bm.getWidth(), bm.getHeight()); // Shade with darkest color at tip of arrow arrowPaintFill.setShader(new LinearGradient(0, 0, mBuffer, mBuffer, r.getColor(R.color.theme_primary), r.getColor(R.color.theme_accent), Shader.TileMode.MIRROR)); // Rotate around below coordinates (trial and error) rotationX = mPx / 2f + mBuffer / 2f; rotationY = bm.getHeight() / 2f - mBuffer / 2f; } else if (direction.equals(WEST)) { directionAngle = 0f; // Arrow is drawn pointing West, so no rotation bm = Bitmap.createBitmap((int) (mPx + mBuffer), mPx, Bitmap.Config.ARGB_8888); c = new Canvas(bm); shape = ContextCompat.getDrawable(context, R.drawable.map_stop_icon); shape.setBounds((int) mBuffer, 0, bm.getWidth(), bm.getHeight()); arrowPaintFill.setShader(new LinearGradient(0, bm.getHeight() / 2, mArrowHeightPx, bm.getHeight() / 2, r.getColor(R.color.theme_primary), r.getColor(R.color.theme_accent), Shader.TileMode.MIRROR)); // For WEST rotationX = bm.getHeight() / 2f; rotationY = bm.getHeight() / 2f; } else if (direction.equals(SOUTH_WEST)) { directionAngle = 225f; // Arrow is drawn N, rotate 225 degrees bm = Bitmap.createBitmap((int) (mPx + mBuffer), (int) (mPx + mBuffer), Bitmap.Config.ARGB_8888); c = new Canvas(bm); shape = ContextCompat.getDrawable(context, R.drawable.map_stop_icon); shape.setBounds((int) mBuffer, 0, bm.getWidth(), mPx); arrowPaintFill.setShader(new LinearGradient(0, bm.getHeight(), mBuffer, bm.getHeight() - mBuffer, r.getColor(R.color.theme_primary), r.getColor(R.color.theme_accent), Shader.TileMode.MIRROR)); // Rotate around below coordinates (trial and error) rotationX = bm.getWidth() / 2f - mBuffer / 4f; rotationY = mPx / 2f + mBuffer / 4f; } else if (direction.equals(SOUTH)) { directionAngle = 180f; // Arrow is drawn N, rotate 180 degrees bm = Bitmap.createBitmap(mPx, (int) (mPx + mBuffer), Bitmap.Config.ARGB_8888); c = new Canvas(bm); shape = ContextCompat.getDrawable(context, R.drawable.map_stop_icon); shape.setBounds(0, 0, bm.getWidth(), (int) (bm.getHeight() - mBuffer)); arrowPaintFill.setShader(new LinearGradient(bm.getWidth() / 2, bm.getHeight(), bm.getWidth() / 2, bm.getHeight() - mArrowHeightPx, r.getColor(R.color.theme_primary), r.getColor(R.color.theme_accent), Shader.TileMode.MIRROR)); rotationX = bm.getWidth() / 2f; rotationY = bm.getHeight() / 2f; } else if (direction.equals(SOUTH_EAST)) { directionAngle = 135f; // Arrow is drawn N, rotate 135 degrees bm = Bitmap.createBitmap((int) (mPx + mBuffer), (int) (mPx + mBuffer), Bitmap.Config.ARGB_8888); c = new Canvas(bm); shape = ContextCompat.getDrawable(context, R.drawable.map_stop_icon); shape.setBounds(0, 0, mPx, mPx); arrowPaintFill.setShader(new LinearGradient(bm.getWidth(), bm.getHeight(), bm.getWidth() - mBuffer, bm.getHeight() - mBuffer, r.getColor(R.color.theme_primary), r.getColor(R.color.theme_accent), Shader.TileMode.MIRROR)); // Rotate around below coordinates (trial and error) rotationX = (mPx + mBuffer / 2) / 2f; rotationY = bm.getHeight() / 2f; } else if (direction.equals(EAST)) { directionAngle = 180f; // Arrow is drawn pointing West, so rotate 180 bm = Bitmap.createBitmap((int) (mPx + mBuffer), mPx, Bitmap.Config.ARGB_8888); c = new Canvas(bm); shape = ContextCompat.getDrawable(context, R.drawable.map_stop_icon); shape.setBounds(0, 0, mPx, bm.getHeight()); arrowPaintFill.setShader(new LinearGradient(bm.getWidth(), bm.getHeight() / 2, bm.getWidth() - mArrowHeightPx, bm.getHeight() / 2, r.getColor(R.color.theme_primary), r.getColor(R.color.theme_accent), Shader.TileMode.MIRROR)); rotationX = bm.getWidth() / 2f; rotationY = bm.getHeight() / 2f; } else if (direction.equals(NORTH_EAST)) { directionAngle = 45f; // Arrow is drawn pointing N, so rotate 45 degrees bm = Bitmap.createBitmap((int) (mPx + mBuffer), (int) (mPx + mBuffer), Bitmap.Config.ARGB_8888); c = new Canvas(bm); shape = ContextCompat.getDrawable(context, R.drawable.map_stop_icon); shape.setBounds(0, (int) mBuffer, mPx, bm.getHeight()); // Shade with darkest color at tip of arrow arrowPaintFill.setShader(new LinearGradient(bm.getWidth(), 0, bm.getWidth() - mBuffer, mBuffer, r.getColor(R.color.theme_primary), r.getColor(R.color.theme_accent), Shader.TileMode.MIRROR)); // Rotate around middle of circle rotationX = (float) mPx / 2; rotationY = bm.getHeight() - (float) mPx / 2; } else { throw new IllegalArgumentException(direction); } shape.draw(c); if (direction.equals(NO_DIRECTION)) { // Everything after this point is for drawing the arrow image, so return the bitmap as-is for no arrow return bm; } /** * Draw the arrow - all dimensions should be relative to px so the arrow is drawn the same * size for all orientations */ // Height of the cutout in the bottom of the triangle that makes it an arrow (0=triangle) final float CUTOUT_HEIGHT = mPx / 12; Path path = new Path(); float x1 = 0, y1 = 0; // Tip of arrow float x2 = 0, y2 = 0; // lower left float x3 = 0, y3 = 0; // cutout in arrow bottom float x4 = 0, y4 = 0; // lower right if (direction.equals(NORTH) || direction.equals(SOUTH) || direction.equals(NORTH_EAST) || direction.equals(SOUTH_EAST) || direction.equals(NORTH_WEST) || direction.equals(SOUTH_WEST)) { // Arrow is drawn pointing NORTH // Tip of arrow x1 = mPx / 2; y1 = 0; // lower left x2 = (mPx / 2) - (mArrowWidthPx / 2); y2 = mArrowHeightPx; // cutout in arrow bottom x3 = mPx / 2; y3 = mArrowHeightPx - CUTOUT_HEIGHT; // lower right x4 = (mPx / 2) + (mArrowWidthPx / 2); y4 = mArrowHeightPx; } else if (direction.equals(EAST) || direction.equals(WEST)) { // Arrow is drawn pointing WEST // Tip of arrow x1 = 0; y1 = mPx / 2; // lower left x2 = mArrowHeightPx; y2 = (mPx / 2) - (mArrowWidthPx / 2); // cutout in arrow bottom x3 = mArrowHeightPx - CUTOUT_HEIGHT; y3 = mPx / 2; // lower right x4 = mArrowHeightPx; y4 = (mPx / 2) + (mArrowWidthPx / 2); } path.setFillType(Path.FillType.EVEN_ODD); path.moveTo(x1, y1); path.lineTo(x2, y2); path.lineTo(x3, y3); path.lineTo(x4, y4); path.lineTo(x1, y1); path.close(); // Rotate arrow around (rotationX, rotationY) point Matrix matrix = new Matrix(); matrix.postRotate(directionAngle, rotationX, rotationY); path.transform(matrix); c.drawPath(path, arrowPaintFill); c.drawPath(path, mArrowPaintStroke); return bm; }
From source file:com.viewpagerindicator.TitlePageIndicatorEx1.java
@Override protected void onDraw(Canvas canvas) { super.onDraw(canvas); //Calculate views bounds ArrayList<RectF> bounds = calculateAllBounds(mPaintText); final int count = mViewPager.getAdapter().getCount(); final int countMinusOne = count - 1; final float halfWidth = getWidth() / 2f; final int left = getLeft(); final float leftClip = left + mClipPadding; final int width = getWidth(); final int height = getHeight(); final int right = left + width; final float rightClip = right - mClipPadding; int page = mCurrentPage; // int page = mSelectedPage; float offsetPercent; // Log.d("xx", "page: "+mCurrentPage+", offset: "+mCurrentOffset+", halfwidth: "+halfWidth); if (mCurrentOffset <= halfWidth) { offsetPercent = 1.0f * mCurrentOffset / width; } else {/* w w w. ja v a 2s . c o m*/ page += 1; offsetPercent = 1.0f * (width - mCurrentOffset) / width; } final boolean currentSelected = (offsetPercent <= SELECTION_FADE_PERCENTAGE); final boolean currentBold = (offsetPercent <= BOLD_FADE_PERCENTAGE); final float selectedPercent = (SELECTION_FADE_PERCENTAGE - offsetPercent) / SELECTION_FADE_PERCENTAGE; //Verify if the current view must be clipped to the screen RectF curPageBound = bounds.get(mCurrentPage); float curPageWidth = curPageBound.right - curPageBound.left; // if (curPageBound.left < leftClip) { // //Try to clip to the screen (left side) // clipViewOnTheLeft(curPageBound, curPageWidth, left); // } // if (curPageBound.right > rightClip) { // //Try to clip to the screen (right side) // clipViewOnTheRight(curPageBound, curPageWidth, right); // } // // //Left views starting from the current position // if (mCurrentPage > 0) { // for (int i = mCurrentPage - 1; i >= 0; i--) { // RectF bound = bounds.get(i); // //Is left side is outside the screen // if (bound.left < leftClip) { // float w = bound.right - bound.left; // //Try to clip to the screen (left side) // clipViewOnTheLeft(bound, w, left); // //Except if there's an intersection with the right view // RectF rightBound = bounds.get(i + 1); // //Intersection // if (bound.right + mTitlePadding > rightBound.left) { // bound.left = rightBound.left - w - mTitlePadding; // bound.right = bound.left + w; // } // } // } // } // //Right views starting from the current position // if (mCurrentPage < countMinusOne) { // for (int i = mCurrentPage + 1 ; i < count; i++) { // RectF bound = bounds.get(i); // //If right side is outside the screen // if (bound.right > rightClip) { // float w = bound.right - bound.left; // //Try to clip to the screen (right side) // clipViewOnTheRight(bound, w, right); // //Except if there's an intersection with the left view // RectF leftBound = bounds.get(i - 1); // //Intersection // if (bound.left - mTitlePadding < leftBound.right) { // bound.left = leftBound.right + mTitlePadding; // bound.right = bound.left + w; // } // } // } // } //Now draw views for (int i = 0; i < count; i++) { //Get the title RectF bound = bounds.get(i); //Only if one side is visible if ((bound.left > left && bound.left < right) || (bound.right > left && bound.right < right)) { final boolean currentPage = (i == page); //Only set bold if we are within bounds mPaintText.setFakeBoldText(currentPage && currentBold && mBoldText); //Draw text as unselected mPaintText.setColor(mColorText); canvas.drawText(mTitleProvider.getTitle(i), bound.left, bound.bottom + mTopPadding, mPaintText); //If we are within the selected bounds draw the selected text if (currentPage && currentSelected) { mPaintText.setColor(mColorSelected); mPaintText.setAlpha((int) ((mColorSelected >>> 24) * selectedPercent)); canvas.drawText(mTitleProvider.getTitle(i), bound.left, bound.bottom + mTopPadding, mPaintText); } } } //Draw the footer line mPath = new Path(); mPath.moveTo(0, height - mFooterLineHeight / 2f); mPath.lineTo(width, height - mFooterLineHeight / 2f); mPath.close(); canvas.drawPath(mPath, mPaintFooterLine); switch (mFooterIndicatorStyle) { case Triangle: mPath = new Path(); mPath.moveTo(halfWidth, height - mFooterLineHeight - mFooterIndicatorHeight); mPath.lineTo(halfWidth + mFooterIndicatorHeight, height - mFooterLineHeight); mPath.lineTo(halfWidth - mFooterIndicatorHeight, height - mFooterLineHeight); mPath.close(); canvas.drawPath(mPath, mPaintFooterIndicator); break; case Underline: if (!currentSelected) { break; } RectF underlineBounds = bounds.get(page); mPath = new Path(); mPath.moveTo(underlineBounds.left - mFooterIndicatorUnderlinePadding, height - mFooterLineHeight); mPath.lineTo(underlineBounds.right + mFooterIndicatorUnderlinePadding, height - mFooterLineHeight); mPath.lineTo(underlineBounds.right + mFooterIndicatorUnderlinePadding, height - mFooterLineHeight - mFooterIndicatorHeight); mPath.lineTo(underlineBounds.left - mFooterIndicatorUnderlinePadding, height - mFooterLineHeight - mFooterIndicatorHeight); mPath.close(); // mPaintFooterIndicator.setAlpha((int)(0xFF * selectedPercent)); canvas.drawPath(mPath, mPaintFooterIndicator); // mPaintFooterIndicator.setAlpha(0xFF); break; } }