步骤1
关联
compile 'com.android.support:design:24.2.0'
步骤2 自定义可拖拽的控件 并完成拖拽的逻辑
package com.example.xietiaozhe;
import android.content.Context;
import android.support.design.widget.CoordinatorLayout;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.view.View;
/**
* Name: MyView
* Action:
* Author: liuan
* creatTime:2017-01-21 21:37
*/
public class MyView extends View{
private double lastY;
private double lastX;
public MyView(Context context) {
super(context);
}
public MyView(Context context, AttributeSet attrs) {
super(context, attrs);
}
public MyView(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
}
@Override
public boolean onTouchEvent(MotionEvent event) {
//获取当前用户手指位置
int touchX = (int) event.getRawX();
int touchY = (int) event.getRawY();
switch (event.getAction()) {
case MotionEvent.ACTION_DOWN:
break;
case MotionEvent.ACTION_MOVE:
// 计算两次拖拽的偏移量 并求该当前控件的位置
int offsetX=(int)(touchX-lastX);
int offsetY=(int)(touchY-lastY);
CoordinatorLayout.MarginLayoutParams p= (CoordinatorLayout.MarginLayoutParams) getLayoutParams();
p.leftMargin+=offsetX;
p.topMargin+=offsetY;
setLayoutParams(p);
break;
case MotionEvent.ACTION_UP:
break;
}
//移动后记录新位置 供下一次移动计算位置使用
lastX=touchX;
lastY=touchY;
return true;
}
}
步骤3 自定义行为 继承 自CoordubatirLayout
package com.example.xietiaozhe;
import android.content.Context;
import android.support.design.widget.CoordinatorLayout;
import android.support.v4.view.ViewCompat;
import android.util.AttributeSet;
import android.view.View;
import android.widget.Button;
/**
* Name: MyBehavior
* Action:
* Author: liuan
* creatTime:2017-01-21 21:53
*/
//自定义协调者布局的属性
public class MyBehavior extends CoordinatorLayout.Behavior<Button> {
//空参和有参必须重写出来 不然会报错...
public MyBehavior() {
}
public MyBehavior(Context context, AttributeSet attrs) {
super(context, attrs);
}
//当初始化界面时 卸掉着布局 会调用此方法 询问 是否将child和MyBehavior相关联 如果返回true
//则表示两个控件有关联
@Override
public boolean layoutDependsOn(CoordinatorLayout parent, Button child, View dependency) {
return dependency instanceof MyView;
}
// 当dependency的layout反生变化 协调者布局会调用此方法
@Override
public boolean onDependentViewChanged(CoordinatorLayout parent, Button child, View dependency) {
int top = dependency.getTop();
//比列
float percent=top/1080f;
ViewCompat.setAlpha(child,percent);
return super.onDependentViewChanged(parent, child, dependency);
}
}
步骤4 布局
<?xml version="1.0" encoding="utf-8"?>
<android.support.design.widget.CoordinatorLayout 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"
tools:context="com.example.xietiaozhe.MainActivity">
<Button
android:layout_width="100dp"
android:layout_height="100dp"
android:background="#f00"
app:layout_behavior="com.example.xietiaozhe.MyBehavior" />
<com.example.xietiaozhe.MyView
android:layout_width="100dp"
android:layout_height="100dp"
android:layout_marginTop="200dp"
android:background="#0f0"/>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="可移动布局"
/>
</LinearLayout>
</android.support.design.widget.CoordinatorLayout>
步骤5 啥也不用干 运行...