久久婷婷香蕉热狠狠综合,精品无码国产自产拍在线观看蜜,寡妇房东在做爰3,中文字幕日本人妻久久久免费,国产成人精品三上悠亚久久

當前位置:首頁 > 嵌入式培訓 > 嵌入式學習 > 講師博文 > Android滑動效果(一)—Layout方(fang)法(fa)

Android滑動效果(一)—Layout方法 時間:2018-09-25    ;  來(lai)源(yuan):未知

上一(yi)(yi)篇我(wo)們(men)了解了Android的坐標系(xi)(xi)和觸(chu)控事(shi)件,今(jin)天我(wo)們(men)就來看看Android中如何使(shi)用系(xi)(xi)統提供(gong)的API來具體實(shi)(shi)現滑動(dong)效(xiao)(xiao)果(guo)。實(shi)(shi)現滑動(dong)效(xiao)(xiao)果(guo)的六種方法(fa)之Layout方法(fa),在這(zhe)里我(wo)們(men)要(yao)明(ming)確一(yi)(yi)件事(shi)情,無論是哪種方式(shi),滑動(dong)效(xiao)(xiao)果(guo)實(shi)(shi)現的基本思想是:當(dang)觸(chu)摸(mo)(mo)View時,系(xi)(xi)統記錄(lu)下當(dang)前的坐標系(xi)(xi);當(dang)手指移(yi)(yi)動(dong)時,系(xi)(xi)統記錄(lu)下移(yi)(yi)動(dong)后觸(chu)摸(mo)(mo)的坐標系(xi)(xi),從而獲取到相對于前一(yi)(yi)次坐標點的偏(pian)移(yi)(yi)量(liang),并(bing)通過(guo)偏(pian)移(yi)(yi)量(liang)來修改(gai)View的坐標,這(zhe)樣不斷重(zhong)復,從而實(shi)(shi)現Android的滑動(dong)效(xiao)(xiao)果(guo)。

下面,我們就(jiu)來看(kan)一個實例,來了解(jie)一下在(zai)Android中是如何實現滑動效果的(de)。

新建項目,然后自定義一個(ge)view,代碼如下:

package com.example.testdragview;

import android.content.Context;

import android.util.AttributeSet;

import android.util.Log;

import android.view.MotionEvent;

import android.view.View;

public class DragView extends View{

private int lastX;

private int lastY;

public DragView(Context context, AttributeSet attrs, int defStyleAttr) {

super(context, attrs, defStyleAttr);

}

 public DragView(Context context, AttributeSet attrs) {

super(context, attrs);

}

public DragView(Context context) {

super(context);

}

 public boolean onTouchEvent(MotionEvent event) {

//獲取到手指處的橫坐標(biao)和縱坐標(biao)

int x = (int) event.getX();

int y = (int) event.getY();

switch(event.getAction())

{

case MotionEvent.ACTION_DOWN:

lastX = x;

lastY = y;

break;

case MotionEvent.ACTION_MOVE:

//計(ji)算移動的(de)距(ju)離

int offX = x - lastX;

int offY = y - lastY;

//調(diao)用layout方(fang)法(fa)來(lai)重新放(fang)置它的位置

layout(getLeft()+offX, getTop()+offY,

getRight()+offX , getBottom()+offY);

break;

}

return true;

}

}

核心代碼就(jiu)(jiu)是onTouchEvent方法了。代碼很簡(jian)單,無非就(jiu)(jiu)是記(ji)錄手(shou)指的(de)上次坐標與下次坐標,然(ran)后將(jiang)前(qian)后移動的(de)增量(liang)傳(chuan)遞(di)給layout方法而已(yi)。

值得注意的是(shi),onTouchEvent的返回(hui)值為true,表示我們要成功消化掉這個觸摸事件(jian)。

然后再修改activity_main.xml的(de)代碼(ma),將(jiang)這個view裝到布局里(li),如下:

<LinearLayout xmlns:android="//schemas.android.com/apk/res/android"

xmlns:tools="//schemas.android.com/tools"

android:layout_width="match_parent"

android:layout_height="match_parent"

>

<com.example.testdragview.DragView   <>

android:layout_width="100dp"

android:layout_height="100dp"

android:background="#FF0000" />

</LinearLayout>

上一篇:Android滑動效果(一)- Android坐標研究

下一篇:C語言初始化效率問題以及關鍵字解釋

熱(re)點文章推薦
華清學(xue)員就(jiu)業榜(bang)單
高薪學(xue)員經驗(yan)分享
熱(re)點新(xin)聞推薦
前臺專線:010-82525158 企業培訓洽談專線(xian):010-82525379 院(yuan)校合作洽談專線:010-82525379 Copyright © 2004-2022 北京華清遠見科技集團有限公司 版權所有 ,,京公海網安備11010802025203號

回到頂部