欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  移动技术

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

程序员文章站 2022-06-14 09:33:31
自定义布局实现仿qq侧滑部分android代码,供大家参考,具体内容如下 源码demo地址:https://github.com/applelili/imitatio...

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

源码demo地址:https://github.com/applelili/imitationqq

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

实现说明:

通过自定义布局实现:

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");
}
//获取左侧菜单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);
}

/**
* 该方法在滑动的时候会不断的调用
* @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://手指抬起的时候判断是否关闭
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);

 }
}


以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。