日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 运维知识 > Android >内容正文

Android

android交互功能,Android 用户界面交互---拖放(OnDragListener)

發布時間:2023/12/2 Android 34 豆豆
生活随笔 收集整理的這篇文章主要介紹了 android交互功能,Android 用户界面交互---拖放(OnDragListener) 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

設計拖放操作

本節主要內容如下:

1.??如何開始拖拽;

2.??在拖拽期間如何響應事件;

3.??如何響應落下事件;

4.??如何結束拖放操作。

開始拖拽

用戶使用一個拖拽手勢開始拖拽,通常是在View對象上長按。在響應中,應該做下列事情:

1.??必要時,給要移動的數據創建一個ClipData和ClipData.Item對象,作為ClipData對象的一部分,在ClipData對象內部的ClipDescription對象中保存了元數據。因為拖放操作不代表數據的移動,因此可以使用null來代替實際的對象。

例如,以下代碼段顯示了如何在ImageView對象的長按事件上創建一個包含ImageView對象標簽的ClipData對象。// Create a string for the ImageView label

private static final String IMAGEVIEW_TAG = "icon bitmap"

// Creates a new ImageView

ImageView imageView = new ImageView(this);

// Sets the bitmap for the ImageView from an icon bit map (defined elsewhere)

imageView.setImageBitmap(mIconBitmap);

// Sets the tag

imageView.setTag(IMAGEVIEW_TAG);

...

// Sets a long click listener for the ImageView using an anonymous listener object that

// implements the OnLongClickListener interface

imageView.setOnLongClickListener(new View.OnLongClickListener() {

// Defines the one method for the interface, which is called when the View is long-clicked

public boolean onLongClick(View v) {

// Create a new ClipData.

// This is done in two steps to provide clarity. The convenience method

// ClipData.newPlainText() can create a plain text ClipData in one step.

// Create a new ClipData.Item from the ImageView object's tag

ClipData.Item item = new ClipData.Item(v.getTag());

// Create a new ClipData using the tag as a label, the plain text MIME type, and

// the already-created item. This will create a new ClipDescription object within the

// ClipData, and set its MIME type entry to "text/plain"

ClipData dragData = new ClipData(v.getTag(),ClipData.MIMETYPE_TEXT_PLAIN,item);

// Instantiates the drag shadow builder.

View.DragShadowBuilder myShadow = new MyDragShadowBuilder(imageView);

// Starts the drag

v.startDrag(dragData,? // the data to be dragged

myShadow,? // the drag shadow builder

null,????? // no need to use local data

0????????? // flags (not currently used, set to 0)

);

}

}

2.?以下代碼段定義了一個myDragShadowBuilder類,它創建一個用于拖拽TextView對象的小的灰色的矩形作為拖拽影子:private static class MyDragShadowBuilder extends View.DragShadowBuilder {

// The drag shadow image, defined as a drawable thing

private static Drawable shadow;

// Defines the constructor for myDragShadowBuilder

public MyDragShadowBuilder(View v) {

// Stores the View parameter passed to myDragShadowBuilder.

super(v);

// Creates a draggable image that will fill the Canvas provided by the system.

shadow = new ColorDrawable(Color.LTGRAY);

}

// Defines a callback that sends the drag shadow dimensions and touch point back to the

// system.

@Override

public void onProvideShadowMetrics (Point size, Point touch)

// Defines local variables

private int width, height;

// Sets the width of the shadow to half the width of the original View

width = getView().getWidth() / 2;

// Sets the height of the shadow to half the height of the original View

height = getView().getHeight() / 2;

// The drag shadow is a ColorDrawable. This sets its dimensions to be the same as the

// Canvas that the system will provide. As a result, the drag shadow will fill the

// Canvas.

shadow.setBounds(0, 0, width, height);

// Sets the size parameter's width and height values. These get back to the system

// through the size parameter.

size.set(width, height);

// Sets the touch point's position to be in the middle of the drag shadow

touch.set(width / 2, height / 2);

}

// Defines a callback that draws the drag shadow in a Canvas that the system constructs

// from the dimensions passed in onProvideShadowMetrics().

@Override

public void onDrawShadow(Canvas canvas) {

// Draws the ColorDrawable in the Canvas passed in from the system.

shadow.draw(canvas);

}

}

注意:不必擴展View.DragShadowBuilder類,因為構造器View.DragShadowBuilder(View)會創建一個默認的跟傳遞給它的View對象相同尺寸的拖拽影子,而且觸點在拖拽影子的中心。

對拖拽開始的響應

在拖拽操作期間,系統會分發拖拽事件給當前布局中View對象的拖拽事件監聽器。監聽器應該通過調用getAction()方法對獲得的操作類型做出反應。在拖拽開始時,這個方法返回ACTION_DRAG_STARTED.

在對ACTION_DRAG_STARTED操作類型做出的響應中,監聽器應該做下列事情:

1.??調用getClipDescription()方法來獲得ClipDescription對象,使用ClipDescription對象中的MIME類型方法來判斷監聽器是否能夠接收被拖拽的數據。

如果拖拽操作不代表要移動的數據,這個判斷就不是必須的了。

2.??如果監聽器能夠接受落下事件,它應該返回true。這樣就告訴系統可以繼續給這個監聽器發送拖拽事件。如果不能夠接收落下事件,它應該返回false,系統就不再會給這個監聽器發送拖拽事件了。

要注意的是針對ACTION_DRAG_STARTED事件,下列DragEvent對象方法不能獲取有效的數據:getClipData()、getX()、getY()和getResult()。

在拖拽期間處理事件

在拖拽期間,在響應ACTION_DARG_STARTED拖拽事件中返回true的監聽器會繼續接收拖拽事件。這種類型的拖拽監聽器會依賴拖拽期間拖拽影子的位置和監聽器的View對象的可見性來接收拖拽事件。

在拖拽期間,監聽器主要使用拖拽事件來判斷是否應該改變View對象的外觀。

拖拽期間,getAction方法會返回下列三個值之一:

1.?ACTION_DRAG_ENTERED:當觸點(觸屏上手指下方的點)進入監聽器View對象的邊框時,View對象的拖拽監聽器就會收到這個事件。

2.?ACTION_DRAG_LOCATION:一旦拖拽監聽器收到一個ACTION_DRAG_ENTERED事件,并且在收到ACTION_DRAG_EXITED事件之前,每次移動觸點時都會收到一個新的ACTION_DRAG_LOCATION事件。getX和getY()方法會返回觸點的X和Y軸坐標。

3.?ACTION_DRAG_EXITED:在拖拽影子離開監聽器View對象的邊框之后,這個事件會發送給之前收到ACTION_DRAG_ENTERED事件的那個監聽器。

監聽器不需要對這些操作類型都做出反應,如果監聽器給系統返回了一個值,它就會被忽略。以下是響應這些操作類型的一些指南:

1.??在對ACTION_DRAG_ENTERED或ACTION_DRAG_LOCATION事件的響應中,監聽器能夠改變View對象的外觀來指示View對象能夠接受放下事件。

2.?ACTION_DRAG_LOCATION事件包含了對getX()和getY()方法有效的數據,這兩個方法的返回值對應了觸點的位置。監聽器可以使用這個信息來修改觸點所在的View對象的外觀,也能使用這個信息來判斷用戶拖放陰影的準確位置。

3.?在對ACTION_DRAG_EXITED事件的響應中,監聽器應該重設在響應ACTION_DRAG_ENTERED或ACTION_DRAG_LOCATION事件對外觀的任何改變。這個事件指示拖放對象已經離開準備放下的目標。

響應放下事件

當用戶在應用中的一個View對象上釋放了拖拽影子,并且這個View對象是之前報告的能夠接收被拖拽內容的那個View對象,系統就會給這個View對象發送一個ACTION_DROP類型的拖拽事件。監聽器應該做下列事情:

1.??調用getClipData()方法獲得ClipData對象,這個對象在調用startDrag()方法時被初始化并保存在拖拽監聽器中。如果拖放操作不移動數據,那么就不需要做這件事;

2.??返回true,指示放下事件被成功處理,否則返回false。對于ACTION_DRAG_ENDED事件,這個返回值就是通過getResult()方法返回的值。

要注意的是,如果系統不發送ACTION_DROP事件,針對對ACTION_DRAG_ENDED事件的getResult()方法調用的返回值是false。

系統允許用戶在監聽器不接收拖放事件的View對象之上釋放拖拽影子,也允許用戶在應用程序UI的空白區域或應用程序以外的區域釋放拖拽影子,這樣系統就不會發出ACTION_DROP類型的事件,直接會發出一個ACTION_DRAG_ENDED事件。

響應拖拽結束事件

用戶釋放了拖拽影子后,系統會立即給應用程序中所有的拖拽事件監聽器發送ACTION_DRAG_ENDED類型的拖拽事件,指示拖拽操作結束了。

每個監聽器都應該做下列事情:

1.??如果監聽器在操作期間改變了View對象的外觀,那么應該把View對象重設為默認的外觀。這是對用戶可見的操作結束的指示;

2.??監聽器能夠可選的調用getResult()方法來查找更多的相關操作。如果在響應ACTION_DROP類型的事件中監聽器返回了true,那么getResult()方法也會返回true。在其他的情況中,getResult()方法會返回false,包括系統沒有發出ACTION_DROP事件的情況;

3.??監聽器應該給系統返回true。

響應拖拽事件的一個例子:

所有的拖拽事件都會被拖拽事件的回調方法或監聽器接收。以下代碼片段是一個簡單的在監聽器中對拖拽事件作出反應的示例。// Creates a new drag event listener

mDragListen = new myDragEventListener();

View imageView = new ImageView(this);

// Sets the drag event listener for the View

imageView.setOnDragListener(mDragListen);

...

protected class myDragEventListener implements View.OnDragEventListener {

// This is the method that the system calls when it dispatches a drag event to the

// listener.

public boolean onDrag(View v, DragEvent event) {

// Defines a variable to store the action type for the incoming event

final int action = event.getAction();

// Handles each of the expected events

switch(action) {

case DragEvent.ACTION_DRAG_STARTED:

// Determines if this View can accept the dragged data

if (event.getClipDescription().hasMimeType(ClipDescription.MIMETYPE_TEXT_PLAIN)) {

// As an example of what your application might do,

// applies a blue color tint to the View to indicate that it can accept

// data.

v.setColorFilter(Color.BLUE);

// Invalidate the view to force a redraw in the new tint

v.invalidate();

// returns true to indicate that the View can accept the dragged data.

return(true);

} else {

// Returns false. During the current drag and drop operation, this View will

// not receive events again until ACTION_DRAG_ENDED is sent.

return(false);

}

break;

case DragEvent.ACTION_DRAG_ENTERED: {

// Applies a green tint to the View. Return true; the return value is ignored.

v.setColorFilter(Color.GREEN);

// Invalidate the view to force a redraw in the new tint

v.invalidate();

return(true);

break;

case DragEvent.ACTION_DRAG_LOCATION:

// Ignore the event

return(true);

break;

case DragEvent.ACTION_DRAG_EXITED:

// Re-sets the color tint to blue. Returns true; the return value is ignored.

v.setColorFilter(Color.BLUE);

// Invalidate the view to force a redraw in the new tint

v.invalidate();

return(true);

break;

case DragEvent.ACTION_DROP:

// Gets the item containing the dragged data

ClipData.Item item = event.getClipData().getItemAt(0);

// Gets the text data from the item.

dragData = item.getText();

// Displays a message containing the dragged data.

Toast.makeText(this, "Dragged data is " + dragData, Toast.LENGTH_LONG);

// Turns off any color tints

v.clearColorFilter();

// Invalidates the view to force a redraw

v.invalidate();

// Returns true. DragEvent.getResult() will return true.

return(true);

break;

case DragEvent.ACTION_DRAG_ENDED:

// Turns off any color tinting

v.clearColorFilter();

// Invalidates the view to force a redraw

v.invalidate();

// Does a getResult(), and displays what happened.

if (event.getResult()) {

Toast.makeText(this, "The drop was handled.", Toast.LENGTH_LONG);

} else {

Toast.makeText(this, "The drop didn't work.", Toast.LENGTH_LONG);

};

// returns true; the value is ignored.

return(true);

break;

// An unknown action type was received.

default:

Log.e("DragDrop Example","Unknown action type received by OnDragListener.");

break;

};

};

};

總結

以上是生活随笔為你收集整理的android交互功能,Android 用户界面交互---拖放(OnDragListener)的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。