Created
October 15, 2016 22:27
-
-
Save NightlyNexus/e885772e1854ea630e93ed24fb7d472c to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
* Copyright (C) 2016 Eric Cochran | |
* | |
* Licensed under the Apache License, Version 2.0 (the "License"); | |
* you may not use this file except in compliance with the License. | |
* You may obtain a copy of the License at | |
* | |
* http://www.apache.org/licenses/LICENSE-2.0 | |
* | |
* Unless required by applicable law or agreed to in writing, software | |
* distributed under the License is distributed on an "AS IS" BASIS, | |
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | |
* See the License for the specific language governing permissions and | |
* limitations under the License. | |
*/ | |
import android.graphics.Canvas; | |
import android.graphics.Paint; | |
import android.graphics.Rect; | |
import android.graphics.Typeface; | |
import android.graphics.drawable.ShapeDrawable; | |
import android.graphics.drawable.shapes.OvalShape; | |
import android.graphics.drawable.shapes.Shape; | |
import android.support.annotation.ColorInt; | |
import static android.graphics.Paint.ANTI_ALIAS_FLAG; | |
import static android.graphics.Paint.Align.CENTER; | |
public final class ProfileImageLoadingDrawable extends ShapeDrawable { | |
private final Paint textPaint; | |
private final Rect textBounds = new Rect(); | |
private char[] letters; | |
public ProfileImageLoadingDrawable(@ColorInt int textColor, float textSize, Typeface typeface) { | |
super(new OvalShape()); | |
textPaint = new Paint(ANTI_ALIAS_FLAG); | |
textPaint.setTextAlign(CENTER); | |
textPaint.setColor(textColor); | |
textPaint.setTextSize(textSize); | |
textPaint.setTypeface(typeface); | |
} | |
public void setLetters(char... letters) { | |
this.letters = letters; | |
textPaint.getTextBounds(letters, 0, letters.length, textBounds); | |
invalidateSelf(); | |
} | |
@Override protected void onDraw(Shape shape, Canvas canvas, Paint paint) { | |
super.onDraw(shape, canvas, paint); | |
if (letters == null) { | |
return; | |
} | |
Rect bounds = getBounds(); | |
float textBottom = (bounds.height() + textBounds.height()) / 2f; | |
canvas.drawText(letters, 0, letters.length, bounds.width() / 2f, textBottom, textPaint); | |
} | |
@Override protected void onBoundsChange(Rect bounds) { | |
setIntrinsicWidth(bounds.width()); | |
setIntrinsicHeight(bounds.height()); | |
super.onBoundsChange(bounds); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment