Android学习教程之分类侧滑菜单(5)

本文实例为大家分享了Android分类侧滑菜单的制作方法,供大家参考,具体内容如下

classificmenuActivity.java代码:

package com.siso.crazyworld;

import android.animation.Animator;
import android.content.res.Configuration;
import android.graphics.Color;
import android.graphics.drawable.BitmapDrawable;
import android.os.Bundle;
import android.support.v4.widget.DrawerLayout;
import android.support.v7.app.ActionBarActivity;
import android.support.v7.app.ActionBarDrawerToggle;
import android.support.v7.widget.Toolbar;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewAnimationUtils;
import android.view.animation.AccelerateInterpolator;
import android.widget.LinearLayout;

import com.siso.crazyworld.fragment.ContentFragment;

import java.util.ArrayList;
import java.util.List;
import sidemenu.interfaces.Resourceble;
import sidemenu.interfaces.ScreenShotable;
import sidemenu.model.SlideMenuItem;
import sidemenu.util.ViewAnimator;

public class classificmenuActivity extends ActionBarActivity implements ViewAnimator.ViewAnimatorListener {
 private DrawerLayout drawerLayout;
 private ActionBarDrawerToggle drawerToggle;
 private List<SlideMenuItem> list = new ArrayList<>();
 private ContentFragment contentFragment;
 private ViewAnimator viewAnimator;
 private int res = R.drawable.content_music;
 private LinearLayout linearLayout;

 @Override
 protected void onCreate(Bundle savedInstanceState) {
  super.onCreate(savedInstanceState);
  setContentView(R.layout.activity_classificmenu);
  contentFragment = ContentFragment.newInstance(R.drawable.content_music);
  getSupportFragmentManager().beginTransaction()
    .replace(R.id.content_frame, contentFragment)
    .commit();
  drawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout);
  drawerLayout.setScrimColor(Color.TRANSPARENT);
  linearLayout = (LinearLayout) findViewById(R.id.left_drawer);
  linearLayout.setOnClickListener(new View.OnClickListener() {
   @Override
   public void onClick(View v) {
    drawerLayout.closeDrawers();
   }
  });

  setActionBar();
  createMenuList();
  viewAnimator = new ViewAnimator<>(this, list, contentFragment, drawerLayout, this);
 }

 private void createMenuList() {
  SlideMenuItem menuItem0 = new SlideMenuItem(ContentFragment.CLOSE, R.drawable.icn_close);
  list.add(menuItem0);
  SlideMenuItem menuItem = new SlideMenuItem(ContentFragment.BUILDING, R.drawable.icn_1);
  list.add(menuItem);
  SlideMenuItem menuItem2 = new SlideMenuItem(ContentFragment.BOOK, R.drawable.icn_2);
  list.add(menuItem2);
  SlideMenuItem menuItem3 = new SlideMenuItem(ContentFragment.PAINT, R.drawable.icn_3);
  list.add(menuItem3);
  SlideMenuItem menuItem4 = new SlideMenuItem(ContentFragment.CASE, R.drawable.icn_4);
  list.add(menuItem4);
  SlideMenuItem menuItem5 = new SlideMenuItem(ContentFragment.SHOP, R.drawable.icn_5);
  list.add(menuItem5);
  SlideMenuItem menuItem6 = new SlideMenuItem(ContentFragment.PARTY, R.drawable.icn_6);
  list.add(menuItem6);
  SlideMenuItem menuItem7 = new SlideMenuItem(ContentFragment.MOVIE, R.drawable.icn_7);
  list.add(menuItem7);
 }

 private void setActionBar() {
  Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
  setSupportActionBar(toolbar);
  getSupportActionBar().setHomeButtonEnabled(true);
  getSupportActionBar().setDisplayHomeAsUpEnabled(true);
  drawerToggle = new ActionBarDrawerToggle(
    this,     /* host Activity */
    drawerLayout,   /* DrawerLayout object */
    toolbar, /* nav drawer icon to replace 'Up' caret */
    R.string.drawer_open, /* "open drawer" description */
    R.string.drawer_close /* "close drawer" description */
  ) {

   /** Called when a drawer has settled in a completely closed state. */
   public void onDrawerClosed(View view) {
    super.onDrawerClosed(view);
    linearLayout.removeAllViews();
    linearLayout.invalidate();
   }

   @Override
   public void onDrawerSlide(View drawerView, float slideOffset) {
    super.onDrawerSlide(drawerView, slideOffset);
    if (slideOffset > 0.6 && linearLayout.getChildCount() == 0)
     viewAnimator.showMenuContent();
   }

   /** Called when a drawer has settled in a completely open state. */
   public void onDrawerOpened(View drawerView) {
    super.onDrawerOpened(drawerView);
   }
  };
  drawerLayout.setDrawerListener(drawerToggle);
 }

 @Override
 protected void onPostCreate(Bundle savedInstanceState) {
  super.onPostCreate(savedInstanceState);
  drawerToggle.syncState();
 }

 @Override
 public void onConfigurationChanged(Configuration newConfig) {
  super.onConfigurationChanged(newConfig);
  drawerToggle.onConfigurationChanged(newConfig);
 }

 @Override
 public boolean onCreateOptionsMenu(Menu menu) {
  getMenuInflater().inflate(R.menu.menu_main, menu);
  return true;
 }

 @Override
 public boolean onOptionsItemSelected(MenuItem item) {
  if (drawerToggle.onOptionsItemSelected(item)) {
   return true;
  }
  switch (item.getItemId()) {
   case R.id.action_settings:
    return true;
   default:
    return super.onOptionsItemSelected(item);
  }
 }

 private ScreenShotable replaceFragment(ScreenShotable screenShotable, int topPosition) {
  this.res = this.res == R.drawable.content_music ? R.drawable.content_films : R.drawable.content_music;
  View view = findViewById(R.id.content_frame);
  int finalRadius = Math.max(view.getWidth(), view.getHeight());
  Animator animator = ViewAnimationUtils.createCircularReveal(view, 0, topPosition, 0, finalRadius);
  animator.setInterpolator(new AccelerateInterpolator());
  animator.setDuration(ViewAnimator.CIRCULAR_REVEAL_ANIMATION_DURATION);

  findViewById(R.id.content_overlay).setBackground(new BitmapDrawable(getResources(), screenShotable.getBitmap()));
  animator.start();
  ContentFragment contentFragment = ContentFragment.newInstance(this.res);
  getSupportFragmentManager().beginTransaction().replace(R.id.content_frame, contentFragment).commit();
  return contentFragment;
 }

 @Override
 public ScreenShotable onSwitch(Resourceble slideMenuItem, ScreenShotable screenShotable, int position) {
  switch (slideMenuItem.getName()) {
   case ContentFragment.CLOSE:
    return screenShotable;
   default:
    return replaceFragment(screenShotable, position);
  }
 }

 @Override
 public void disableHomeButton() {
  getSupportActionBar().setHomeButtonEnabled(false);

 }

 @Override
 public void enableHomeButton() {
  getSupportActionBar().setHomeButtonEnabled(true);
  drawerLayout.closeDrawers();

 }

 @Override
 public void addViewToContainer(View view) {
  linearLayout.addView(view);
 }
}

fragment文件夹下ContentFragment.java代码:

package com.siso.crazyworld.fragment;

import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;

import com.siso.crazyworld.R;

import sidemenu.interfaces.ScreenShotable;

public class ContentFragment extends Fragment implements ScreenShotable {
 public static final String CLOSE = "Close";
 public static final String BUILDING = "Building";
 public static final String BOOK = "Book";
 public static final String PAINT = "Paint";
 public static final String CASE = "Case";
 public static final String SHOP = "Shop";
 public static final String PARTY = "Party";
 public static final String MOVIE = "Movie";

 private View containerView;
 protected ImageView mImageView;
 protected int res;
 private Bitmap bitmap;

 public static ContentFragment newInstance(int resId) {
  ContentFragment contentFragment = new ContentFragment();
  Bundle bundle = new Bundle();
  bundle.putInt(Integer.class.getName(), resId);
  contentFragment.setArguments(bundle);
  return contentFragment;
 }

 @Override
 public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
  super.onViewCreated(view, savedInstanceState);
  this.containerView = view.findViewById(R.id.container);
 }

 @Override
 public void onCreate(Bundle savedInstanceState) {
  super.onCreate(savedInstanceState);
  res = getArguments().getInt(Integer.class.getName());
 }

 @Override
 public View onCreateView(LayoutInflater inflater, ViewGroup container,
        Bundle savedInstanceState) {
  View rootView = inflater.inflate(R.layout.fragment_main, container, false);
  mImageView = (ImageView) rootView.findViewById(R.id.image_content);
  mImageView.setClickable(true);
  mImageView.setFocusable(true);
  mImageView.setImageResource(res);
  return rootView;
 }

 @Override
 public void takeScreenShot() {
  Thread thread = new Thread() {
   @Override
   public void run() {
    Bitmap bitmap = Bitmap.createBitmap(containerView.getWidth(),
      containerView.getHeight(), Bitmap.Config.ARGB_8888);
    Canvas canvas = new Canvas(bitmap);
    containerView.draw(canvas);
    ContentFragment.this.bitmap = bitmap;
   }
  };

  thread.start();

 }

 @Override
 public Bitmap getBitmap() {
  return bitmap;
 }
}

activity_classificmenu.xml内容:

<android.support.v4.widget.DrawerLayout
 xmlns:android="http://schemas.android.com/apk/res/android"
 android:id="@+id/drawer_layout"
 android:layout_width="match_parent"
 android:layout_height="match_parent">

 <io.codetail.widget.RevealFrameLayout
  android:id="@+id/container_frame"
  xmlns:android="http://schemas.android.com/apk/res/android"
  android:layout_width="match_parent"
  android:layout_height="match_parent">

  <LinearLayout
   android:id="@+id/content_overlay"
   android:layout_width="match_parent"
   android:layout_height="match_parent"
   android:orientation="vertical"/>

  <LinearLayout
   android:id="@+id/content_frame"
   android:layout_width="match_parent"
   android:layout_height="match_parent"
   android:orientation="vertical"/>

  <android.support.v7.widget.Toolbar
   android:id="@+id/toolbar"
   android:layout_height="wrap_content"
   android:layout_width="match_parent"
   android:minHeight="?attr/actionBarSize"
   android:background="?attr/colorPrimary"/>

 </io.codetail.widget.RevealFrameLayout>

 <ScrollView
  android:id="@+id/scrollView"
  android:scrollbarThumbVertical="@android:color/transparent"
  android:layout_width="@dimen/sliding_menu_width"
  android:layout_height="match_parent"
  android:layout_gravity="start|bottom">

  <LinearLayout
   android:id="@+id/left_drawer"
   android:orientation="vertical"
   android:layout_width="@dimen/sliding_menu_width"
   android:layout_height="wrap_content"
   android:divider="@android:color/transparent"
   android:background="@android:color/transparent">
  </LinearLayout>
 </ScrollView>
</android.support.v4.widget.DrawerLayout>

.xml内容:

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
 <item android:state_pressed="true" android:drawable="@drawable/item_down"/>
 <item android:state_selected="true" android:drawable="@drawable/item_down"/>
 <item android:state_focused="true" android:drawable="@drawable/item_down"/>
 <item android:drawable="@drawable/item_up"/>
</selector>

fragment_main.xml内容:

<?xml version="1.0" encoding="utf-8"?>
<io.codetail.widget.RevealFrameLayout
 xmlns:android="http://schemas.android.com/apk/res/android"
 android:layout_width="match_parent"
 android:layout_height="match_parent">

 <FrameLayout
  android:id="@+id/container"
  android:layout_width="match_parent"
  android:layout_height="match_parent">

  <ImageView
   android:scaleType="fitXY"
   android:id="@+id/image_content"
   android:src="@drawable/content_films"
   android:layout_width="match_parent"
   android:layout_height="match_parent"/>

 </FrameLayout>
</io.codetail.widget.RevealFrameLayout>

strings.xml:

<?xml version="1.0" encoding="utf-8"?>
<resources>

 <string name="app_name">CrazyWorld</string>
 <string name="action_settings">设置</string>
 <string name="drawer_open">Open</string>
 <string name="drawer_close">Close</string>

</resources>

styles.xml

<resources>

 <!-- Base application theme. -->
 <style name="AppTheme" parent="Theme.AppCompat.Light.NoActionBar">
 </style>

</resources>

运行结果:

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持我们。

(0)

相关推荐

  • Android滑动优化高仿QQ6.0侧滑菜单(滑动优化)

     推荐阅读:Android使用ViewDragHelper实现仿QQ6.0侧滑界面(一) 但是之前的实现,只是简单的可以显示和隐藏左侧的菜单,但是特别生硬,而且没有任何平滑的趋势,那么今天就来优化一下吧,加上平滑效果,而且可以根据手势滑动的方向来判断是否是显示和隐藏. 首先先来实现手势判断是否隐藏和显示 这里就要用到了一个方法了,如下: 这个是ViewDradHelper里面的方法: /** * 当view被释放的时候处理的事情(松手) * * @param releasedChild 被释放的

  • Android使用DrawerLayout实现仿QQ双向侧滑菜单

    1.概述 之前写了一个Android 高仿 QQ5.0 侧滑菜单效果 自定义控件来袭 ,恰逢QQ5.2又加了一个右侧菜单,刚好看了下DrawerLayout,一方面官方的东西,我都比较感兴趣:另一方面,这玩意用起来的确方便,于是简单写了个demo,高仿QQ5.2双向侧滑,分享给大家. 首先看看效果图: DrawerLayout用起来真的很方便,下面一起看看用法~ 2.DrawerLayout的使用 直接将DrawerLayout作为根布局,然后其内部第一个View为内容区域,第二个View为左侧

  • Android控件View打造完美的自定义侧滑菜单

    一.概述 在App中,经常会出现侧滑菜单,侧滑滑出View等效果,虽然说Android有很多第三方开源库,但是实际上咱们可以自己也写一个自定义的侧滑View控件,其实不难,主要涉及到以下几个要点: 1.对Android中Window类中的DecorView有所了解 2.对Scroller类实现平滑移动效果 3.自定义ViewGroup的实现 首先来看看效果图吧:     下面现在就来说说这里咱们实现侧滑View的基本思路吧,这里我采用的是自定义一个继承于RelativeLayout的控件叫做XC

  • Android实现网易新闻客户端侧滑菜单(1)

    Android中很多产品(比如360手机助手.网易菜单...)都采用侧滑菜单的展现形式,采用这种展现形式 1.能把更多的展现内容都存放在菜单中 2.设计上也能体现出视觉效果 现在这种交互方式越来越流行了,虽然这种交互方式可以通过自定义组件的方式来实现,但是用三方开源库更简单. SlidingMenu:SlidingMenu的是一种比较新的设置界面或配置界面效果,在主界面左滑或者右滑出现设置界面,能方便的进行各种操作.目前有大量的应用都在使用这一效果. 地址:https://github.com/

  • Android实现原生侧滑菜单的超简单方式

    先来看看效果图 当你点击菜单可以更改图标,例如点击happy,首页就会变一个笑脸,这个实现的过程超级简单 你需要使用ToolBar与DrawableLayout两个比较新的控件 首先要写三个xml布局文件,我这里的布局文件是使用了include标签嵌入的,代码如下 headbar_toolbar.xml <?xml version="1.0" encoding="utf-8"?> <android.support.v7.widget.Toolbar

  • Android使用自定义控件HorizontalScrollView打造史上最简单的侧滑菜单

    侧滑菜单在很多应用中都会见到,最近QQ5.0侧滑还玩了点花样~~对于侧滑菜单,一般大家都会自定义ViewGroup,然后隐藏菜单栏,当手指滑动时,通过Scroller或者不断的改变leftMargin等实现:多少都有点复杂,完成以后还需要对滑动冲突等进行处理~~今天给大家带来一个简单的实现,史上最简单有点夸张,但是的确是我目前遇到过的最简单的一种实现~~~ 1.原理分析 既然是侧滑,无非就是在巴掌大的屏幕,塞入大概两巴掌大的布局,需要滑动可以出现另一个,既然这样,大家为啥不考虑使用Android

  • Android自定义控件简单实现侧滑菜单效果

    侧滑菜单在很多应用中都会见到,最近QQ5.0侧滑还玩了点花样~~对于侧滑菜单,一般大家都会自定义ViewGroup,然后隐藏菜单栏,当手指滑动时,通过Scroller或者不断的改变leftMargin等实现:多少都有点复杂,完成以后还需要对滑动冲突等进行处理~~今天给大家带来一个简单的实现,史上最简单有点夸张,但是的确是我目前遇到过的最简单的一种实现~~~ 1.原理分析 既然是侧滑,无非就是在巴掌大的屏幕,塞入大概两巴掌大的布局,需要滑动可以出现另一个,既然这样,大家为啥不考虑使用Android

  • Android UI实现SlidingMenu侧滑菜单效果

    本篇博客给大家分享一个效果比较好的侧滑菜单的Demo,实现点击左边菜单切换Fragment. 效果如下: 主Activity代码: package com.infzm.slidingmenu.demo; import android.os.Bundle; import android.support.v4.app.Fragment; import android.view.View; import android.view.View.OnClickListener; import android

  • 代码分析Android实现侧滑菜单

    Android 侧滑菜单的实现,参考网上的代码,实现侧滑菜单.最重要的是这个动画类UgcAnimations,如何使用动画类来侧滑的封装FlipperLayout. 1.实现效果 2.动画类UgcAnimations package com.mmsx.base; import android.content.Context; import android.view.View; import android.view.ViewGroup.MarginLayoutParams; import and

  • Android开源组件SlidingMenu侧滑菜单使用介绍

    现在很多android应用都有侧滑菜单,效果很不错. GitHub上有SlidingMenu的开源库,使用起来很方便. SlidingMenu GitHub地址:https://github.com/jfeinstein10/SlidingMenu.GitHub上说,Sliding结合ActionBarSherlock使用功能可以更丰富,ActionBarSherlock GitHub地址:https://github.com/JakeWharton/ActionBarSherlock 附csd

随机推荐