gpt4 book ai didi

android - Android中文本行的垂直滚动

转载 作者:塔克拉玛干 更新时间:2023-11-02 09:01:38 26 4
gpt4 key购买 nike

我已经实现了 Android Note Pad 示例代码中的编辑器代码。现在我想添加垂直滚动文本行的功能。我想要完成的一个例子是在 Andorid 中快速滚动选项行

我在 Google 上搜索了 scroller 和 fling 的例子,但我找不到任何适合我需要的东西。我还没有发现任何与我正在尝试做的事情相差甚远的事情。

最佳答案

我已经通过鲜血、汗水和泪水找到了我的问题的答案。我会把它贴在这里,希望它能帮助别人。以下方法位于 Android Note Pad 示例代码中的 LinedEditText 类中。

============================

    public void InitScroller(Context context) {
mScroller = new Scroller(context); // Get a scroller object
mScrollY = 0 ; // Set beginning of program as top of screen.
mMinScroll = getLineHeight ()/2; // Set minimum scroll distance
mFlingV = 750; // Minimum fling velocity

}

@Override
public boolean onTouchEvent(MotionEvent event) {
super.onTouchEvent(event);

if (mVelocityTracker == null) { // If we do not have velocity tracker
mVelocityTracker = VelocityTracker.obtain(); // then get one
}
mVelocityTracker.addMovement(event); // add this movement to it

final int action = event.getAction(); // Get action type
final float y = event.getY(); // Get the displacement for the action

switch (action) {

case MotionEvent.ACTION_DOWN: // User has touched screen
if (!mScroller.isFinished()) { // If scrolling, then stop now
mScroller.abortAnimation();
}
mLastMotionY = y; // Save start (or end) of motion
mScrollY = this.getScrollY(); // Save where we ended up
mText.setCursorVisible (true);
didMove = false;

break;

case MotionEvent.ACTION_MOVE: // The user finger is on the move
didMove = true;
final int deltaY = (int) (mLastMotionY - y); // Calculate distance moved since last report
mLastMotionY = y; // Save the start of this motion

if (deltaY < 0) { // If user is moving finger up screen
if (mScrollY > 0) { // and we are not at top of text
int m = mScrollY - mMinScroll; // Do not go beyond top of text
if (m < 0){
m = mScrollY;
}else m = mMinScroll;

scrollBy(0, -m); // Scroll the text up
}
} else
if (deltaY > 0) { // The user finger is moving up
int max = getLineCount() * getLineHeight () - sHeight; // Set max up value
if (mScrollY < max-mMinScroll){
scrollBy(0, mMinScroll); // Scroll up
}
}
postInvalidate();
break;

case MotionEvent.ACTION_UP: // User finger lifted up
final VelocityTracker velocityTracker = mVelocityTracker; // Find out how fast the finger was moving
velocityTracker.computeCurrentVelocity(mFlingV);
int velocityY = (int) velocityTracker.getYVelocity();

if (Math.abs(velocityY) > mFlingV){ // if the velocity exceeds threshold
int maxY = getLineCount() * getLineHeight () - sHeight; // calculate maximum Y movement
mScroller.fling(0, mScrollY, 0, -velocityY, 0, 0, 0, maxY); // Do the filng
}else{
if (mVelocityTracker != null) { // If the velocity less than threshold
mVelocityTracker.recycle(); // recycle the tracker
mVelocityTracker = null;
}
}
break;
}

mScrollY = this.getScrollY(); // Save where we ended up

return true ; // Tell caller we handled the move event
}



public void computeScroll() { // Called while flinging to execute a fling step
if (mScroller.computeScrollOffset()) {
mScrollY = mScroller.getCurrY(); // Get where we should scroll to
scrollTo(0, mScrollY); // and do it
postInvalidate(); // the redraw the sreem
}
}

关于android - Android中文本行的垂直滚动,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6422697/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com