研究了下QQ侧滑的功能,记录下与大家分享:
采用自定义HorizontalScrollView来实现,侧滑功能。
具体看代码解释。
定义两个布局文件:
menu.xml与 activity_main.xml
<?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="match_parent"
android:gravity="center_vertical"
android:orientation="vertical">
<TextView
android:textSize="40sp"
android:id="@+id/tv_1"
android:layout_margin="8dp"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="item_1" />
<TextView
android:textSize="40sp"
android:id="@+id/tv_2"
android:layout_margin="8dp"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="item_2" />
<TextView
android:textSize="40sp"
android:id="@+id/tv_3"
android:layout_margin="8dp"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="item_3" />
<TextView
android:textSize="40sp"
android:id="@+id/tv_4"
android:layout_margin="8dp"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="item_4" />
</LinearLayout>
<?xml version="1.0" encoding="utf-8"?>
<com.vic.customhorizontalscrollview.CustomHorizontalScrollView xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/hsl"
android:background="?attr/colorPrimary"
android:scrollbars="none"
tools:context=".MainActivity">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal"
>
<include layout="@layout/menu" />
<LinearLayout
android:background="?attr/colorPrimaryDark"
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:id="@+id/tv_context"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textSize="50sp"
android:text="Content" />
</LinearLayout>
</LinearLayout>
</com.vic.customhorizontalscrollview.CustomHorizontalScrollView>
如下为自定义HorizontalScrollView.java 缩放动画采用开源nineoladadnroids-2.4.0.jar, 测试时记得网上下载jar包
package com.vic.customhorizontalscrollview;
import android.content.Context;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.view.View;
import android.widget.HorizontalScrollView;
import android.widget.LinearLayout;
import com.nineoldandroids.view.ViewHelper;
/**
* Created by Vic on 2016/1/13.
*/
public class CustomHorizontalScrollView extends HorizontalScrollView {
private int screenWidth = -1;
private int menuPaddingRight = 200;
private int menuWidth;
private View menuUI,UI;
public CustomHorizontalScrollView(Context context, AttributeSet attrs) {
super(context, attrs);
//获得屏幕宽度
screenWidth = context.getResources().getDisplayMetrics().widthPixels;
//设置侧滑菜单宽度
menuWidth = screenWidth - menuPaddingRight;
}
/**
* 定义子控件宽度
* @param widthMeasureSpec
* @param heightMeasureSpec
*/
@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
super.onMeasure(widthMeasureSpec, heightMeasureSpec);
//获得子控件
LinearLayout ll = (LinearLayout) getChildAt(0);
//侧滑菜单view
menuUI = ll.getChildAt(0);
//内容
UI = ll.getChildAt(1);
//设置侧滑菜单宽度为定义的宽度
menuUI.getLayoutParams().width = menuWidth;
//设置内容控件宽度为屏幕宽度
UI.getLayoutParams().width = screenWidth;
}
@Override
protected void onLayout(boolean changed, int l, int t, int r, int b) {
super.onLayout(changed, l, t, r, b);
//程序一开始,滑到内容界面
smoothScrollTo(menuWidth, 0);
}
@Override
public boolean onTouchEvent(MotionEvent ev) {
if (ev.getAction() == MotionEvent.ACTION_UP) {
//如果滑动距离大于侧滑菜单宽度的一半就滑到内容界面,否则回到初始位置
if (getScrollX() > menuWidth / 2) {
smoothScrollTo(menuWidth, 0);
} else {
smoothScrollTo(0, 0);
}
return true;
}
return super.onTouchEvent(ev);
}
@Override
protected void onScrollChanged(int l, int t, int oldl, int oldt) {
super.onScrollChanged(l, t, oldl, oldt);
//设置缩放比例
float scale = l*1.0f/menuWidth; // (0,1)
float menuScale = 0.7f + 0.3f*(1-scale); //(1,0.7)
float UIScale = 0.8f+0.2f*scale; //(0.8,1)
ViewHelper.setScaleX(menuUI,menuScale);
ViewHelper.setScaleY(menuUI,menuScale);
ViewHelper.setScaleX(UI,UIScale);
ViewHelper.setScaleY(UI,UIScale);
}
/**
* 侧滑菜单操作后,界面回到程序开始位置
*/
public void retore(){
smoothScrollTo(menuWidth,0);
}
}
package com.vic.customhorizontalscrollview;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.View;
import android.widget.TextView;
public class MainActivity extends AppCompatActivity implements View.OnClickListener{
private TextView tv_context,tv_1,tv_2,tv_3,tv_4;
private CustomHorizontalScrollView hsl;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
initView();
}
private void initView() {
hsl = (CustomHorizontalScrollView) findViewById(R.id.hsl);
tv_1 = (TextView) findViewById(R.id.tv_1);
tv_2 = (TextView) findViewById(R.id.tv_2);
tv_3 = (TextView) findViewById(R.id.tv_3);
tv_4 = (TextView) findViewById(R.id.tv_4);
tv_context = (TextView) findViewById(R.id.tv_context);
tv_1.setOnClickListener(this);
tv_2.setOnClickListener(this);
tv_3.setOnClickListener(this);
tv_4.setOnClickListener(this);
}
@Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.tv_1:
tv_context.setText(tv_1.getText().toString());
hsl.retore();
break;
case R.id.tv_2:
tv_context.setText(tv_2.getText().toString());
hsl.retore();
break;
case R.id.tv_3:
tv_context.setText(tv_3.getText().toString());
hsl.retore();
break;
case R.id.tv_4:
tv_context.setText(tv_4.getText().toString());
hsl.retore();
break;
}
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.menu,menu);
return super.onCreateOptionsMenu(menu);
}
}
------------------我是一个小广告纯绿色---相信程序员需要的不只是技术-------------------
https://shop60162341.taobao.com/ 点击打开链接