123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126 |
- /*
- * Nextcloud Android client application
- *
- * @author Tobias Kaminsky
- * Copyright (C) 2020 Tobias Kaminsky
- * Copyright (C) 2020 Nextcloud GmbH
- *
- * This program is free software: you can redistribute it and/or modify
- * it under the terms of the GNU Affero General Public License as published by
- * the Free Software Foundation, either version 3 of the License, or
- * (at your option) any later version.
- *
- * This program is distributed in the hope that it will be useful,
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
- * GNU Affero General Public License for more details.
- *
- * You should have received a copy of the GNU Affero General Public License
- * along with this program. If not, see <https://www.gnu.org/licenses/>.
- */
- package com.owncloud.android.ui;
- import android.content.Context;
- import android.graphics.Canvas;
- import android.graphics.Color;
- import android.graphics.ColorFilter;
- import android.graphics.Paint;
- import android.graphics.PixelFormat;
- import android.graphics.drawable.Drawable;
- import com.owncloud.android.utils.BitmapUtils;
- import androidx.annotation.DrawableRes;
- import androidx.annotation.NonNull;
- import androidx.core.content.res.ResourcesCompat;
- /**
- * A Drawable object that draws a status
- */
- public class StatusDrawable extends Drawable {
- private String text;
- private @DrawableRes int icon = -1;
- private Paint textPaint;
- private final Paint backgroundPaint;
- private final float radius;
- private Context context;
- public StatusDrawable(@DrawableRes int icon, float size, Context context) {
- radius = size;
- this.icon = icon;
- this.context = context;
- backgroundPaint = new Paint();
- backgroundPaint.setStyle(Paint.Style.FILL);
- backgroundPaint.setAntiAlias(true);
- backgroundPaint.setColor(Color.argb(200, 255, 255, 255));
- }
- public StatusDrawable(BitmapUtils.Color color, float size) {
- radius = size;
- backgroundPaint = new Paint();
- backgroundPaint.setStyle(Paint.Style.FILL);
- backgroundPaint.setAntiAlias(true);
- backgroundPaint.setColor(Color.argb(color.a, color.r, color.g, color.b));
- }
- public StatusDrawable(String icon, float size) {
- text = icon;
- radius = size;
- backgroundPaint = new Paint();
- backgroundPaint.setStyle(Paint.Style.FILL);
- backgroundPaint.setAntiAlias(true);
- backgroundPaint.setColor(Color.argb(200, 255, 255, 255));
- textPaint = new Paint();
- textPaint.setColor(Color.WHITE);
- textPaint.setTextSize(size);
- textPaint.setAntiAlias(true);
- textPaint.setTextAlign(Paint.Align.CENTER);
- }
- /**
- * Draw in its bounds (set via setBounds) respecting optional effects such as alpha (set via setAlpha) and color
- * filter (set via setColorFilter) a circular background with a user's first character.
- *
- * @param canvas The canvas to draw into
- */
- @Override
- public void draw(@NonNull Canvas canvas) {
- if (backgroundPaint != null) {
- canvas.drawCircle(radius, radius, radius, backgroundPaint);
- }
- if (text != null) {
- textPaint.setTextSize(1.6f * radius);
- canvas.drawText(text, radius, radius - ((textPaint.descent() + textPaint.ascent()) / 2), textPaint);
- }
- if (icon != -1) {
- Drawable drawable = ResourcesCompat.getDrawable(context.getResources(), icon, null);
- drawable.setBounds(0,
- 0,
- (int) (2 * radius),
- (int) (2 * radius));
- drawable.draw(canvas);
- }
- }
- @Override
- public void setAlpha(int alpha) {
- textPaint.setAlpha(alpha);
- }
- @Override
- public void setColorFilter(ColorFilter cf) {
- textPaint.setColorFilter(cf);
- }
- @Override
- public int getOpacity() {
- return PixelFormat.TRANSLUCENT;
- }
- }
|