Android development completely custom floating draggable view without floating permission

A custom View floating button Lib has good scalability, but the floating layout can be matched with other layouts

Vibration is used, so vibration permission is required. The permission has been added to the manifest, but above 6.0, you need to add it manually

   <uses-permission android:name="android.permission.VIBRATE" />

Instructions

Root layout in layout layout file

<?xml version="1.0" encoding="utf-8"?>
<com.isoftstone.floatlibrary.widget.FloatViewLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/layout_float"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

</com.isoftstone.floatlibrary.widget.FloatViewLayout>

 

Secondly, the floating button layout is needed. The reason why it is written in the layout form is that the expansion is good and any style can be realized.

 

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:orientation="vertical">

    <ImageView
        android:layout_width="@dimen/floating_icon_size"
        android:layout_height="@dimen/floating_icon_size"
        android:src="@mipmap/float_btn" />
</LinearLayout>

 

Only need to call related methods in Activity

 

public class MainActivity extends AppCompatActivity {
    private FloatViewLayout floatViewLayout;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        floatViewLayout = (FloatViewLayout) findViewById(R.id.layout_float);
        floatViewLayout.setmFloatView(new FloatViewImpl() {
            @Override
            public View createFloatView() {
                View view = LayoutInflater.from(MainActivity.this).inflate(R.layout.layout_float_view, null);
                return view;
            }

            @Override
            public int setFloatViewSideOffset() {
                return super.setFloatViewSideOffset();
            }

            @Override
            public boolean setEnableBackground() {
                return false;
            }
        });
    }
}


FloatViewImpl is a static class that has implemented setFloatViewSideOffset() and setEnableBackground() methods and given default values, which can be modified by yourself.
createFloatView() method: mainly used to create the floating button View
setFloatViewSideOffset() method: used to set the floating button and the edge offset, mainly used to indent a certain pixel
setEnableBackground() method: used to set whether to keep the sliding appearance The background color and realize the disappearance of the floating button

 

 

 

Source code

Welcome to my WeChat public account:


 

Guess you like

Origin blog.csdn.net/xhf_123/article/details/77698599