android 悬浮图片滑动

来源:互联网 发布:梦想海贼王超进化数据 编辑:程序博客网 时间:2024/06/11 05:24

1. 前言

类似iphone的AssistiveTouch,可以跟随手指,全屏随意滑动。
通过这篇文章后,http://blog.csdn.net/dfskhgalshgkajghljgh/article/details/51967260,应该做一个这种效果非常简单。

2.选用何种方式

1.scrollTo/scrollBy:操作简单,适合对view的内容的滑动,同样也可以对view的父元素滑动,从而让子元素位置改变。 【不合适】
2平移动画实现滑动:操作简单,适用于没有交互的view和有复杂动画效果的。 【可以】
3.改变view的layoutparams使得view重新布局,从而实现滑动:操作复杂,适用于有交互的view。【可以】

3.实现

本文选用第三种实现,具体代码如下所示:
(1)自定义一个view,实现onTounchEvent()方法,移动逻辑都在这个方法里面,代码如下:

package com.view.viewtest;import android.content.Context;import android.util.AttributeSet;import android.view.MotionEvent;import android.widget.RelativeLayout;import android.widget.TextView;public class MyTextView extends TextView {    public MyTextView(Context context) {        super(context);    }    public MyTextView(Context context, AttributeSet attrs) {        super(context, attrs);    }    public MyTextView(Context context, AttributeSet attrs, int defStyleAttr) {        super(context, attrs, defStyleAttr);    }    int mLastx = -1;    int mLasty = -1;    @Override    public boolean onTouchEvent(MotionEvent event) {        int x = (int) event.getRawX();        int y = (int) event.getRawY();        switch (event.getAction()) {            case MotionEvent.ACTION_DOWN:                break;            case MotionEvent.ACTION_MOVE:                if (mLasty != -1) {                    RelativeLayout.LayoutParams linearParams = (RelativeLayout.LayoutParams)this.getLayoutParams();                    linearParams.leftMargin +=(x-mLastx);                    linearParams.topMargin +=(y-mLasty);                    this.setLayoutParams(linearParams);                }                break;            case MotionEvent.ACTION_UP:                break;            default:                break;        }        mLastx = x;        mLasty = y;        return true;    }}

(2)xml中引入自定义view

<?xml version="1.0" encoding="utf-8"?><RelativeLayout    xmlns:android="http://schemas.android.com/apk/res/android"    xmlns:tools="http://schemas.android.com/tools"    android:layout_width="match_parent"    android:layout_height="match_parent"    tools:context="com.view.viewtest.MainActivity">    <LinearLayout        android:layout_width="match_parent"        android:layout_height="wrap_content"        android:orientation="vertical">        <LinearLayout            android:id="@+id/linear_layout"            android:layout_width="match_parent"            android:layout_height="200dp"            android:background="#dddddd"            android:orientation="vertical">            <TextView                android:id="@+id/bitmap"                android:layout_width="100dp"                android:layout_height="100dp"                android:layout_marginLeft="150dp"                android:background="@mipmap/ic_launcher"/>        </LinearLayout>        <Button            android:id="@+id/scrollTo"            android:layout_width="match_parent"            android:layout_height="40dp"            android:text="scrollTo"/>        <Button            android:id="@+id/scrollBy"            android:layout_width="match_parent"            android:layout_height="40dp"            android:text="scrollBy"/>        <Button            android:id="@+id/animation"            android:layout_width="match_parent"            android:layout_height="40dp"            android:text="Animation"/>        <Button            android:id="@+id/params"            android:layout_width="match_parent"            android:layout_height="40dp"            android:text="params"/>        <TextView            android:id="@+id/postion"            android:layout_width="match_parent"            android:layout_height="wrap_content"            />    </LinearLayout>    <com.view.viewtest.MyTextView        android:id="@+id/mytext_view"        android:layout_width="100dp"        android:layout_height="100dp"        android:layout_marginLeft="150dp"        android:layout_marginTop="150dp"        android:background="@mipmap/ic_launcher"/></RelativeLayout>

4.效果

这里写图片描述这里写图片描述
解释:通过手指安准最下面的图标,拖到最右边,其实跟iphone的AssistiveTouch效果一样。

代码地址:http://download.csdn.net/detail/dfskhgalshgkajghljgh/9581241

1 0