circle icons for accounts with color coding

This commit is contained in:
Andy Scherzinger 2016-03-24 01:28:57 +01:00
parent d9d3424968
commit 7e2c560004
3 changed files with 31 additions and 15 deletions

View file

@ -32,17 +32,19 @@
<ImageView
android:id="@+id/drawer_usericon"
android:layout_width="24dp"
android:layout_height="24dp"
android:layout_width="32dp"
android:layout_height="32dp"
android:layout_gravity="center_vertical"
android:src="@drawable/ic_account_circle"
android:padding="0dp"
android:layout_margin="0dp"
/>
<LinearLayout
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:gravity="center_vertical"
android:layout_gravity="top"
android:paddingLeft="@dimen/standard_half_padding"
android:paddingRight="@dimen/standard_half_padding"
android:orientation="vertical">

View file

@ -5,7 +5,6 @@ import android.graphics.Color;
import android.graphics.ColorFilter;
import android.graphics.Paint;
import android.graphics.PixelFormat;
import android.graphics.RectF;
import android.graphics.drawable.Drawable;
/**
@ -24,21 +23,23 @@ public class TextDrawable extends Drawable {
bg = new Paint();
bg.setStyle(Paint.Style.FILL);
bg.setAntiAlias(true);
bg.setColor(color);
paint = new Paint();
paint.setColor(Color.WHITE);
paint.setTextSize(20);
paint.setTextSize(60);
paint.setAntiAlias(true);
paint.setFakeBoldText(true);
}
@Override
public void draw(Canvas canvas) {
//RectF rf = new RectF(0,24,24,0);
//canvas.drawRoundRect(rf,24,24,bg);
canvas.drawRect(0,-20,20,40,bg);
canvas.drawText(text, 4, 6, paint);
//float[] radii = {radius, radius, radius, radius, radius, radius, radius, radius};
//new RoundRectShape(radii, null, null);
canvas.drawCircle(40, 40, 40, bg);
//canvas.drawRect(0, -20, 20, 40, bg);
canvas.drawText(text, 20, 60, paint);
}
@Override

View file

@ -4,6 +4,7 @@ import android.accounts.Account;
import android.accounts.AccountManager;
import android.content.Intent;
import android.content.res.Configuration;
import android.graphics.Color;
import android.os.Bundle;
import android.support.design.widget.NavigationView;
import android.support.v4.view.GravityCompat;
@ -250,9 +251,9 @@ public abstract class DrawerActivity extends ToolbarActivity {
// add all accounts to list
for (int i = 0; i < accounts.length; i++) {
try {
int[] rgb = calculateRGB(accounts[i]);
TextDrawable icon = new TextDrawable(accounts[i].name.substring(0, 1).toUpperCase(),
rgb[0], rgb[1], rgb[2]);
int[] rgb = calculateRGB(accounts[i].name);
TextDrawable icon = new TextDrawable(accounts[i].name.substring(0, 1).toUpperCase()
, rgb[0], rgb[1], rgb[2]);
mNavigationView.getMenu().add(R.id.drawer_menu_accounts, Menu.NONE, 0, accounts[i].name).setIcon(icon);
} catch (Exception e) {
Log_OC.e(TAG, "Error calculating RGB value for account menu item.", e);
@ -267,10 +268,10 @@ public abstract class DrawerActivity extends ToolbarActivity {
}
}
private int[] calculateRGB(Account account) throws UnsupportedEncodingException, NoSuchAlgorithmException {
private int[] calculateRGB(String accountName) throws UnsupportedEncodingException, NoSuchAlgorithmException {
// using adapted algorithm from /core/js/placeholder.js:50
int lastAtPos = account.name.lastIndexOf("@");
String username = account.name.substring(0, lastAtPos);
int lastAtPos = accountName.lastIndexOf("@");
String username = accountName.substring(0, lastAtPos);
byte[] seed = username.getBytes("UTF-8");
MessageDigest md = MessageDigest.getInstance("MD5");
// Integer seedMd5Int = Math.abs(new String(Hex.encodeHex(seedMd5))
@ -316,6 +317,18 @@ public abstract class DrawerActivity extends ToolbarActivity {
usernameFull.setText(accountName);
int lastAtPos = accountName.lastIndexOf("@");
username.setText(accountName.substring(0, lastAtPos));
ImageView usericon = (ImageView) ((NavigationView) findViewById(R.id.nav_view))
.getHeaderView(0).findViewById(R.id.drawer_usericon);
try {
int[] rgb = calculateRGB(accountName);
TextDrawable icon = new TextDrawable(
accountName.substring(0, 1).toUpperCase(), rgb[0], rgb[1], rgb[2]);
usericon.setImageDrawable(icon);
} catch (Exception e) {
Log_OC.e(TAG, "Error calculating RGB value for active account icon.", e);
usericon.setImageResource(R.drawable.ic_account_circle);
}
}
}