国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 系統 > Android > 正文

Android自定義布局實現仿qq側滑部分代碼

2019-12-12 03:13:32
字體:
來源:轉載
供稿:網友

自定義布局實現仿qq側滑部分Android代碼,供大家參考,具體內容如下

源碼DEMO地址:https://github.com/applelili/ImitationQQ

實現說明:

通過自定義布局實現:

SlidingLayout繼承于 HorizontalScrollView

/*** Created by Administrator on 2017/3/29.*/public class SlidingLayout extends HorizontalScrollView{/** 左側右邊間距 */private float rightPadding;/** 左側菜單的寬度 */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);//計算左側菜單的寬度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");}//獲取左側菜單viewleftView = (ViewGroup) container.getChildAt(0);//獲取主布局的ViwecontentView = (ViewGroup) container.getChildAt(1);//設置子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);}/*** 該方法在滑動的時候會不斷的調用* @param l : left* @param t* @param oldl* @param oldt*/@Overrideprotected 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);}@Overridepublic boolean onTouchEvent(MotionEvent ev) {switch (ev.getAction()) {case MotionEvent.ACTION_UP://手指抬起的時候判斷是否關閉int currentX = getScrollX();if (isOpenMeun) {if (currentX >= leftWidth / 2) {closeMeun();} else {openMeun();}//點擊關閉float x = ev.getX();if (x > leftWidth) {closeMeun();}return true;} else {//關閉狀態if (currentX < leftWidth / 2) {openMeun();} else {closeMeun();}return true;}}return super.onTouchEvent(ev);}/** 關閉菜單 */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); }}

以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持武林網。

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 桃江县| 五原县| 公主岭市| 项城市| 郧西县| 汉中市| 仪陇县| 潢川县| 百色市| 云浮市| 喀喇沁旗| 保康县| 铜陵市| 镇坪县| 肇庆市| 三明市| 蒙山县| 额敏县| 石家庄市| 灌云县| 云和县| 禹城市| 陆丰市| 玉环县| 河津市| 宁河县| 深泽县| 万州区| 石城县| 鹤岗市| 台湾省| 义马市| 台湾省| 瑞昌市| 涞水县| 镇赉县| 唐山市| 康定县| 独山县| 邵阳县| 福建省|