时间:2021-05-19
自定义布局实现仿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); }}以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。
声明:本页内容来源网络,仅供用户参考;我单位不保证亦不表示资料全面及准确无误,也不保证亦不表示这些资料为最新信息,如因任何原因,本网内容或者用户因倚赖本网内容造成任何损失或损害,我单位将不会负任何法律责任。如涉及版权问题,请提交至online#300.cn邮箱联系删除。
Android自定义SwipeLayout仿QQ侧滑条目,供大家参考,具体内容如下先看动图看布局文件activity_main.xml
本课程将带领大家通过自定义控件实现QQ5.0侧滑菜单,课程将循序渐进,首先实现最普通的侧滑菜单,然后引入属性动画与拖动菜单效果相结合,最终实现QQ5.0侧滑菜单
前面已经讲过通过三方开源库SlideMenu来实现这种效果,请参考Android实现网易新闻客户端侧滑菜单(一)今天通过自定义View来实现这种功能。代码如下:
本文介绍了Android仿微信自定义数字键盘的实现代码,分享给大家,希望对大家有帮助最终效果:实现这个自定义键盘的思路很简单:要写出一个数字键盘的布局;与Edi
一、概述 在App中,经常会出现侧滑菜单,侧滑滑出View等效果,虽然说Android有很多第三方开源库,但是实际上咱们可以自己也写一个自定义的侧滑View控