Java tutorial
/* * This is the source code of Telegram for Android v. 3.x.x. * It is licensed under GNU GPL v. 2 or later. * You should have received a copy of the license in this archive (see LICENSE). * * Copyright Nikolai Kudashov, 2013-2016. */ package ir.besteveryeverapp.ui.Cells; import android.content.Context; import android.graphics.Bitmap; import android.graphics.Canvas; import android.graphics.Paint; import android.graphics.PorterDuff; import android.graphics.PorterDuffColorFilter; import android.graphics.Rect; import android.graphics.drawable.BitmapDrawable; import android.graphics.drawable.ColorDrawable; import android.graphics.drawable.Drawable; import android.os.Build; import android.support.v4.content.ContextCompat; import android.text.TextUtils; import android.util.TypedValue; import android.view.Gravity; import android.widget.FrameLayout; import android.widget.ImageView; import android.widget.TextView; import chitchat.FontManager; import chitchat.skin.SkinMan; import ir.besteveryeverapp.PhoneFormat.PhoneFormat; import ir.besteveryeverapp.telegram.AndroidUtilities; import ir.besteveryeverapp.telegram.ApplicationLoader; import ir.besteveryeverapp.telegram.FileLog; import ir.besteveryeverapp.telegram.R; import ir.besteveryeverapp.telegram.UserObject; import ir.besteveryeverapp.tgnet.TLRPC; import ir.besteveryeverapp.ui.ActionBar.Theme; import ir.besteveryeverapp.ui.Components.AvatarDrawable; import ir.besteveryeverapp.ui.Components.BackupImageView; import ir.besteveryeverapp.ui.Components.LayoutHelper; public class DrawerProfileCell extends FrameLayout { private BackupImageView avatarImageView; private TextView nameTextView; private TextView phoneTextView; private ImageView shadowView; private Rect srcRect = new Rect(); private Rect destRect = new Rect(); private Paint paint = new Paint(); private int currentColor; public DrawerProfileCell(Context context) { super(context); setBackgroundColor(//0xff4c84b5 SkinMan.currentSkin.actionbarColor()); shadowView = new ImageView(context); shadowView.setVisibility(INVISIBLE); shadowView.setScaleType(ImageView.ScaleType.FIT_XY); shadowView.setImageResource(R.drawable.bottom_shadow); addView(shadowView, LayoutHelper.createFrame(LayoutHelper.MATCH_PARENT, 70, Gravity.LEFT | Gravity.BOTTOM)); avatarImageView = new BackupImageView(context); avatarImageView.getImageReceiver().setRoundRadius(AndroidUtilities.dp(32)); addView(avatarImageView, LayoutHelper.createFrame(64, 64, Gravity.LEFT | Gravity.BOTTOM, 16, 0, 0, 67)); if (SkinMan.currentSkin.hideDrawerProfile()) avatarImageView.setVisibility(GONE); nameTextView = new TextView(context); nameTextView.setTypeface(FontManager.instance().getTypeface()); nameTextView.setTextColor(0xffffffff); nameTextView.setTextSize(TypedValue.COMPLEX_UNIT_DIP, 15); nameTextView.setTypeface(FontManager.instance().getTypeface()); nameTextView.setLines(1); nameTextView.setMaxLines(1); nameTextView.setSingleLine(true); nameTextView.setGravity(Gravity.LEFT); nameTextView.setEllipsize(TextUtils.TruncateAt.END); addView(nameTextView, LayoutHelper.createFrame(LayoutHelper.MATCH_PARENT, LayoutHelper.WRAP_CONTENT, Gravity.LEFT | Gravity.BOTTOM, 16, 0, 16, 28)); phoneTextView = new TextView(context); phoneTextView.setTypeface(FontManager.instance().getTypeface()); phoneTextView.setTextColor(0xffc2e5ff); phoneTextView.setTextSize(TypedValue.COMPLEX_UNIT_DIP, 13); phoneTextView.setLines(1); phoneTextView.setMaxLines(1); phoneTextView.setSingleLine(true); phoneTextView.setGravity(Gravity.LEFT); addView(phoneTextView, LayoutHelper.createFrame(LayoutHelper.MATCH_PARENT, LayoutHelper.WRAP_CONTENT, Gravity.LEFT | Gravity.BOTTOM, 16, 0, 16, 9)); } @Override protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) { if (Build.VERSION.SDK_INT >= 21) { super.onMeasure(widthMeasureSpec, MeasureSpec.makeMeasureSpec( AndroidUtilities.dp(148) + AndroidUtilities.statusBarHeight, MeasureSpec.EXACTLY)); } else { try { super.onMeasure(widthMeasureSpec, MeasureSpec.makeMeasureSpec(AndroidUtilities.dp(148), MeasureSpec.EXACTLY)); } catch (Exception e) { setMeasuredDimension(MeasureSpec.getSize(widthMeasureSpec), AndroidUtilities.dp(148)); FileLog.e("tmessages", e); } } } @Override protected void onDraw(Canvas canvas) { int pic = SkinMan.currentSkin.actionPicture(); Drawable backgroundDrawable = pic != 0 ? ContextCompat.getDrawable(getContext(), pic) : ApplicationLoader.getCachedWallpaper(); int color = ApplicationLoader.getServiceMessageColor(); if (currentColor != color) { currentColor = color; shadowView.getDrawable() .setColorFilter(new PorterDuffColorFilter(color | 0xff000000, PorterDuff.Mode.MULTIPLY)); } if (ApplicationLoader.isCustomTheme() || pic != 0 && backgroundDrawable != null) { phoneTextView.setTextColor(0xffffffff); shadowView.setVisibility(VISIBLE); if (backgroundDrawable instanceof ColorDrawable) { backgroundDrawable.setBounds(0, 0, getMeasuredWidth(), getMeasuredHeight()); backgroundDrawable.draw(canvas); } else if (backgroundDrawable instanceof BitmapDrawable) { Bitmap bitmap = ((BitmapDrawable) backgroundDrawable).getBitmap(); float scaleX = (float) getMeasuredWidth() / (float) bitmap.getWidth(); float scaleY = (float) getMeasuredHeight() / (float) bitmap.getHeight(); float scale = scaleX < scaleY ? scaleY : scaleX; int width = (int) (getMeasuredWidth() / scale); int height = (int) (getMeasuredHeight() / scale); int x = (bitmap.getWidth() - width) / 2; int y = (bitmap.getHeight() - height) / 2; srcRect.set(x, y, x + width, y + height); destRect.set(0, 0, getMeasuredWidth(), getMeasuredHeight()); canvas.drawBitmap(bitmap, srcRect, destRect, paint); } } else { shadowView.setVisibility(INVISIBLE); phoneTextView.setTextColor(0xffc2e5ff); super.onDraw(canvas); } } public void setUser(TLRPC.User user) { if (user == null) { return; } TLRPC.FileLocation photo = null; if (user.photo != null) { photo = user.photo.photo_small; } String name = SkinMan.currentSkin.drawerProfileTitle(); String phon = SkinMan.currentSkin.drawerProfileSubtitle(); nameTextView.setText(name == null ? UserObject.getUserName(user) : name); phoneTextView.setText(phon == null ? PhoneFormat.getInstance().format("+" + user.phone) : phon); AvatarDrawable avatarDrawable = new AvatarDrawable(user); avatarDrawable.setColor(Theme.ACTION_BAR_MAIN_AVATAR_COLOR); avatarImageView.setImage(photo, "50_50", avatarDrawable); } }