Android Open Source - MaterialNavigationDrawer Material Account






From Project

Back to project page MaterialNavigationDrawer.

License

The source code is released under:

Apache License

If you think the Android project MaterialNavigationDrawer listed in this page is inappropriate, such as containing malicious code/tools or violating the copyright, please email info at java2s dot com, thanks.

Java Source Code

package it.neokree.materialnavigationdrawer.elements;
/*  www . j  a  v a2  s . c  o  m*/
import android.content.Context;
import android.content.res.Resources;
import android.graphics.Bitmap;
import android.graphics.Point;
import android.graphics.Typeface;
import android.graphics.drawable.BitmapDrawable;
import android.graphics.drawable.Drawable;
import android.os.AsyncTask;
import android.view.View;

import it.neokree.materialnavigationdrawer.elements.listeners.MaterialSectionListener;
import it.neokree.materialnavigationdrawer.util.Utils;

/**
 * Created by neokree on 11/12/14.
 */
public class MaterialAccount {

    // datas

    private Drawable photo;
    private Drawable background;
    private Drawable circularPhoto;
    private String title;
    private String subTitle;
    private int accountNumber;
    private String notifications;

    private boolean hasNotifications;

    private Resources resources;
    private OnAccountDataLoaded listener;
    private MaterialSection sectionView;

    public static final int FIRST_ACCOUNT = 0;
    public static final int SECOND_ACCOUNT = 1;
    public static final int THIRD_ACCOUNT = 2;

    // constructors

    public MaterialAccount(Resources resources,String title, String subTitle, int photo,Bitmap background) {
        this.title = title;
        this.subTitle = subTitle;
        this.resources = resources;

        // resize and caching bitmap
        new ResizePhotoResource().execute(photo);
        if(background != null)
            new ResizeBackgroundBitmap().execute(background);

    }

    public MaterialAccount(Resources resources,String title, String subTitle, int photo,int background) {
        this.title = title;
        this.subTitle = subTitle;
        this.resources = resources;

        // resize and caching bitmap
        new ResizePhotoResource().execute(photo);
        new ResizeBackgroundResource().execute(background);
    }

    public MaterialAccount(Resources resources,String title, String subTitle, Bitmap photo, int background) {
        this.title = title;
        this.subTitle = subTitle;
        this.resources = resources;

        // resize and caching bitmap
        if(photo != null)
            new ResizePhotoBitmap().execute(photo);
        new ResizeBackgroundResource().execute(background);
    }

    public MaterialAccount(Resources resources,String title, String subTitle, Bitmap photo, Bitmap background) {
        this.title = title;
        this.subTitle = subTitle;
        this.resources = resources;

        // resize and caching bitmap
        if(photo != null)
            new ResizePhotoBitmap().execute(photo);
        if (background != null)
            new ResizeBackgroundBitmap().execute(background);
    }

    // setter

    public void setPhoto(int photo){
        new ResizePhotoResource().execute(photo);
    }

    public void setPhoto(Bitmap photo) {
        new ResizePhotoBitmap().execute(photo);
    }

    public void setBackground(Bitmap background) {
        new ResizeBackgroundBitmap().execute(background);
    }

    public void setBackground(int background) {
        new ResizeBackgroundResource().execute(background);
    }

    public void setTitle(String title) {
        this.title = title;
    }

    public void setSubTitle(String subTitle) {
        this.subTitle = subTitle;
    }

    public void setAccountNumber(int number) {
        this.accountNumber = number;
    }

    public void setAccountListener(OnAccountDataLoaded listener) {
        this.listener = listener;
    }

    public MaterialAccount setNotifications(int number) {
        hasNotifications = true;
        notifications = String.valueOf(number);

        if(number >= 100) {
            notifications = "99+";
        }
        if(number < 0) {
            notifications = "0";
        }

        return this;

    }

    // getter

    public Drawable getPhoto() {
        return photo;
    }

    public Drawable getBackground() {
        return background;
    }

    public Drawable getCircularPhoto() {
        return circularPhoto;
    }

    public String getTitle() {
        return title;
    }

    public String getSubTitle() {
        return subTitle;
    }

    public int getAccountNumber() {
        return accountNumber;
    }

    public View getSectionView(Context ctx, Typeface font, MaterialSectionListener listener, boolean rippleSupport,int position) {
        if(sectionView == null) {
            sectionView = new MaterialSection(ctx,MaterialSection.ICON_40DP,rippleSupport,MaterialSection.TARGET_LISTENER);
            sectionView.useRealColor();
        }

        // set dei dati passati
        sectionView.setTypeface(font);
        sectionView.setOnClickListener(listener);

        // set dei dati dell'account
        sectionView.setIcon(getCircularPhoto());
        sectionView.setTitle(getTitle());
        if(hasNotifications) {
            sectionView.setNotificationsText(notifications);
        }
        sectionView.setPosition(position);

        return sectionView.getView();
    }

    // custom

    public void recycle() {
        Utils.recycleDrawable(photo);
        Utils.recycleDrawable(circularPhoto);
        Utils.recycleDrawable(background);
    }

    public interface OnAccountDataLoaded {

        public void onUserPhotoLoaded(MaterialAccount account);

        public void onBackgroundLoaded(MaterialAccount account);
    }

    // asynctasks

    private class ResizePhotoResource extends  AsyncTask<Integer, Void, BitmapDrawable> {

        @Override
        protected BitmapDrawable doInBackground(Integer... params) {
            Point photoSize = Utils.getUserPhotoSize(resources);

            Bitmap photo = Utils.resizeBitmapFromResource(resources,params[0],photoSize.x,photoSize.y);

            circularPhoto = new BitmapDrawable(resources,Utils.getCroppedBitmapDrawable(photo));
            return new BitmapDrawable(resources,photo);
        }

        @Override
        protected void onPostExecute(BitmapDrawable drawable) {
            photo = drawable;

            if(listener != null)
                listener.onUserPhotoLoaded(MaterialAccount.this);
        }
    }

    private class ResizePhotoBitmap extends AsyncTask<Bitmap, Void, BitmapDrawable> {

        @Override
        protected BitmapDrawable doInBackground(Bitmap... params) {
            Point photoSize = Utils.getUserPhotoSize(resources);


            Bitmap photo = Utils.resizeBitmap(params[0],photoSize.x,photoSize.y);
            params[0].recycle();

            circularPhoto = new BitmapDrawable(resources,Utils.getCroppedBitmapDrawable(photo));
            return new BitmapDrawable(resources,photo);
        }

        @Override
        protected void onPostExecute(BitmapDrawable drawable) {
            photo = drawable;

            if(listener != null)
                listener.onUserPhotoLoaded(MaterialAccount.this);
        }
    }

    private class ResizeBackgroundResource extends AsyncTask<Integer, Void, BitmapDrawable> {
        @Override
        protected BitmapDrawable doInBackground(Integer... params) {
            Point backSize = Utils.getBackgroundSize(resources);

            Bitmap back = Utils.resizeBitmapFromResource(resources,params[0],backSize.x,backSize.y);

            return new BitmapDrawable(resources,back);
        }

        @Override
        protected void onPostExecute(BitmapDrawable drawable) {
            background = drawable;

            if(listener != null)
                listener.onBackgroundLoaded(MaterialAccount.this);
        }
    }

    private class ResizeBackgroundBitmap extends AsyncTask<Bitmap, Void, BitmapDrawable> {

        @Override
        protected BitmapDrawable doInBackground(Bitmap... params) {
            Point backSize = Utils.getBackgroundSize(resources);

            Bitmap back = Utils.resizeBitmap(params[0],backSize.x,backSize.y);
            params[0].recycle();

            return new BitmapDrawable(resources,back);
        }

        @Override
        protected void onPostExecute(BitmapDrawable drawable) {
            background = drawable;

            if(listener != null)
                listener.onBackgroundLoaded(MaterialAccount.this);
        }
    }
}




Java Source Code List

it.neokree.example.MainActivity.java
it.neokree.example.backpattern.BackAnywhere.java
it.neokree.example.backpattern.BackPatternCustom.java
it.neokree.example.backpattern.BackToFirst.java
it.neokree.example.dark.Accounts.java
it.neokree.example.dark.CustomDrawerHeader.java
it.neokree.example.dark.ImageDrawerHeader.java
it.neokree.example.dark.MockedAccount.java
it.neokree.example.dark.NoDrawerHeader.java
it.neokree.example.functionalities.CustomAccountSection.java
it.neokree.example.functionalities.KitkatStatusBar.java
it.neokree.example.functionalities.MultiPane.java
it.neokree.example.functionalities.RealColorSections.java
it.neokree.example.functionalities.RippleBackport.java
it.neokree.example.functionalities.UniqueToolbarColor.java
it.neokree.example.functionalities.master_child.ChildFragment.java
it.neokree.example.functionalities.master_child.MasterChildActivity.java
it.neokree.example.functionalities.master_child.MasterFragment.java
it.neokree.example.light.Accounts.java
it.neokree.example.light.CustomDrawerHeader.java
it.neokree.example.light.ImageDrawerHeader.java
it.neokree.example.light.MockedAccount.java
it.neokree.example.light.NoDrawerHeader.java
it.neokree.example.mockedActivity.Profile.java
it.neokree.example.mockedActivity.Settings.java
it.neokree.example.mockedFragments.FragmentButton.java
it.neokree.example.mockedFragments.FragmentIndex.java
it.neokree.example.mockedFragments.FragmentList.java
it.neokree.materialnavigationdrawer.MaterialNavigationDrawer.java
it.neokree.materialnavigationdrawer.elements.MaterialAccount.java
it.neokree.materialnavigationdrawer.elements.MaterialSection.java
it.neokree.materialnavigationdrawer.elements.MaterialSubheader.java
it.neokree.materialnavigationdrawer.elements.listeners.MaterialAccountListener.java
it.neokree.materialnavigationdrawer.elements.listeners.MaterialSectionListener.java
it.neokree.materialnavigationdrawer.util.MaterialActionBarDrawerToggle.java
it.neokree.materialnavigationdrawer.util.MaterialDrawerLayout.java
it.neokree.materialnavigationdrawer.util.TypefaceManager.java
it.neokree.materialnavigationdrawer.util.Utils.java