iis服务器助手广告广告
返回顶部
首页 > 资讯 > 移动开发 >Android自定义布局实现仿qq侧滑部分代码
  • 925
分享到

Android自定义布局实现仿qq侧滑部分代码

布局Android 2022-06-06 01:06:59 925人浏览 独家记忆
摘要

自定义布局实现仿qq侧滑部分Android代码,供大家参考,具体内容如下 源码DEMO地址:https://GitHub.com/applelili/ImitationQQ

自定义布局实现仿qq侧滑部分Android代码,供大家参考,具体内容如下

源码DEMO地址:https://GitHub.com/applelili/ImitationQQ

实现说明:

通过自定义布局实现:

SlidingLayout继承于 HorizontalScrollView



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");
}
//获取左侧菜单view
leftView = (ViewGroup) container.getChildAt(0);
//获取主布局的Viwe
contentView = (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);
}

@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://手指抬起的时候判断是否关闭
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);
 }
}
您可能感兴趣的文章:Android高仿QQ6.0侧滑删除实例代码Android使用ViewDragHelper实现仿QQ6.0侧滑界面(一)Android使用ViewDragHelper实现QQ6.X最新版本侧滑界面效果实例代码Android滑动优化高仿QQ6.0侧滑菜单(滑动优化)Android使用DrawerLayout实现仿QQ双向侧滑菜单基于Android实现仿QQ5.0侧滑Android基于ViewDragHelper仿QQ5.0侧滑界面效果Android程序开发之使用Design包实现QQ动画侧滑效果和滑动菜单导航Android自定义view系列之99.99%实现QQ侧滑删除效果实例代码详解Android仿QQ6.0主页面侧滑效果


--结束END--

本文标题: Android自定义布局实现仿qq侧滑部分代码

本文链接: https://www.lsjlt.com/news/21925.html(转载时请注明来源链接)

有问题或投稿请发送至: 邮箱/279061341@qq.com    QQ/279061341

本篇文章演示代码以及资料文档资料下载

下载Word文档到电脑,方便收藏和打印~

下载Word文档
猜你喜欢
软考高级职称资格查询
编程网,编程工程师的家园,是目前国内优秀的开源技术社区之一,形成了由开源软件库、代码分享、资讯、协作翻译、讨论区和博客等几大频道内容,为IT开发者提供了一个发现、使用、并交流开源技术的平台。
  • 官方手机版

  • 微信公众号

  • 商务合作