CERCA
PER MODELLO
FullScreen Chatbox! :)

Utente del giorno: gianpassa con ben 1 Thanks ricevuti nelle ultime 24 ore
Utente della settimana: carotix con ben 11 Thanks ricevuti negli ultimi sette giorni
Utente del mese: megthebest con ben 26 Thanks ricevuti nell'ultimo mese

Visualizzazione dei risultati da 1 a 2 su 2
Discussione:

[SNIPPET][View] Floating Action Button [FAB]

Se questa discussione ti è stata utile, ti preghiamo di lasciare un messaggio di feedback in modo che possa essere preziosa in futuro anche per altri utenti come te!
  1. #1
    Androidiani Power User L'avatar di cesco


    Registrato dal
    Sep 2010
    Località
    Bg
    Messaggi
    5,018
    Smartphone
    TYTN2,desire,desireHD, oneX

    Ringraziamenti
    54
    Ringraziato 1,346 volte in 724 Posts
    Predefinito

    [SNIPPET][View] Floating Action Button [FAB]

    Vi lascio qui sotto il gist relativo al Floating Action Button che ho implementato in una mia app e basato sulla classe creata da un ragazzo su Google+
    per chi non lo sapesse FAB è il nuovo pulsante rotondo introdotto con il Material Design. Questo non è un backport ma una View creata per simularlo.
    Ho aggiunto tutte le features mancanti quali la profondità del bottone, la dimensione dell'ombra e gli attributi XML mancanti nella classe originale

    Happy coding

    <?xml version="1.0" encoding="utf-8"?>
    <resources>
    <declare-styleable name="Fab">
    <attr name="fabDrawable" format="reference"/>
    <attr name="fabColor" format="color"/>
    <attr name="fabDepth" format="integer"/>
    <attr name="fabShadowRadius" format="float"/>
    </declare-styleable>
    </resources>
    view raw attrs.xml hosted with ❤ by GitHub
    import android.animation.ObjectAnimator;
    import android.content.Context;
    import android.content.res.TypedArray;
    import android.graphics.Bitmap;
    import android.graphics.Canvas;
    import android.graphics.Color;
    import android.graphics.Paint;
    import android.graphics.Point;
    import android.graphics.drawable.BitmapDrawable;
    import android.graphics.drawable.Drawable;
    import android.util.AttributeSet;
    import android.util.DisplayMetrics;
    import android.view.Display;
    import android.view.MotionEvent;
    import android.view.View;
    import android.view.WindowManager;
    import android.view.animation.AccelerateInterpolator;
    import android.view.animation.DecelerateInterpolator;
    public class Fab extends View {
    Context _context;
    Paint mButtonPaint, mDrawablePaint;
    Bitmap mBitmap;
    int mScreenHeight;
    int mScreenWidth;
    float currentY;
    float currentX;
    float shadowRadius = 10.0f;
    int distance = 2;
    float pressedAlpha = 0.6f;
    boolean mHidden = false;
    int fabColor = Color.WHITE;
    float DEPTH_1 = 1.0f;
    float DEPTH_2 = 3.0f;
    float DEPTH_3 = 10.0f;
    float DEPTH_4 = 14.0f;
    float DEPTH_5 = 19.0f;
    private float mDepth = 3.0f;
    Drawable myDrawable;
    public static final int MODE_FROM_BOTTOM = 0;
    public static final int MODE_FROM_TOP = 1;
    public static final int MODE_FROM_LEFT = 2;
    public static final int MODE_FROM_RIGHT = 3;
    private int mMode = 0;
    public Fab(Context context, AttributeSet attributeSet)
    {
    super(context, attributeSet);
    _context = context;
    parseAttrs(attributeSet,0);
    init(fabColor);
    }
    public Fab(Context context, AttributeSet attributeSet, int defStyle)
    {
    super(context, attributeSet, defStyle);
    _context = context;
    parseAttrs(attributeSet, defStyle);
    init(fabColor);
    }
    public Fab(Context context)
    {
    super(context);
    _context = context;
    init(fabColor);
    }
    private void parseAttrs(AttributeSet attrs, int defStyle) {
    TypedArray a = _context.obtainStyledAttributes(attrs, R.styleable.Fab, defStyle, 0);
    myDrawable = a.getDrawable(R.styleable.Fab_fabDrawable);
    mBitmap = ((BitmapDrawable) myDrawable).getBitmap();
    fabColor = a.getColor(R.styleable.Fab_fabColor, Color.WHITE);
    distance = a.getInt(R.styleable.Fab_fabDepth, distance);
    shadowRadius = a.getFloat(R.styleable.Fab_fabShadowRadius, 10.0f);
    switch(distance) {
    case 0:
    mDepth = DEPTH_1;
    break;
    case 1:
    mDepth = DEPTH_2;
    break;
    case 2:
    mDepth = DEPTH_3;
    break;
    case 3:
    mDepth = DEPTH_4;
    break;
    case 4:
    mDepth = DEPTH_5;
    break;
    default:
    mDepth = DEPTH_2;
    }
    a.recycle();
    }
    public void setFabColor(int color)
    {
    this.fabColor = color;
    invalidate();
    }
    public void setFabDrawable(Drawable fabDrawable)
    {
    myDrawable = fabDrawable;
    mBitmap = ((BitmapDrawable) myDrawable).getBitmap();
    invalidate();
    }
    public void setFabShadowRadius(float radius) {
    this.shadowRadius = radius;
    invalidate();
    }
    public void setFabDepth(int depth) {
    this.distance = depth;
    invalidate();
    }
    public void setPressedAlpha(float alpha) {
    this.pressedAlpha = alpha;
    invalidate();
    }
    public void setAnimationMode(int mode) {
    this.mMode = mode;
    }
    public void init(int fabColor)
    {
    setWillNotDraw(false);
    this.setLayerType(View.LAYER_TYPE_SOFTWARE, null);
    mButtonPaint = new Paint(Paint.ANTI_ALIAS_FLAG);
    mButtonPaint.setColor(fabColor);
    mButtonPaint.setStyle(Paint.Style.FILL);
    mButtonPaint.setShadowLayer(shadowRadius, 0.0f, mDepth, Color.argb(100, 0, 0, 0));
    mDrawablePaint = new Paint(Paint.ANTI_ALIAS_FLAG);
    invalidate();
    WindowManager mWindowManager = (WindowManager) _context.getSystemService(Context.WINDOW_SERVICE);
    Display display = mWindowManager.getDefaultDisplay();
    Point size = new Point();
    display.getSize(size);
    mScreenHeight = size.y;
    mScreenWidth = size.x;
    }
    @Override
    protected void onDraw(Canvas canvas)
    {
    setClickable(true);
    canvas.drawCircle(getWidth()/2, getHeight()/2,(float) (getWidth()/2.6), mButtonPaint);
    canvas.drawBitmap(mBitmap, (getWidth() - mBitmap.getWidth()) / 2, (getHeight() - mBitmap.getHeight()) / 2, mDrawablePaint);
    }
    @Override
    public boolean onTouchEvent(MotionEvent event)
    {
    if(event.getAction() == MotionEvent.ACTION_UP)
    {
    setAlpha(1.0f);
    }
    else if(event.getAction() == MotionEvent.ACTION_DOWN)
    {
    setAlpha(pressedAlpha);
    }
    return super.onTouchEvent(event);
    }
    public int dpToPx(int dp)
    {
    DisplayMetrics displayMetrics = getContext().getResources().getDisplayMetrics();
    int px = Math.round(dp * (displayMetrics.xdpi / DisplayMetrics.DENSITY_DEFAULT));
    return px;
    }
    public boolean isHidden() {
    return this.mHidden;
    }
    public void hideFab() {
    if(mHidden == false) {
    currentY = getY();
    currentX = getX();
    ObjectAnimator mHideAnimation = null;
    switch(mMode) {
    case MODE_FROM_BOTTOM:
    mHideAnimation = ObjectAnimator.ofFloat(this, "Y", mScreenHeight);
    break;
    case MODE_FROM_TOP:
    mHideAnimation = ObjectAnimator.ofFloat(this, "Y", -mScreenHeight);
    break;
    case MODE_FROM_LEFT:
    mHideAnimation = ObjectAnimator.ofFloat(this, "X", -mScreenWidth);
    break;
    case MODE_FROM_RIGHT:
    mHideAnimation = ObjectAnimator.ofFloat(this, "X", mScreenWidth);
    break;
    default:
    mHideAnimation = ObjectAnimator.ofFloat(this, "Y", mScreenHeight);
    break;
    }
    mHideAnimation.setInterpolator(new AccelerateInterpolator());
    mHideAnimation.start();
    mHidden = true;
    }
    }
    public void showFab()
    {
    if(mHidden == true)
    {
    ObjectAnimator mShowAnimation = null;
    switch(mMode) {
    case MODE_FROM_BOTTOM:
    mShowAnimation = ObjectAnimator.ofFloat(this, "Y", currentY);
    break;
    case MODE_FROM_TOP:
    mShowAnimation = ObjectAnimator.ofFloat(this, "Y", -currentY);
    break;
    case MODE_FROM_LEFT:
    mShowAnimation = ObjectAnimator.ofFloat(this, "X", currentX);
    break;
    case MODE_FROM_RIGHT:
    mShowAnimation = ObjectAnimator.ofFloat(this, "X", currentX);
    break;
    default:
    mShowAnimation = ObjectAnimator.ofFloat(this, "Y", currentY);
    break;
    }
    mShowAnimation.setInterpolator(new DecelerateInterpolator());
    mShowAnimation.start();
    mHidden = false;
    }
    }
    }
    view raw Fab.java hosted with ❤ by GitHub
    <com.your.package.Fab
    android:id="@+id/fabbutton"
    android:layout_width="72dp"
    android:layout_height="72dp"
    android:layout_margin="16dp"
    app:fabColor="@android:color/white"
    app:fabDepth="1"
    app:fabDrawable="@drawable/ic_content_new"
    app:fabShadowRadius="12" />
    //***Your code***
    mFab = (Fab) rootView.findViewById(R.id.fabbutton);
    mFab.setAnimationMode(Fab.MODE_FROM_RIGHT);
    //***Your code***
    view raw Sample.java hosted with ❤ by GitHub
    Follow me on:
    <!-- Place this tag where you want the widget to render. -->
    <div class="g-person" data-width="180" data-href="//plus.google.com/113012341277613226011" data-theme="dark" data-rel="author"></div>

    <!-- Place this tag after the last widget tag. -->
    <script type="text/javascript">
    window.___gcfg = {lang: 'it'};

    (function() {
    var po = document.createElement('script'); po.type = 'text/javascript'; po.async = true;
    po.src = 'https://apis.google.com/js/plusone.js';
    var s = document.getElementsByTagName('script')[0]; s.parentNode.insertBefore(po, s);
    })();
    </script>
    <br>
    <a class="twitter-timeline" href="https://twitter.com/xcesco89" data-widget-id="398762031488040960">Tweets di @xcesco89</a>
    <script>!function(d,s,id){var js,fjs=d.getElementsByTagName(s)[0],p=/^http:/.test(d.location)?'http':'https';if(!d.getElementB yId(id)){js=d.createElement(s);js.id=id;js.src=p+" ://platform.twitter.com/widgets.js";fjs.parentNode.insertBefore(js,fjs);}} (document,"script","twitter-wjs");</script>
    Androidiani app
    Estensione Androidiani per Google Chrome

  2. I seguenti 2 Utenti hanno ringraziato cesco per il post:

    Crotan (17-07-14),NiloGlock (17-07-14)

  3.  
  4. #2
    Androidiano VIP L'avatar di punticci


    Registrato dal
    Jul 2011
    Località
    Vicenza
    Messaggi
    1,488
    Smartphone
    LG Nexus 4

    Ringraziamenti
    162
    Ringraziato 152 volte in 126 Posts
    Predefinito

    Ottimo, dovrò testarlo :-)

    -----------------------------------------------------------------------------------
    Modello: Nexus 5
    Play Store:https://play.google.com/store/apps/d...=Davide+Dellai
    All In One System: https://play.google.com/store/apps/d...dd.androreboot
    Email: dellai.davide@gmail.com

Permessi di invio

  • Non puoi inserire discussioni
  • Non puoi inserire risposte
  • Non puoi inserire allegati
  • Non puoi modificare i tuoi messaggi
  •  
Torna su
Privacy Policy