利用ViewDragHelper打造悬浮球效果
来源:互联网 发布:linux 链接文件夹 编辑:程序博客网 时间:2024/04/28 11:53
效果图
自定义布局
import android.content.Context;import android.graphics.Point;import android.support.v4.widget.ViewDragHelper;import android.util.AttributeSet;import android.view.MotionEvent;import android.view.View;import android.widget.RelativeLayout;/** * Created by yebin on 2016/12/8. */public class VDHLayout extends RelativeLayout{ private View autoBackView; private ViewDragHelper mDragger; private Point mAutoBackOriginPos=new Point(); private int mWidth; private int mHeight; public VDHLayout(Context context, AttributeSet attrs) { super(context, attrs); } public void addBackView(View view){ this.autoBackView=view; if(autoBackView!=null){ addView(autoBackView); inintViewDragHelper(); } } private void inintViewDragHelper() { mDragger=ViewDragHelper.create(this, 1.0f, new ViewDragHelper.Callback() { @Override public boolean tryCaptureView(View child, int pointerId) { return child==autoBackView; } @Override public int clampViewPositionHorizontal(View child, int left, int dx) { int leftBound=getPaddingLeft(); int rightBound=getWidth()-child.getWidth()-leftBound; int newLeft=Math.min(Math.max(left,leftBound),rightBound); return newLeft; } @Override public int clampViewPositionVertical(View child, int top, int dy) { int topBound=getPaddingTop(); int bottomBound=getHeight()-child.getHeight()-topBound; int newTop=Math.min(Math.max(top,topBound),bottomBound); return newTop; } @Override public void onViewPositionChanged(View changedView, int left, int top, int dx, int dy) { super.onViewPositionChanged(changedView, left, top, dx, dy); if(changedView==autoBackView){ mAutoBackOriginPos.y=top; if(left<mWidth/2){ mAutoBackOriginPos.x=0; }else{ mAutoBackOriginPos.x=mWidth-autoBackView.getWidth(); } } } @Override public int getViewHorizontalDragRange(View child) { return getMeasuredWidth()-child.getMeasuredWidth(); } @Override public int getViewVerticalDragRange(View child) { return getMeasuredHeight()-child.getMeasuredHeight(); } @Override public void onViewReleased(View releasedChild, float xvel, float yvel) { if(releasedChild==autoBackView){ mDragger.settleCapturedViewAt(mAutoBackOriginPos.x,mAutoBackOriginPos.y); invalidate(); } } }); } @Override public void computeScroll() { // super.computeScroll(); if(mDragger!=null){ if(mDragger.continueSettling(true)){ invalidate(); } } } @Override public boolean onTouchEvent(MotionEvent event) { if(mDragger!=null){ mDragger.processTouchEvent(event); } return true; } @Override public boolean onInterceptTouchEvent(MotionEvent ev) { return mDragger!=null?mDragger.shouldInterceptTouchEvent(ev):super.onInterceptTouchEvent(ev); } @Override protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) { super.onMeasure(widthMeasureSpec, heightMeasureSpec); } @Override protected void onLayout(boolean changed, int l, int t, int r, int b) { super.onLayout(changed, l, t, r, b); mWidth=getWidth(); mHeight=getHeight(); }}
XML布局文件
<?xml version="1.0" encoding="utf-8"?><com.example.xunfuqiu.VDHLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:id="@+id/rl_content" > <TextView android:layout_width="match_parent" android:layout_height="wrap_content" android:gravity="center" android:text="Hello World!" /> <ImageView android:layout_width="200dp" android:layout_height="200dp" android:src="@mipmap/ic_launcher" android:layout_centerInParent="true" /> <EditText android:layout_width="match_parent" android:layout_height="wrap_content" android:hint="请输入文字。。。" android:textSize="16sp" android:layout_alignParentBottom="true" android:layout_marginBottom="20dp" /></com.example.xunfuqiu.VDHLayout>
MainActivty使用
import android.support.v7.app.AppCompatActivity;import android.os.Bundle;import android.util.Log;import android.view.View;import android.view.ViewGroup;import android.view.WindowManager;import android.widget.ImageView;import android.widget.RelativeLayout;import android.widget.Toast;public class MainActivity extends AppCompatActivity { private VDHLayout layout; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); layout = ((VDHLayout) this.findViewById(R.id.rl_content)); ImageView iv=new ImageView(this); RelativeLayout.LayoutParams params=new RelativeLayout.LayoutParams( RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.WRAP_CONTENT); iv.setImageResource(R.mipmap.ic_launcher); params.addRule(RelativeLayout.ALIGN_PARENT_RIGHT); params.addRule(RelativeLayout.CENTER_VERTICAL); iv.setLayoutParams(params); iv.bringToFront(); layout.addBackView(iv); iv.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { Toast.makeText(MainActivity.this, "00000000000000", Toast.LENGTH_SHORT).show(); } }); }}
1 0
- 利用ViewDragHelper打造悬浮球效果
- 利用ViewDragHelper实现菜鸟裹裹的悬浮按钮效果
- RecyclerView:打造悬浮效果
- 【Android】RecyclerView:打造悬浮效果
- 使用ItemDecoration打造列表顶部悬浮效果
- 快速打造 RecyclerView 悬浮吸顶效果
- 利用ViewDragHelper实现3d效果的View
- 利用bootstrap实现悬浮窗口的效果
- 利用bootstrap实现悬浮窗口的效果
- Android RecyclerView 打造带悬浮效果的联系人
- ViewDragHelper实战 自己打造Drawerlayout
- ViewDragHelper实战 自己打造Drawerlayout
- ViewDragHelper实战 自己打造Drawerlayout
- ViewDragHelper实战 自己打造Drawerlayout
- ViewDragHelper实战 自己打造Drawerlayout
- ViewDragHelper实战 自己打造Drawerlayout
- ViewDragHelper实战 自己打造Drawerlayout
- 悬浮效果
- 在其它任何的主机上以root身份登录
- rocketmq学习笔记 六 流程之取消息
- lvs、haproxy、nginx 负载均衡的比较分析
- Try
- 【C#】55. .Net中的并发集合——ConcurrentQueue
- 利用ViewDragHelper打造悬浮球效果
- 第2章Gherkins与Feature
- 1622-5 孔富晨 总结《2016年12月12日》 【连续第73天总结】
- Iometer存储测试工具参数说明-图形主界面(整理)
- jiffies溢出与时间先后比较
- $.ajax()
- linux awk 基本命令
- html form标签
- easyui dialog 关闭窗口的时候,必填框显示在TIP