首页 文章 精选 留言 我的

精选列表

搜索[文档处理],共10017篇文章
优秀的个人博客,低调大师

Android官方开发文档Training系列课程中文版:手势处理之拖拽或缩放

原文地址:https://developer.android.com/training/gestures/scale.html 这节课主要学习如何使用触摸手势来拖动、放大屏幕上的对象。 拖动对象 如果你的重点在Android 3.0以上的版本,那么你可以使用内置的拖拽事件监听器View.OnDragListener。 触摸手势最常见的操作就是使用它来拖动屏幕上的对象。下面的代码允许用户拖动屏幕上的图像。要注意以下几点: 在拖动操作中,APP会一直保持手指拖动的轨迹,就算是另一只手指触到屏幕也是。举个例子,想象一根手指在拖动着一张图像,这时用户将第二根手指放置到屏幕上,如果APP只是追踪单根手指的轨迹,那么它会将第二根手指作为默认位置,并会将图像移动到这个位置。 为了防止这样的事件发生,APP需要区分第一根手指与其它手指。为此,需要追踪 ACTION_POINTER_DOWN 及 ACTION_POINTER_UP 。ACTION_POINTER_DOWN 及 ACTION_POINTER_UP在第二根手指落下或抬起的时候由onTouchEvent()方法传回。 在ACTION_POINTER_UP的情况下,示例提取了这个事件的索引,并确保当前活动的指针不是那个已经不在屏幕上的指针。如果是那个指针的话,那么APP会选择一个不同的指针使其活动并保存它的X及Y的位置。一旦这个值被保存下来,那么APP将会使用正确指针的数据一直计算剩余移动的距离。 下面的代码允许用户在屏幕上拖动对象。它记录了当前活动指针的初始位置,计算了它所位移的距离,并将对象移动到新的位置上。 这里要注意,代码段使用了getActionMasked()方法。你应该一直使用这个方法来接收MotionEvent对象的活动。与getAction()方法不同,getActionMasked()工作于多点触控模式下。它会返回被执行的掩饰活动,不包括指针的索引比特。 // The ‘active pointer’ is the one currently moving our object. private int mActivePointerId = INVALID_POINTER_ID; @Override public boolean onTouchEvent(MotionEvent ev) { // Let the ScaleGestureDetector inspect all events. mScaleDetector.onTouchEvent(ev); final int action = MotionEventCompat.getActionMasked(ev); switch (action) { case MotionEvent.ACTION_DOWN: { final int pointerIndex = MotionEventCompat.getActionIndex(ev); final float x = MotionEventCompat.getX(ev, pointerIndex); final float y = MotionEventCompat.getY(ev, pointerIndex); // Remember where we started (for dragging) mLastTouchX = x; mLastTouchY = y; // Save the ID of this pointer (for dragging) mActivePointerId = MotionEventCompat.getPointerId(ev, 0); break; } case MotionEvent.ACTION_MOVE: { // Find the index of the active pointer and fetch its position final int pointerIndex = MotionEventCompat.findPointerIndex(ev, mActivePointerId); final float x = MotionEventCompat.getX(ev, pointerIndex); final float y = MotionEventCompat.getY(ev, pointerIndex); // Calculate the distance moved final float dx = x - mLastTouchX; final float dy = y - mLastTouchY; mPosX += dx; mPosY += dy; invalidate(); // Remember this touch position for the next move event mLastTouchX = x; mLastTouchY = y; break; } case MotionEvent.ACTION_UP: { mActivePointerId = INVALID_POINTER_ID; break; } case MotionEvent.ACTION_CANCEL: { mActivePointerId = INVALID_POINTER_ID; break; } case MotionEvent.ACTION_POINTER_UP: { final int pointerIndex = MotionEventCompat.getActionIndex(ev); final int pointerId = MotionEventCompat.getPointerId(ev, pointerIndex); if (pointerId == mActivePointerId) { // This was our active pointer going up. Choose a new // active pointer and adjust accordingly. final int newPointerIndex = pointerIndex == 0 ? 1 : 0; mLastTouchX = MotionEventCompat.getX(ev, newPointerIndex); mLastTouchY = MotionEventCompat.getY(ev, newPointerIndex); mActivePointerId = MotionEventCompat.getPointerId(ev, newPointerIndex); } break; } } return true; } 平移 上面的部分展示了如何在屏幕上拖动对象。另一个通用的场景就是平移了,平移的意思是:用户的拖动动作引起的x及y轴方向上的滚动。上面的代码直接将MotionEvent拦截实现拖动。这部分的代码将会采用另一种更具有优势的方法,以便支持通用手势。它重写了GestureDetector.SimpleOnGestureListener的onScroll()方法。 只有用户在使用手指移动内容时,onScroll()才会被调用。onScroll()只有在手指按下的时候才会调用,一旦手指离开屏幕,那么平移手势也随之终止。 下面是onScroll()的使用摘要: // The current viewport. This rectangle represents the currently visible // chart domain and range. private RectF mCurrentViewport = new RectF(AXIS_X_MIN, AXIS_Y_MIN, AXIS_X_MAX, AXIS_Y_MAX); // The current destination rectangle (in pixel coordinates) into which the // chart data should be drawn. private Rect mContentRect; private final GestureDetector.SimpleOnGestureListener mGestureListener = new GestureDetector.SimpleOnGestureListener() { ... @Override public boolean onScroll(MotionEvent e1, MotionEvent e2, float distanceX, float distanceY) { // Scrolling uses math based on the viewport (as opposed to math using pixels). // Pixel offset is the offset in screen pixels, while viewport offset is the // offset within the current viewport. float viewportOffsetX = distanceX * mCurrentViewport.width() / mContentRect.width(); float viewportOffsetY = -distanceY * mCurrentViewport.height() / mContentRect.height(); ... // Updates the viewport, refreshes the display. setViewportBottomLeft( mCurrentViewport.left + viewportOffsetX, mCurrentViewport.bottom + viewportOffsetY); ... return true; } 下面是setViewportBottomLeft()方法的实现,它主要实现了移动内容的逻辑: /** * Sets the current viewport (defined by mCurrentViewport) to the given * X and Y positions. Note that the Y value represents the topmost pixel position, * and thus the bottom of the mCurrentViewport rectangle. */ private void setViewportBottomLeft(float x, float y) { /* * Constrains within the scroll range. The scroll range is simply the viewport * extremes (AXIS_X_MAX, etc.) minus the viewport size. For example, if the * extremes were 0 and 10, and the viewport size was 2, the scroll range would * be 0 to 8. */ float curWidth = mCurrentViewport.width(); float curHeight = mCurrentViewport.height(); x = Math.max(AXIS_X_MIN, Math.min(x, AXIS_X_MAX - curWidth)); y = Math.max(AXIS_Y_MIN + curHeight, Math.min(y, AXIS_Y_MAX)); mCurrentViewport.set(x, y - curHeight, x + curWidth, y); // Invalidates the View to update the display. ViewCompat.postInvalidateOnAnimation(this); } 缩放 在Detecting Common Gestures中,我们讨论到GestureDetector可以帮助我们来检测比如滑动、滚动、长按等手势。而对于缩放,Android提供了ScaleGestureDetector. GestureDetector 以及 ScaleGestureDetector。 为了可以反馈检测到的手势事件,手势探测器使用了监听器对象ScaleGestureDetector.OnScaleGestureListener。如果你只关心部分手势的话,Android还提供了ScaleGestureDetector.SimpleOnScaleGestureListener,你可以通过重写它的方法来使用。 缩放基础示例 下面的代码是缩放所需要的基础: private ScaleGestureDetector mScaleDetector; private float mScaleFactor = 1.f; public MyCustomView(Context mContext){ ... // View code goes here ... mScaleDetector = new ScaleGestureDetector(context, new ScaleListener()); } @Override public boolean onTouchEvent(MotionEvent ev) { // Let the ScaleGestureDetector inspect all events. mScaleDetector.onTouchEvent(ev); return true; } @Override public void onDraw(Canvas canvas) { super.onDraw(canvas); canvas.save(); canvas.scale(mScaleFactor, mScaleFactor); ... // onDraw() code goes here ... canvas.restore(); } private class ScaleListener extends ScaleGestureDetector.SimpleOnScaleGestureListener { @Override public boolean onScale(ScaleGestureDetector detector) { mScaleFactor *= detector.getScaleFactor(); // Don't let the object get too small or too large. mScaleFactor = Math.max(0.1f, Math.min(mScaleFactor, 5.0f)); invalidate(); return true; } } 稍微复杂点的示例 下面是一个稍微复杂一点的示例,它摘自与这节课所提供的示例InteractiveChart(PS:示例工程请参见原网页)。InteractiveChart同时支持平移、缩放,它使用了ScaleGestureDetector的“平移”(getCurrentSpanX/Y)及“焦点” (getFocusX/Y)特性: @Override private RectF mCurrentViewport = new RectF(AXIS_X_MIN, AXIS_Y_MIN, AXIS_X_MAX, AXIS_Y_MAX); private Rect mContentRect; private ScaleGestureDetector mScaleGestureDetector; ... public boolean onTouchEvent(MotionEvent event) { boolean retVal = mScaleGestureDetector.onTouchEvent(event); retVal = mGestureDetector.onTouchEvent(event) || retVal; return retVal || super.onTouchEvent(event); } /** * The scale listener, used for handling multi-finger scale gestures. */ private final ScaleGestureDetector.OnScaleGestureListener mScaleGestureListener = new ScaleGestureDetector.SimpleOnScaleGestureListener() { /** * This is the active focal point in terms of the viewport. Could be a local * variable but kept here to minimize per-frame allocations. */ private PointF viewportFocus = new PointF(); private float lastSpanX; private float lastSpanY; // Detects that new pointers are going down. @Override public boolean onScaleBegin(ScaleGestureDetector scaleGestureDetector) { lastSpanX = ScaleGestureDetectorCompat. getCurrentSpanX(scaleGestureDetector); lastSpanY = ScaleGestureDetectorCompat. getCurrentSpanY(scaleGestureDetector); return true; } @Override public boolean onScale(ScaleGestureDetector scaleGestureDetector) { float spanX = ScaleGestureDetectorCompat. getCurrentSpanX(scaleGestureDetector); float spanY = ScaleGestureDetectorCompat. getCurrentSpanY(scaleGestureDetector); float newWidth = lastSpanX / spanX * mCurrentViewport.width(); float newHeight = lastSpanY / spanY * mCurrentViewport.height(); float focusX = scaleGestureDetector.getFocusX(); float focusY = scaleGestureDetector.getFocusY(); // Makes sure that the chart point is within the chart region. // See the sample for the implementation of hitTest(). hitTest(scaleGestureDetector.getFocusX(), scaleGestureDetector.getFocusY(), viewportFocus); mCurrentViewport.set( viewportFocus.x - newWidth * (focusX - mContentRect.left) / mContentRect.width(), viewportFocus.y - newHeight * (mContentRect.bottom - focusY) / mContentRect.height(), 0, 0); mCurrentViewport.right = mCurrentViewport.left + newWidth; mCurrentViewport.bottom = mCurrentViewport.top + newHeight; ... // Invalidates the View to update the display. ViewCompat.postInvalidateOnAnimation(InteractiveLineGraphView.this); lastSpanX = spanX; lastSpanY = spanY; return true; } };

优秀的个人博客,低调大师

Android官方开发文档Training系列课程中文版:手势处理之记录手指移动的轨迹

原文地址:http://android.xsoftlab.net/training/gestures/movement.html 这节课将会学习如何在触摸事件中记录手指移动的轨迹。 当手指触摸的位置、压力或者尺寸发生变化时,ACTION_MOVE事件就会被触发。与Detecting Common Gestures中描述的一样,所有的事件都被记录在一个MotionEvent对象中。 因为基于手指的触摸并不是很精确的交互方式,所以检测触摸事件的行为需要更多的轨迹点。为了帮助APP区分基于轨迹的手势(比如滑动等移动的手势)与非轨迹手势(比如单点等不移动的手势),Android提出了一个名为”touch slop”的概念。Touch slop指的是用户按下的以像素为单位的距离。 这里有若干项不同的追踪手势轨迹的方法,具体使用哪个方法取决于应用程序的需求: 指针的起始位置与结束位置。 指针位移的方向,由X,Y的坐标判断。 历史记录,你可以通过getHistorySize()获得手势的历史尺寸。然后可以通过getHistorical(Value)方法获得这些历史事件的位置,尺寸,事件以及压力。当渲染手指的轨迹时,比如在屏幕上用手指画线条等,历史记录这时就会派上用场。 指针在屏幕上滑动的速度。 轨迹的速度 在记录手势的特性或者在检查何种手势事件发生时,除了要依靠手指移动的距离、方向这两个要素之外。还需要另外一个非常重要的因素就是速度。为了使速度计算更加容易,Android为此提供了VelocityTracker类以及VelocityTrackerCompat类。VelocityTracker用于辅助记录触摸事件的速度。这对于判断哪个速度是手势的标准部分,比如飞速滑动。 下面的例子用于演示在VelocityTracker API中方法的目的: public class MainActivity extends Activity { private static final String DEBUG_TAG = "Velocity"; ... private VelocityTracker mVelocityTracker = null; @Override public boolean onTouchEvent(MotionEvent event) { int index = event.getActionIndex(); int action = event.getActionMasked(); int pointerId = event.getPointerId(index); switch(action) { case MotionEvent.ACTION_DOWN: if(mVelocityTracker == null) { // Retrieve a new VelocityTracker object to watch the velocity of a motion. mVelocityTracker = VelocityTracker.obtain(); } else { // Reset the velocity tracker back to its initial state. mVelocityTracker.clear(); } // Add a user's movement to the tracker. mVelocityTracker.addMovement(event); break; case MotionEvent.ACTION_MOVE: mVelocityTracker.addMovement(event); // When you want to determine the velocity, call // computeCurrentVelocity(). Then call getXVelocity() // and getYVelocity() to retrieve the velocity for each pointer ID. mVelocityTracker.computeCurrentVelocity(1000); // Log velocity of pixels per second // Best practice to use VelocityTrackerCompat where possible. Log.d("", "X velocity: " + VelocityTrackerCompat.getXVelocity(mVelocityTracker, pointerId)); Log.d("", "Y velocity: " + VelocityTrackerCompat.getYVelocity(mVelocityTracker, pointerId)); break; case MotionEvent.ACTION_UP: case MotionEvent.ACTION_CANCEL: // Return a VelocityTracker object back to be re-used by others. mVelocityTracker.recycle(); break; } return true; } Note: 注意,应当在ACTION_MOVE事件内部计算速度,不要在ACTION_UP内部计算,因为在ACTION_UP内部计算所得到的X与Y的速度值都是0.

优秀的个人博客,低调大师

Android官方开发文档Training系列课程中文版:键盘输入处理之指定输入的类型

原文地址:http://android.xsoftlab.net/training/keyboard-input/index.html 引言 在文本框接收到焦点时,Android系统会在屏幕上显示一个软键盘。为了提供良好的用户体验,你可以指定相关输入类型的特性,以及输入法应当如何展现。 除了屏幕上的软键盘之外,Android还支持实体键盘,所以APP如何与各种类型的键盘交互这件事情,就变得很重要了。 指定输入的类型 每一个文本框必定只有一种输入类型,比如一个电子邮件地址,一个电话号码或者是常规文本。所以为每一个文本框指定输入类型就变得很重要,这样的话系统才会显示正确的输入法。 你可以指定比如输入方法所提供的拼写建议、首字母大写、以及输入法右下角按钮的行为(Done或者Next)。这节课主要介绍如何指定这些特性。 指定键盘类型 你应该总是为文本框声明输入类型,通过android:inputType属性可以为文本框添加输入类型。 比如,如果你希望文本框的输入类型为电话号码,可以使用”phone”: <EditText android:id="@+id/phone" android:layout_width="fill_parent" android:layout_height="wrap_content" android:hint="@string/phone_hint" android:inputType="phone" /> 或者如果文本框主要是用于输入密码的,可以使用”textPassword”隐藏用户的输入文本: <EditText android:id="@+id/password" android:hint="@string/password_hint" android:inputType="textPassword" ... /> android:inputType含有多种指定的输入类型,并且一些值可以组合使用。 开启拼写检查与其它功能 android:inputType属性允许你可以为输入类型指定多种行为。更重要的一点是,如果文本框的重点在基础文本输入上(如文本消息),你应当使用”textAutoCorrect”开启拼写检查。 你还可以为android:inputType属性指定多种不同的行为以及输入类型。比如,下面的例子就展示了如何同时开启首字母大写以及拼写检查的功能: <EditText android:id="@+id/message" android:layout_width="wrap_content" android:layout_height="wrap_content" android:inputType= "textCapSentences|textAutoCorrect" ... /> 指定输入法按钮的行为 大多数的输入法都在右下角提供了一个用户功能按钮,这对于当前的文本框来说是极为恰当的。在默认情况下,系统使用这个按钮来实现Next或者Done功能。除非你的文本框允许多行情况的出现(比如使用了android:inputType=”textMultiLine”)。在这种情况下,该功能按钮是一个回车按钮。然而,你可以指定一些更加符合你文本框的特别功能,比如Send或Go。 为了指定键盘的功能按钮,需要使用属性android:imeOptions,并需要执行比如”actionSend”或”actionSearch”之类的值: <EditText android:id="@+id/search" android:layout_width="fill_parent" android:layout_height="wrap_content" android:hint="@string/search_hint" android:inputType="text" android:imeOptions="actionSend" /> 接下来可以通过TextView.OnEditorActionListener来监听功能按钮的按下事件,并需要在该监听器内响应正确的IME功能ID,该ID定义与EditorInfo中,比如下面使用的就是IME_ACTION_SEND: EditText editText = (EditText) findViewById(R.id.search); editText.setOnEditorActionListener(new OnEditorActionListener() { @Override public boolean onEditorAction(TextView v, int actionId, KeyEvent event) { boolean handled = false; if (actionId == EditorInfo.IME_ACTION_SEND) { sendMessage(); handled = true; } return handled; } });

优秀的个人博客,低调大师

Android官方开发文档Training系列课程中文版:键盘输入处理之控制输入法的显示方式

原文地址:http://android.xsoftlab.net/training/keyboard-input/visibility.html 当输入的焦点进入或者离开文本框时,Android会适时的显示或隐藏输入法。系统还会决定UI及文本框如何出现在输入法的上方。比如,当垂直方向上的可用空间非常紧张时,那么文本框可能就会填充输入法上方的整个区域。对于大多数的APP来说,这样的默认行为是它们所需要的。 不过在另一些情况中,你可能需要直接控制输入法的显示方式,以及需要在输入法可见的时候控制UI的排布方式。那么这节课主要就是介绍如何实现这些。 在Activity启动的时候显示输入法 尽管在Activity启动的时候Android将焦点给了第一个文本框,但是它是不会触发输入法弹出的。这样的行为是符合正常的习惯的,因为进入文本框可能不会Activity启动后的首要任务。不管怎么说,如果进入文本框是Activity的首要任务的话(比如登录界面),那么你可能希望默认情况下进入Activity后就会弹出输入法。 为了在Activity启动后可以显示输入法,需要在清单文件中对应的Activity的元素中添加属性android:windowSoftInputMode。如下: <application ... > <activity android:windowSoftInputMode="stateVisible" ... > ... </activity> ... </application> Note:如果用户的设备含有实体按键,那么软键盘是不会弹出的。 按需求弹出输入法 如果在Activity的生命周期内有这么一个方法:你希望确保在该方法调用后输入法是可见的,那么你可以使用InputMethodManager来将它弹出。 举个例子,下面的方法持有了一个View对象,用户会在这个View内部输入点什么,所以调用requestFocus()方法可以将焦点赋给它,然后showSoftInput()就会将输入法打开: public void showSoftKeyboard(View view) { if (view.requestFocus()) { InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE); imm.showSoftInput(view, InputMethodManager.SHOW_IMPLICIT); } } Note: 一旦输入法被弹出,那么最好不要使用代码隐藏它。系统会在用户终止文本框中的任务后将它隐藏或者用户通过系统隐藏了它(比如使用了返回按钮)。 指明你的UI应该如何响应输入法的弹出 当输入法出现在屏幕上时,它会减少APP在屏幕上的使用空间。那么系统会决定如何调整UI的部分区域,但是它可能不是最准确的。为了确保APP拥有最佳的用户体验,应该指明系统如何调整UI。 为了声明Activity的首选方式,应当在项目的清单文件中对应的Activity下添加android:windowSoftInputMode属性,并使用其中一个含有”adjust”的值。 举个例子,为了确保系统可将UI调整到可用区域,应当使用”adjustResize”: <application ... > <activity android:windowSoftInputMode="adjustResize" ... > ... </activity> ... </application> 除了以上的方法,你还可以使用组合的的方式来声明UI调整规则与输入法的可见性规则: <activity android:windowSoftInputMode="stateVisible|adjustResize" ... > ... </activity> 指明”adjustResize”是很重要的:如果UI中含有一些用户可能需要迅速访问的按键或者需要操作的文本框的话。举个例子,如果你使用相对布局将一个按钮放置到了屏幕的底部,那么使用”adjustResize”调整布局可以使该按钮出现在输入法的顶部,这样在输入完成之后就可以直接点击该按钮。

资源下载

更多资源
Mario

Mario

马里奥是站在游戏界顶峰的超人气多面角色。马里奥靠吃蘑菇成长,特征是大鼻子、头戴帽子、身穿背带裤,还留着胡子。与他的双胞胎兄弟路易基一起,长年担任任天堂的招牌角色。

腾讯云软件源

腾讯云软件源

为解决软件依赖安装时官方源访问速度慢的问题,腾讯云为一些软件搭建了缓存服务。您可以通过使用腾讯云软件源站来提升依赖包的安装速度。为了方便用户自由搭建服务架构,目前腾讯云软件源站支持公网访问和内网访问。

Rocky Linux

Rocky Linux

Rocky Linux(中文名:洛基)是由Gregory Kurtzer于2020年12月发起的企业级Linux发行版,作为CentOS稳定版停止维护后与RHEL(Red Hat Enterprise Linux)完全兼容的开源替代方案,由社区拥有并管理,支持x86_64、aarch64等架构。其通过重新编译RHEL源代码提供长期稳定性,采用模块化包装和SELinux安全架构,默认包含GNOME桌面环境及XFS文件系统,支持十年生命周期更新。

WebStorm

WebStorm

WebStorm 是jetbrains公司旗下一款JavaScript 开发工具。目前已经被广大中国JS开发者誉为“Web前端开发神器”、“最强大的HTML5编辑器”、“最智能的JavaScript IDE”等。与IntelliJ IDEA同源,继承了IntelliJ IDEA强大的JS部分的功能。

用户登录
用户注册