转载于: http://blog.csdn.net/dany1202/article/details/6274349
android源码有关窗口小部件的所支持的东西确实很少,用起来也很别手,2.2版的窗口小部件应该也只是用作一种被动的显示。其连最基本的手势事件也不支持。当然,HTC已经实现了小部件的onTouch事件,并根据手势做动作。
本人工作之余,利用加班或空闲时间,断断续续的研究RemoteView几周的时间,总算实现了小部件的onTouch,并使其识别手势。简单分享:
方法一,提供一个较笨的只能实现部分简单功能的方法:
在framework层自己写一个View,使其继承自所要响应onTouch方法的控件,并实现手势操作
[java] view plain copy print ?
- package android.widget;
- import android.content.Context;
- import android.util.AttributeSet;
- import android.util.Log;
- import android.view.GestureDetector;
- import android.view.MotionEvent;
- import android.widget.TextView;
- import android.widget.RemoteViews.RemoteView;
- @RemoteView
- public class TouchTextView extends TextView {
- private GestureDetector mGestureDetector = null;
- public TouchTextView(Context context, AttributeSet attrs) {
- super(context, attrs);
- mGestureDetector = new GestureDetector(context, new LearnGestureListener());
- }
-
-
- public boolean onTouchEvent(MotionEvent event) {
- return mGestureDetector.onTouchEvent(event);
- }
- class LearnGestureListener extends GestureDetector.SimpleOnGestureListener {
- @Override
- public boolean onSingleTapUp(MotionEvent ev) {
- Log.d("DEBUG","onSingleTapUp");
- return true;
- }
- @Override
- public void onShowPress(MotionEvent ev) {
- Log.d("DEBUG","onShowPress");
- }
- @Override
- public void onLongPress(MotionEvent ev) {
- Log.d("DEBUG","onLongPress");
- }
- @Override
- public boolean onScroll(MotionEvent e1, MotionEvent e2,
- float distanceX, float distanceY) {
- Log.d("DEBUG","onScroll");
- return true;
- }
- @Override
- public boolean onDown(MotionEvent ev) {
- Log.d("DEBUG","onDownd");
- return true;
- }
- @Override
- public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX,
- float velocityY) {
- Log.d("DEBUG","onFling");
- return true;
- }
- public boolean onDoubleTap(MotionEvent event){
- Log.d("DEBUG","onDoubleTap");
- return true;
- }
- }
-
- }
package android.widget; import android.content.Context; import android.util.AttributeSet; import android.util.Log; import android.view.GestureDetector; import android.view.MotionEvent; import android.widget.TextView; import android.widget.RemoteViews.RemoteView; @RemoteView public class TouchTextView extends TextView { private GestureDetector mGestureDetector = null; public TouchTextView(Context context, AttributeSet attrs) { super(context, attrs); mGestureDetector = new GestureDetector(context, new LearnGestureListener()); } public boolean onTouchEvent(MotionEvent event) { return mGestureDetector.onTouchEvent(event); } class LearnGestureListener extends GestureDetector.SimpleOnGestureListener { @Override public boolean onSingleTapUp(MotionEvent ev) { Log.d("DEBUG","onSingleTapUp"); return true; } @Override public void onShowPress(MotionEvent ev) { Log.d("DEBUG","onShowPress"); } @Override public void onLongPress(MotionEvent ev) { Log.d("DEBUG","onLongPress"); } @Override public boolean onScroll(MotionEvent e1, MotionEvent e2, float distanceX, float distanceY) { Log.d("DEBUG","onScroll"); return true; } @Override public boolean onDown(MotionEvent ev) { Log.d("DEBUG","onDownd"); return true; } @Override public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX, float velocityY) { Log.d("DEBUG","onFling"); return true; } public boolean onDoubleTap(MotionEvent event){ Log.d("DEBUG","onDoubleTap"); return true; } } }
利用此办法,即可实现手势识别,但缺点很严重,就是想要响应的所有手势事件都必须在framework层实现,这样很笨,也很麻烦,但确实也算一种方法吧。使用时在xml文件中直接定义即可
<TouchTextView
android:id="@+id/note_widget"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
方法二,在RemoteView中添加一个setOnTouchPendingIntent事件
当然,我最初的想法也是如此,不过添加这个onTouch事件确实耗费了好一段时间,而且由于在本地编译整个工程,每次编译都要1个多小时,很是头疼,还好最终总算是实现了。
提供思路:在RemoteView中仿照setOnClickPendingIntent事件写一个setOnTouchPendingIntent事件,并将event传递出来,在onReceive中接受。定义手势,重写手势中的onScroll等方法,通过mGestureDetector.onTouchEvent(event); 去响应即可
[java] view plain copy print ?
- 响应手势的方法说明:
- GestureDetector mGestureDetector = new GestureDetector(context, new LearnGestureListener());
- mGestureDetector.onTouchEvent(event);
- class LearnGestureListener extends GestureDetector.SimpleOnGestureListener {
- @Override
- public boolean onSingleTapUp(MotionEvent ev) {
- Log.d("DEBUG","onSingleTapUp");
- return true;
- }
- 。。。
- }