viernes, 19 de septiembre de 2014

Android: Drop down menu

I had a custom action bar, the center button was the button that opened a menu under it like a drop down menu (if not exactly like one).

AbstractActivity
public abstract class AbstractActivity extends ActionBarActivity {

@Override
 protected void onCreate(Bundle savedInstanceState) {

  this.getSupportActionBar().setDisplayHomeAsUpEnabled(false);
  this.getSupportActionBar().setDisplayShowHomeEnabled(false);
  this.getSupportActionBar().setDisplayUseLogoEnabled(false);
  this.getSupportActionBar().setDisplayShowCustomEnabled(true);
  this.getSupportActionBar().setCustomView(R.layout.action_bar_view);
  this.getSupportActionBar().setBackgroundDrawable(
    getResources().getDrawable(R.drawable.background_test));

  View customView = getSupportActionBar().getCustomView();
  View mainMenu = customView.findViewById(R.id.main_menu);
  mainMenu.setOnClickListener(new OnClickListener() {

   @Override
   public void onClick(View v) {

    getMainMenu().animateToggle();
   }
  });

  setTitle(this.getTitle());
 }
}

action_bar_view
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:background="@android:color/white"
    android:gravity="center"
    android:orientation="horizontal" >

    <com.provengroup.view.MainMenuButtonView 
        android:id="@+id/main_menu"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />

</LinearLayout>

MainMenuButtonView
public class MainMenuButtonView extends LinearLayout {

    private ImageView logoImage;
    private Animation animationRight;
    private Animation animationLeft;
    boolean opened = false;

    public MainMenuButtonView(Context context) {
        this(context, null);
    }

    public MainMenuButtonView(Context context, AttributeSet attrs) {
        super(context, attrs);

        LayoutInflater inflater = (LayoutInflater) context
                .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        inflater.inflate(R.layout.main_menu_button_view, this, true);

        logoImage = (ImageView) findViewById(R.id.logo_image);

        animationRight = AnimationUtils.loadAnimation(context, R.anim.rotate_right);
        animationRight.setFillAfter(true);

        animationLeft = AnimationUtils.loadAnimation(context, R.anim.rotate_left);
        animationLeft.setFillAfter(true);
    }

    public Bitmap getResizedImage() {

        Bitmap image = BitmapFactory.decodeResource(getResources(),
                R.drawable.checkpos_logo_image);

        int height = getHeight();

        if (height != 0 && height != image.getWidth()) {

            int width = height * image.getWidth() / image.getHeight();

            Bitmap scaledImage = Bitmap.createScaledBitmap(image, width,
                    height, false);

            image.recycle();

            return scaledImage;

        } else {

            return image;
        }
    }

    @Override
    public void setOnClickListener(final OnClickListener l) {

        super.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(final View v) {

                if (opened) {
                    logoImage.startAnimation(animationLeft);
                    
                    opened = false;

                } else {
                    logoImage.startAnimation(animationRight);
                    
                    opened = true;
                }

                l.onClick(v);
            }
        });
    }

    public void restore() {
        
        if (opened) {
            
            logoImage.startAnimation(animationLeft);
            
            opened = false;
        }
    }

}

main_menu_button_view
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/main_menu"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:background="@android:color/white"
    android:gravity="center"
    android:orientation="horizontal" >

    <ImageView
        android:id="@+id/logo_image"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:src="@drawable/checkpos_logo_image" />

    <ImageView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginLeft="10dp"
        android:src="@drawable/checkpos_logo_text" />

</LinearLayout>

rotate_right
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android">
    <rotate 
        android:duration="300"
        android:pivotX="50%"
        android:pivotY="50%"
        android:fromDegrees="0"
        android:toDegrees="180"/>
</set>

rotate_left
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android">
    <rotate 
        android:duration="300"
        android:pivotX="50%"
        android:pivotY="50%"
        android:fromDegrees="180"
        android:toDegrees="0"/>
</set>

No hay comentarios:

Publicar un comentario