Search in sources :

Example 1 with ShadowSelectorGenerator

use of com.devpaul.materiallibrary.utils.ShadowSelectorGenerator in project MaterialLibrary by DeveloperPaul123.

the class MaterialFloatingActionButton method init.

/**
 * Initialize various things for this view.
 * @param context the context passed from the constructor.
 * @param attributeSet attributeSet passed from the constructor.
 */
private void init(Context context, AttributeSet attributeSet) {
    int size = 0;
    // try to first deduce the accent color.
    int accentColor = -1;
    TypedArray a = null;
    try {
        TypedValue typedValue = new TypedValue();
        a = context.obtainStyledAttributes(typedValue.data, new int[] { R.attr.colorAccent });
        accentColor = a.getColor(0, -1);
    } catch (Resources.NotFoundException e) {
        e.printStackTrace();
    } finally {
        if (a != null) {
            a.recycle();
        }
    }
    // read the attributes.
    TypedArray attr = context.obtainStyledAttributes(attributeSet, R.styleable.MaterialFloatingActionButton, 0, 0);
    mButtonColor = attr.getColor(R.styleable.MaterialFloatingActionButton_mat_fab_colorNormal, accentColor == -1 ? getColor(android.R.color.holo_blue_dark) : accentColor);
    mButtonPressedColor = attr.getColor(R.styleable.MaterialFloatingActionButton_mat_fab_colorPressed, ColorUtils.getDarkerColor(mButtonColor));
    mIcon = attr.getResourceId(R.styleable.MaterialFloatingActionButton_mat_fab_icon, 0);
    useSelector = attr.getBoolean(R.styleable.MaterialFloatingActionButton_mat_fab_use_selector, false);
    size = attr.getInt(R.styleable.MaterialFloatingActionButton_mat_fab_size, 0);
    attr.recycle();
    iconSize = getDimension(R.dimen.mat_fab_icon_size);
    if (mIcon != 0) {
        iconBitmap = BitmapFactory.decodeResource(getResources(), mIcon);
        drawable = new BitmapDrawable(getResources(), iconBitmap);
        drawable.setAntiAlias(true);
        bitmapRect = new Rect(0, 0, iconBitmap.getWidth(), iconBitmap.getHeight());
        bitmapDrawRect = new RectF(0.0f, 0.0f, iconSize, iconSize);
    } else {
        iconBitmap = getDefaulBitmap();
        bitmapRect = new Rect(0, 0, iconBitmap.getWidth(), iconBitmap.getHeight());
        bitmapDrawRect = new RectF(0.0f, 0.0f, iconSize, iconSize);
    }
    float maxShadowOffset = getDimension(R.dimen.mat_fab_shadow_offset) * 1.5f;
    float minShadowOffset = maxShadowOffset / 1.5f;
    float maxShadowSize = getDimension(R.dimen.mat_fab_shadow_max_radius);
    float minShawdowSize = getDimension(R.dimen.mat_fab_shadow_min_radius) / 2;
    buttonSize = size == 0 ? getDimension(R.dimen.mat_fab_normal_size) : getDimension(R.dimen.mat_fab_mini_size);
    mSize = buttonSize + maxShadowSize * 4 + maxShadowOffset * 3;
    cx = mSize / 2;
    cy = mSize / 2;
    rotation = 0;
    rotationAnimator = ObjectAnimator.ofFloat(this, "rotation", MAX_ROTATION);
    rotationAnimator.setDuration(200);
    rotationAnimator.setInterpolator(new OvershootInterpolator());
    float halfsize = buttonSize / 2;
    float halfBitmapSize = iconSize / 2;
    bitmapDrawRect.set(cx - halfBitmapSize, cy - halfBitmapSize, cx + halfBitmapSize, cy + halfBitmapSize);
    mButtonPaint.setStyle(Paint.Style.FILL);
    mButtonPaint.setColor(mButtonColor);
    bitmapPaint = new Paint();
    bitmapPaint.setAntiAlias(true);
    bitmapPaint.setFilterBitmap(true);
    bitmapPaint.setDither(true);
    shadowRippleGenerator = new ShadowRippleGenerator(this, mButtonPaint);
    shadowRippleGenerator.setRippleColor(ColorUtils.getDarkerColor(mButtonColor));
    shadowRippleGenerator.setClipRadius((int) buttonSize / 2);
    shadowRippleGenerator.setAnimationDuration(200);
    shadowRippleGenerator.setMaxRippleRadius((int) (0.75f * buttonSize / 2));
    shadowRippleGenerator.setBoundingRect(new RectF(cx - halfsize, cy - halfsize, cx + halfsize, cy + halfsize));
    shadowSelectorGenerator = new ShadowSelectorGenerator(this, mButtonPaint);
    shadowSelectorGenerator.setNormalColor(mButtonColor);
    shadowSelectorGenerator.setPressedColor(mButtonPressedColor);
    shadowSelectorGenerator.setAnimationDuration(200);
    shadowSelectorGenerator.setShadowLimits(minShadowOffset, maxShadowOffset, minShawdowSize, maxShadowSize);
    invalidate();
}
Also used : Rect(android.graphics.Rect) OvershootInterpolator(android.view.animation.OvershootInterpolator) ShadowRippleGenerator(com.devpaul.materiallibrary.utils.ShadowRippleGenerator) BitmapDrawable(android.graphics.drawable.BitmapDrawable) Paint(android.graphics.Paint) SuppressLint(android.annotation.SuppressLint) Paint(android.graphics.Paint) RectF(android.graphics.RectF) TypedArray(android.content.res.TypedArray) Resources(android.content.res.Resources) TypedValue(android.util.TypedValue) ShadowSelectorGenerator(com.devpaul.materiallibrary.utils.ShadowSelectorGenerator)

Aggregations

SuppressLint (android.annotation.SuppressLint)1 Resources (android.content.res.Resources)1 TypedArray (android.content.res.TypedArray)1 Paint (android.graphics.Paint)1 Rect (android.graphics.Rect)1 RectF (android.graphics.RectF)1 BitmapDrawable (android.graphics.drawable.BitmapDrawable)1 TypedValue (android.util.TypedValue)1 OvershootInterpolator (android.view.animation.OvershootInterpolator)1 ShadowRippleGenerator (com.devpaul.materiallibrary.utils.ShadowRippleGenerator)1 ShadowSelectorGenerator (com.devpaul.materiallibrary.utils.ShadowSelectorGenerator)1