Android view隨觸碰滑動效果
主要思路是通過父布局的onTouch(),方法,獲取滑動到的位置和點(diǎn)擊下的位置,再去設(shè)置子view的位置。我的代碼中考慮了在邊緣情況。需要注意的是,使用RelativeLayout,以imageView為例。從測試結(jié)果來看,bottomMargin 和rightMargin 性能非常差,最好還是用leftMargin與topMargin定位。
下面是運(yùn)行效果:

布局文件里面就是一個Relativelayout中有一個ImageView。如下
<?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:id="@+id/relativeLayout" android:layout_width="match_parent" android:layout_height="match_parent" tools:context="com.xingyi.moveviewwithtouch.MainActivity"> <ImageView android:id="@+id/imageView" android:layout_width="40dp" android:layout_height="40dp" android:background="@android:color/black"/> </RelativeLayout>
Java代碼如下,這里考慮了邊緣位置滑動的效果。如果考慮,在最左邊緣imageView會有一半在屏幕之外,在最右邊緣會縮小,直到看不見。
package com.xingyi.moveviewwithtouch;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.MotionEvent;
import android.view.View;
import android.widget.ImageView;
import android.widget.RelativeLayout;
public class MainActivity extends AppCompatActivity {
ImageView imageView;
RelativeLayout relativeLayout;
int heightRL,widthRL;
int halfHeight,halfWidth;
boolean first=true;
private int widthImg;
private int heightImg;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
initView();
}
//初始化視圖
private void initView() {
imageView = (ImageView) findViewById(R.id.imageView);
relativeLayout = (RelativeLayout) findViewById(R.id.relativeLayout);
//獲取滑動瞬間位置和點(diǎn)擊瞬間位置,并移動imageview
relativeLayout.setOnTouchListener(new View.OnTouchListener() {
@Override
public boolean onTouch(View view, MotionEvent motionEvent) {
switch (motionEvent.getAction()) {
case MotionEvent.ACTION_MOVE:
moveView(imageView, motionEvent.getX(), motionEvent.getY());
break;
case MotionEvent.ACTION_DOWN:
getWidthAndHeight();
moveView(imageView, motionEvent.getX(), motionEvent.getY());
break;
default:
break;
}
return true;
}
});
}
//因?yàn)椴荒茉诔跏蓟晥D時獲得長寬,而每次計算一次長寬又影響性能
private void getWidthAndHeight(){
if(first){
widthRL=relativeLayout.getWidth();
heightRL=relativeLayout.getHeight();
widthImg=imageView.getWidth();
heightImg=imageView.getHeight();
halfWidth = imageView.getWidth() / 2;//imageView寬度的一半
halfHeight = imageView.getHeight() / 2;//imageView高度的一半
first=false;
}
}
//滑動瞬間,將x和y分別作imageView的中心點(diǎn)到relativeLayout最左和頂端距離
private void moveView(View view, float x, float y) {
RelativeLayout.LayoutParams params = (RelativeLayout.LayoutParams) view.getLayoutParams();
//設(shè)置水平位置
if (x < halfWidth) {//左邊緣
params.leftMargin = 0;//設(shè)置imageview到左端距離為0
} else if (x > widthRL- halfWidth) {
params.leftMargin = widthRL-widthImg;//設(shè)置imageview左端到左端端距離(params.rightMargin的性能非常糟糕)
} else {
params.leftMargin = (int) (x - halfWidth);//imageview左端到relativelayout左端距離
}
//設(shè)置豎直位置
if (y < halfHeight) {
params.topMargin = 0;
} else if (y > heightRL - halfHeight) {
params.topMargin = heightRL-widthImg;//params.bottomMargin的性能非常糟糕
} else {
params.topMargin = (int) (y - halfHeight);
}
view.setLayoutParams(params);
}
}
總結(jié)
以上所述是小編給大家介紹的Android view隨觸碰滑動效果,希望對大家有所幫助,如果大家有任何疑問請給我留言,小編會及時回復(fù)大家的。在此也非常感謝大家對腳本之家網(wǎng)站的支持!
相關(guān)文章
Android 開發(fā)之Dialog中隱藏鍵盤的正確使用方法
這篇文章主要介紹了Android 開發(fā)之Dialog中隱藏鍵盤的正確使用方法的相關(guān)資料,主要說明Dialog 隱藏鍵盤的注意事項(xiàng),需要的朋友可以參考下2017-09-09
Android 使用Vitamio打造自己的萬能播放器(9)—— 在線播放 (在線電視)
本文主要介紹Android 使用Vitamio開發(fā)播放器,實(shí)現(xiàn)在線電視播放,這里提供效果圖和實(shí)例代碼以便大家參考,2016-07-07
Android文件下載進(jìn)度條的實(shí)現(xiàn)代碼
我們今天開始學(xué)習(xí)的是下載進(jìn)度的實(shí)現(xiàn)。今天的這段代碼是網(wǎng)上找的,自己做了些小改,通過模擬器測試。文件下載進(jìn)度條控制(就是為了高清壁紙加個進(jìn)度條),自己研究了好久,但是進(jìn)度條只能顯示緩存寫入文件的進(jìn)度,不能顯示下載進(jìn)度。找了好久,終于找到一段用的代碼,所以記錄下來,大家分享2013-01-01
Android利用Theme自定義Activity間的切換動畫
這篇文章主要為大家詳細(xì)介紹了Android利用Theme自定義Activity間的切換動畫,感興趣的小伙伴們可以參考一下2016-09-09
Android實(shí)現(xiàn)兩臺手機(jī)屏幕共享和遠(yuǎn)程控制功能
在遠(yuǎn)程協(xié)助、在線教學(xué)、技術(shù)支持等多種場景下,實(shí)時獲得另一部移動設(shè)備的屏幕畫面,并對其進(jìn)行操作,具有極高的應(yīng)用價值,本項(xiàng)目旨在實(shí)現(xiàn)兩臺 Android 手機(jī)之間的屏幕共享與遠(yuǎn)程控制,需要的朋友可以參考下2025-04-04
Android中使用HttpURLConnection實(shí)現(xiàn)GET POST JSON數(shù)據(jù)與下載圖片
這篇文章主要介紹了Android中使用HttpURLConnection實(shí)現(xiàn)GET POST JSON數(shù)據(jù)與下載圖片,需要的朋友可以參考下2016-01-01
Android獲取、更改包名的小技巧分享(超實(shí)用)
這篇文章主要給大家分享介紹了關(guān)于利用Android更改包名的一個小技巧,通過這個方法大家可以很方便的修改包名,再也不用為頻繁修改包名而感到頭疼,文中還給大家分享利一個android獲取手機(jī)所有應(yīng)用包名的方法,下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧。2017-12-12
Android中Item實(shí)現(xiàn)點(diǎn)擊水波紋效果
這篇文章主要給大家介紹了關(guān)于Android中Item實(shí)現(xiàn)點(diǎn)擊水波紋效果的相關(guān)資料,文中通過示例代碼介紹的非常詳細(xì),對各位Android開發(fā)者們具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2018-11-11
Android?Flutter實(shí)現(xiàn)搜索的三種方式詳解
這篇文章主要為大家詳細(xì)介紹了Android?Flutter實(shí)現(xiàn)搜索的三種方式,文中的示例代碼講解詳細(xì),具有一定的借鑒價值,感興趣的可以了解一下2022-08-08

