SlidingMenu的使用

本文介绍如何在Android应用中集成QQ第三方登录功能,并实现一个带有多种选项的侧边栏菜单。通过示例代码展示了如何使用Tencent SDK进行用户授权登录流程,以及SlidingMenu库来创建自定义动画效果的侧边栏。
public class MainActivity extends AppCompatActivity {
    private static final String TAG = "MainActivity";
    private static final String APP_ID = "1105602574";//官方获取的APPID
    private Tencent mTencent;
    private BaseUiListener mIUiListener;
    private UserInfo mUserInfo;
    private ImageView image;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        this.requestWindowFeature(Window.FEATURE_NO_TITLE);
        setContentView(R.layout.activity_main);

        // 创建对象
        SlidingMenu smenu = new SlidingMenu(this);
        // 设置出现在左边还是右边
        smenu.setMode(SlidingMenu.LEFT);
        // 设置调出slidingmenu的区域
        smenu.setTouchModeAbove(SlidingMenu.TOUCHMODE_MARGIN);
        // 设置阴影的宽度
        smenu.setShadowWidth(8);
        smenu.setShadowDrawable(R.drawable.shadow);
        // 设置slidingmenu滑出来时的宽度
        smenu.setBehindOffset(100);
        // 设置刚拉出来的时候颜色,1为全黑
        smenu.setFadeDegree(0.3f);
        // 添加到Activity上面
        smenu.attachToActivity(this, SlidingMenu.SLIDING_CONTENT);
        // 此处加入要侧滑的布局文件





  smenu.setMenu(R.layout.menu);

        // 关闭监听
//        smenu.setOnClosedListener(new SlidingMenu.OnClosedListener() {
//
//            @Override
//            public void onClosed() {
//                Toast.makeText(MainActivity.this, "关闭了", Toast.LENGTH_SHORT).show();
//
//            }
//        });
//        // 打开监听
//        smenu.setOnOpenedListener(new SlidingMenu.OnOpenedListener() {
//
//            @Override
//            public void onOpened() {
//                Toast.makeText(MainActivity.this, "打开了", Toast.LENGTH_SHORT).show();
//
//            }
//        });
        // 创建动画对象设置显示的时候出现的动画,这里我写的是一个入场动画
        SlidingMenu.CanvasTransformer canvasTransformer = new SlidingMenu.CanvasTransformer() {

            @Override
            public void transformCanvas(Canvas canvas, float percentOpen) {
                float scale = (float) (percentOpen * 0.25 + 0.75);
                canvas.scale(scale, scale, canvas.getWidth() / 2, canvas.getHeight() / 2);
            }
        };
        smenu.setBehindCanvasTransformer(canvasTransformer);
        mTencent = Tencent.createInstance(APP_ID,MainActivity.this.getApplicationContext());

    }
    public void buttonLogin(View v){

        mIUiListener = new BaseUiListener();
        //all表示获取所有权限
        mTencent.login(MainActivity.this,"all", mIUiListener);
    }
    private class BaseUiListener implements IUiListener {

        @Override
        public void onComplete(Object response) {
            Toast.makeText(MainActivity.this, "授权成功", Toast.LENGTH_SHORT).show();
            Log.e(TAG, "response:" + response);
            JSONObject obj = (JSONObject) response;
            try {
                String openID = obj.getString("openid");
                String accessToken = obj.getString("access_token");
                String expires = obj.getString("expires_in");
                mTencent.setOpenId(openID);
                mTencent.setAccessToken(accessToken,expires);
                QQToken qqToken = mTencent.getQQToken();
                mUserInfo = new UserInfo(getApplicationContext(),qqToken);
                mUserInfo.getUserInfo(new IUiListener() {
                    @Override
                    public void onComplete(Object response) {
                        Log.e(TAG,"登录成功"+response.toString());
                    }

                    @Override
                    public void onError(UiError uiError) {
                        Log.e(TAG,"登录失败"+uiError.toString());
                    }

                    @Override
                    public void onCancel() {
                        Log.e(TAG,"登录取消");

                    }
                });
            } catch (JSONException e) {
                e.printStackTrace();
            }
        }

        @Override
        public void onError(UiError uiError) {
            Toast.makeText(MainActivity.this, "授权失败", Toast.LENGTH_SHORT).show();

        }

        @Override
        public void onCancel() {
            Toast.makeText(MainActivity.this, "授权取消", Toast.LENGTH_SHORT).show();

        }

    }

    /**
     * 在调用Login的Activity或者Fragment中重写onActivityResult方法
     * @param requestCode
     * @param resultCode
     * @param data
     */
    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        if(requestCode == Constants.REQUEST_LOGIN){
            Tencent.onActivityResultData(requestCode,resultCode,data,mIUiListener);
        }
        super.onActivityResult(requestCode, resultCode, data);
    }

}





Menu的布局

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical" android:layout_width="match_parent"
    android:layout_height="match_parent">


    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="100dp">

        <ImageView
            android:layout_width="match_parent"
            android:layout_height="180dp"
            android:scaleType="fitXY"
            android:background="#FA6E95" />


        <ImageView
            android:onClick="buttonLogin"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:src="@mipmap/ic_launcher"
            android:layout_marginStart="14dp"
            android:layout_marginTop="16dp"
            android:layout_alignParentTop="true"
            android:layout_alignParentStart="true" />

    </RelativeLayout>

    <LinearLayout
        android:id="@+id/me_message"
        android:layout_width="match_parent"
        android:layout_height="50dp"
        android:background="@color/white">

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

        <TextView
            android:id="@+id/zhuye"
            android:layout_marginLeft="60dp"
            android:layout_weight="1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="center_vertical"
            android:clickable="true"
            android:textColor="@drawable/textview"
            android:text="主页"

            android:textSize="15sp" />

    </LinearLayout>

    <View
        android:layout_width="match_parent"
        android:layout_height="4dp"
        android:background="@color/backgroud"/>

    <LinearLayout
        android:id="@+id/me_focus"
        android:layout_width="match_parent"
        android:layout_height="50dp"
        android:background="@color/white">

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

        <TextView

            android:textColor="@drawable/textview"
            android:layout_marginLeft="60dp"
            android:layout_weight="1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="center_vertical"
            android:text="我的大会员"
            android:textSize="15sp"

            />

    </LinearLayout>
    <View
        android:layout_width="match_parent"
        android:layout_height="1dp"
        android:layout_marginLeft="20dp"
        android:layout_marginRight="20dp"
        android:layout_gravity="center_horizontal"
        android:background="@color/backgroud"/>
    <LinearLayout
        android:id="@+id/me_collect"
        android:layout_width="match_parent"
        android:layout_height="50dp"
        android:background="@color/white">

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

        <TextView
            android:layout_marginLeft="60dp"
            android:layout_weight="1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="center_vertical"
            android:text="离线缓存"
            android:textSize="15sp"

            android:textColor="@drawable/textview"
           />

    </LinearLayout>
    <View
        android:layout_width="match_parent"
        android:layout_height="1dp"
        android:layout_marginLeft="20dp"
        android:layout_marginRight="20dp"
        android:layout_gravity="center_horizontal"
        android:background="@color/backgroud"/>
    <LinearLayout
        android:id="@+id/me_history"
        android:layout_width="match_parent"
        android:layout_height="50dp"
        android:background="@color/white">

        <ImageView
            android:layout_marginLeft="15dp"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="center_vertical"
            android:src="@drawable/ic_launcher" />

        <TextView
            android:layout_marginLeft="60dp"
            android:layout_weight="1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="center_vertical"
            android:text="我的收藏"
            android:textSize="15sp"


            android:textColor="@drawable/textview"
          />

    </LinearLayout>
    <View
        android:layout_width="match_parent"
        android:layout_height="4dp"
        android:background="@color/backgroud"/>
    <LinearLayout
        android:id="@+id/me_game"
        android:layout_width="match_parent"
        android:layout_height="50dp"
        android:background="@color/white">

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

        <TextView
            android:layout_marginLeft="60dp"

            android:layout_weight="0.52"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="center_vertical"
            android:text="历史记录"
            android:textSize="15sp"

            android:textColor="@drawable/textview"
            />

    </LinearLayout>
    <View
        android:layout_width="match_parent"
        android:layout_height="1dp"
        android:layout_marginLeft="20dp"
        android:layout_marginRight="20dp"
        android:layout_gravity="center_horizontal"
        android:background="@color/backgroud"/>
    <LinearLayout
        android:id="@+id/me_shop"
        android:layout_width="match_parent"
        android:layout_height="50dp"
        android:background="@color/white">

        <ImageView
            android:layout_marginLeft="15dp"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="center_vertical"
            android:src="@drawable/ic_launcher" />

        <TextView
            android:layout_marginLeft="60dp"
            android:layout_weight="1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="center_vertical"
            android:text="关注的人"
            android:textSize="15sp"


            android:textColor="@drawable/textview"
        />

    </LinearLayout>
    <View
        android:layout_width="match_parent"
        android:layout_height="4dp"
        android:background="@color/backgroud"/>
    <LinearLayout
        android:id="@+id/me_ssetup"
        android:layout_width="match_parent"
        android:layout_height="50dp"
        android:background="@color/white">

        <ImageView
            android:layout_marginLeft="15dp"
            android:layout_width="20dp"
            android:layout_height="20dp"
            android:layout_gravity="center_vertical"
            android:src="@drawable/ic_launcher" />

        <TextView
            android:layout_marginLeft="60dp"
            android:layout_weight="1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="center_vertical"
            android:text="我的钱包"
            android:textSize="15sp"


            android:textColor="@drawable/textview"
        />

    </LinearLayout>
    <View
        android:layout_width="match_parent"
        android:layout_height="4dp"
        android:background="@color/backgroud"/>
    <LinearLayout

        android:layout_width="match_parent"
        android:layout_height="50dp"
        android:background="@color/white">

        <ImageView
            android:layout_marginLeft="15dp"
            android:layout_width="20dp"
            android:layout_height="20dp"
            android:layout_gravity="center_vertical"
            android:src="@drawable/ic_launcher" />

        <TextView
            android:layout_marginLeft="60dp"
            android:layout_weight="1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="center_vertical"
            android:text="主题选择"
            android:textSize="15sp"

            android:textColor="@drawable/textview"
          />

    </LinearLayout>
    <View
        android:layout_width="match_parent"
        android:layout_height="4dp"
        android:background="@color/backgroud"/>
    <LinearLayout

        android:layout_width="match_parent"
        android:layout_height="50dp"
        android:background="@color/white">

        <ImageView
            android:layout_marginLeft="15dp"
            android:layout_width="20dp"
            android:layout_height="20dp"
            android:layout_gravity="center_vertical"
            android:src="@drawable/ic_launcher" />

        <TextView
            android:layout_marginLeft="60dp"
            android:layout_weight="1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="center_vertical"
            android:text="应用推荐"
            android:textSize="15sp"


            android:textColor="@drawable/textview"
         />

    </LinearLayout>

</LinearLayout>




 shadow的定义
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android" >

    <gradient
        android:centerColor="#777"
        android:endColor="#666"
        android:startColor="#888" />

</shape>




评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值