Android自定義布局實現(xiàn)仿qq側(cè)滑部分代碼
自定義布局實現(xiàn)仿qq側(cè)滑部分Android代碼,供大家參考,具體內(nèi)容如下
源碼DEMO地址:https://github.com/applelili/ImitationQQ

實現(xiàn)說明:
通過自定義布局實現(xiàn):
SlidingLayout繼承于 HorizontalScrollView
/**
* Created by Administrator on 2017/3/29.
*/
public class SlidingLayout extends HorizontalScrollView{
/** 左側(cè)右邊間距 */
private float rightPadding;
/** 左側(cè)菜單的寬度 */
private int leftWidth;
private ViewGroup leftView;
private ViewGroup contentView;
private final Context context;
private boolean isOpenMeun = true;
private ImageView shadowView;
public SlidingLayout(Context context) {
this(context,null);
}
public SlidingLayout(Context context, AttributeSet attrs) {
this(context, attrs,0);
}
public SlidingLayout(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
this.context = context;
//獲取自定義的屬性
TypedArray typedArray=context.obtainStyledAttributes(attrs,R.styleable.SlidingLayout);
rightPadding=typedArray.getDimension(R.styleable.SlidingLayout_rightPadding,80);
//計算左側(cè)菜單的寬度
leftWidth = (int) (getScreenWidth() - rightPadding + 0.5f);
}
//獲取屏幕的寬度
private float getScreenWidth() {
return getResources().getDisplayMetrics().widthPixels;
}
@Override /** 布局解析完畢的時候 */
protected void onFinishInflate() {
super.onFinishInflate();
ViewGroup container= (ViewGroup) getChildAt(0);
if(container.getChildCount() > 2){
throw new IllegalStateException("SlidingLayout中只能放兩個子View");
}
//獲取左側(cè)菜單view
leftView = (ViewGroup) container.getChildAt(0);
//獲取主布局的Viwe
contentView = (ViewGroup) container.getChildAt(1);
//設(shè)置子view 的寬度
leftView.getLayoutParams().width = leftWidth;
contentView.getLayoutParams().width = (int) getScreenWidth();
//移除父布局
container.removeView(contentView);
FrameLayout frameLayout=new FrameLayout(context);
frameLayout.setLayoutParams(new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT,ViewGroup.LayoutParams.MATCH_PARENT));
frameLayout.addView(contentView);
//添加陰影
shadowView = new ImageView(context);
shadowView.setBackgroundColor(Color.parseColor("#99000000"));
frameLayout.addView(shadowView);
container.addView(frameLayout);
}
/**
* 該方法在滑動的時候會不斷的調(diào)用
* @param l : left
* @param t
* @param oldl
* @param oldt
*/
@Override
protected void onScrollChanged(int l, int t, int oldl, int oldt) {
super.onScrollChanged(l, t, oldl, oldt);
float x=l*0.8f;//偏移量
leftView.setTranslationX(x);//平移
float color = 1 - l * 1.0f / leftWidth;
shadowView.setAlpha(color);
}
@Override
public boolean onTouchEvent(MotionEvent ev) {
switch (ev.getAction()) {
case MotionEvent.ACTION_UP://手指抬起的時候判斷是否關(guān)閉
int currentX = getScrollX();
if (isOpenMeun) {
if (currentX >= leftWidth / 2) {
closeMeun();
} else {
openMeun();
}
//點擊關(guān)閉
float x = ev.getX();
if (x > leftWidth) {
closeMeun();
}
return true;
} else {//關(guān)閉狀態(tài)
if (currentX < leftWidth / 2) {
openMeun();
} else {
closeMeun();
}
return true;
}
}
return super.onTouchEvent(ev);
}
/** 關(guān)閉菜單 */
public void closeMeun(){
isOpenMeun = false;
smoothScrollTo(leftWidth,0);// 250ms
}
/** 打開菜單 */
public void openMeun(){
isOpenMeun = true;
smoothScrollTo(0,0);
}
}
attrs屬性文件
<?xml version="1.0" encoding="utf-8"?> <resources> <declare-styleable name="SlidingLayout"> <attr name="rightPadding" format="dimension"/> </declare-styleable> </resources>
布局方面
<?xml version="1.0" encoding="utf-8"?> <com.example.myqq.SlidingLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" app:rightPadding="65dp" tools:context="com.example.myqq.MainActivity"> <LinearLayout android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="horizontal"> <include layout="@layout/left_main" /> <include layout="@layout/right_main" /> </LinearLayout> </com.example.myqq.SlidingLayout>
activity
package com.example.myqq;
import android.animation.ObjectAnimator;
import android.annotation.TargetApi;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.WindowManager;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.ListView;
public class MainActivity extends AppCompatActivity {
private String strings[] = {"開通會員", "QQ錢包", "個性裝扮", "我的收藏", "我的相冊", "我的文件", "我的日程", "我的名片夾"};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setState();
setContentView(R.layout.activity_main);
ListView listView= (ListView) findViewById(R.id.list_left);
listView.setDividerHeight(0);
listView.setAdapter(new ArrayAdapter<>(this,android.R.layout.simple_list_item_1,strings));
ImageView bgimg1= (ImageView) findViewById(R.id.bgimg);
float currentY=bgimg1.getTranslationY();
ObjectAnimator animator = ObjectAnimator.ofFloat(bgimg1, "translationY", currentY, -100, -40, currentY);
animator.setDuration(5000);
animator.setRepeatCount(ObjectAnimator.INFINITE);
animator.start();
}
@TargetApi(20)
private void setState() {
WindowManager.LayoutParams params=new WindowManager.LayoutParams();
params.flags=WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS;
getWindow().setAttributes(params);
}
}
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
- Android高仿QQ6.0側(cè)滑刪除實例代碼
- Android使用ViewDragHelper實現(xiàn)仿QQ6.0側(cè)滑界面(一)
- Android使用ViewDragHelper實現(xiàn)QQ6.X最新版本側(cè)滑界面效果實例代碼
- Android滑動優(yōu)化高仿QQ6.0側(cè)滑菜單(滑動優(yōu)化)
- Android使用DrawerLayout實現(xiàn)仿QQ雙向側(cè)滑菜單
- 基于Android實現(xiàn)仿QQ5.0側(cè)滑
- Android基于ViewDragHelper仿QQ5.0側(cè)滑界面效果
- Android程序開發(fā)之使用Design包實現(xiàn)QQ動畫側(cè)滑效果和滑動菜單導(dǎo)航
- Android自定義view系列之99.99%實現(xiàn)QQ側(cè)滑刪除效果實例代碼詳解
- Android仿QQ6.0主頁面?zhèn)然Ч?/a>
相關(guān)文章
React Native開發(fā)中自動打包腳本的實例代碼
這篇文章主要介紹了React Native開發(fā)中自動打包腳本的實例代碼,非常不錯,具有一定的參考借鑒價值,需要的朋友可以參考下2018-09-09
Android 中自定義ContentProvider與ContentObserver的使用簡單實例
這篇文章主要介紹了Android 中自定義ContentProvider與ContentObserver的使用簡單實例的相關(guān)資料,這里提供實例幫助大家學(xué)習(xí)理解這部分內(nèi)容,需要的朋友可以參考下2017-09-09
使用adb命令向Android模擬器中導(dǎo)入通訊錄聯(lián)系人的方法
這篇文章主要介紹了使用adb命令向Android模擬器中導(dǎo)入通訊錄聯(lián)系人的方法,實例分析了導(dǎo)入通訊錄存儲文件的技巧,需要的朋友可以參考下2015-01-01
Android基礎(chǔ)之Fragment與Activity交互詳解
以下小編就為大家介紹一下Fragment跟Activity之間的關(guān)系。需要的朋友可以過來參考下2013-07-07
flutter PositionedTransition實現(xiàn)縮放動畫
這篇文章主要為大家詳細(xì)介紹了flutter PositionedTransition實現(xiàn)縮放動畫,文中示例代碼介紹的非常詳細(xì),具有一定的參考價值,感興趣的小伙伴們可以參考一下2019-07-07
Android?Flutter使用本地數(shù)據(jù)庫編寫備忘錄應(yīng)用
這篇文章主要為大家詳細(xì)介紹了Android?Flutter如何使用本地數(shù)據(jù)庫實現(xiàn)編寫簡單的備忘錄應(yīng)用,文中的示例代碼講解詳細(xì),感興趣的小伙伴可以了解一下2023-03-03
Android?DialogFragment使用之底部彈窗封裝示例
這篇文章主要為大家介紹了Android?DialogFragment使用之底部彈窗封裝示例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-09-09
Android自定義View實現(xiàn)隨機(jī)數(shù)驗證碼
這篇文章主要為大家詳細(xì)介紹了Android如何利用自定義View實現(xiàn)隨機(jī)數(shù)驗證碼效果,文中的示例代碼講解詳細(xì),感興趣的小伙伴可以了解一下2022-06-06

