Codenameone 滚动容器的橡皮筋效果
我希望在滚动容器上有一个rubberband效果,我觉得组件基类中内置的“拉伸滚动”不足以替代它Codenameone 滚动容器的橡皮筋效果,codenameone,Codenameone,我希望在滚动容器上有一个rubberband效果,我觉得组件基类中内置的“拉伸滚动”不足以替代它 是否有一种合理可行的方法,如禁用默认的overscroll行为,以便以本例中所示的方式控制属性滚动-因为我的问题没有答案,所以我自己回答。请注意,我不是专家,因此这可能不符合您的目的 通过扩展容器类并重写指针方法而不调用其超级方法,可以重写默认的overscroll行为 以下是一个例子: import com.codename1.ui.Component; import com.codename1.
是否有一种合理可行的方法,如禁用默认的overscroll行为,以便以本例中所示的方式控制属性滚动-因为我的问题没有答案,所以我自己回答。请注意,我不是专家,因此这可能不符合您的目的 通过扩展
容器
类并重写指针方法而不调用其超级方法,可以重写默认的overscroll行为
以下是一个例子:
import com.codename1.ui.Component;
import com.codename1.ui.Container;
import com.codename1.ui.Form;
import com.codename1.ui.Graphics;
import com.codename1.ui.Label;
import com.codename1.ui.animations.Motion;
import com.codename1.ui.geom.Point;
import com.codename1.ui.layouts.BorderLayout;
import com.codename1.ui.layouts.BoxLayout;
public class FormOverscroll extends Form {
FormOverscroll() {
super("FormOverscroll");
setScrollable(false);
setLayout(new BorderLayout());
Container container = new Container(BoxLayout.y()) {
boolean bounce = false;
Motion motion = null;
Point pointPressed = null;
long millisPoint = 0;
int scrollYPressed = 0, scrollYPrevious = 0;
{
// setTensileDragEnabled(false);
setScrollableY(true);
}
@Override
protected boolean isStickyDrag() {
return true;
}
@Override
public void pointerPressed(int x, int y) {
pointPressed = new Point(x, y);
scrollYPressed = scrollYPrevious = getScrollY();
millisPoint = System.currentTimeMillis();
motion = null;
bounce = false;
}
@Override
public void pointerDragged(int x, int y) {
if (null == pointPressed) {
return;
}
int yDist = y - pointPressed.getY();
int scrollY = scrollYPressed - yDist;
if (scrollY < 0) {
Motion motionRubberband = Motion.createCubicBezierMotion(0, scrollY, getHeight(), 0f, 1.2f, 0.5f, 0.6f);
motionRubberband.setStartTime(0);
motionRubberband.setCurrentMotionTime(Math.abs(scrollY));
scrollY = motionRubberband.getValue();
}
setScrollY(scrollY);
}
float getVelocity(int scrollY) {
long millisNow = System.currentTimeMillis();
long timediff = millisNow - millisPoint;
float diff = scrollYPrevious - scrollY;
float velocity = (diff / timediff) * -1f;
scrollYPrevious = scrollY;
millisPoint = millisNow;
return velocity;
}
@Override
public void pointerReleased(int x, int y) {
if (null == pointPressed) {
return;
}
int yDist = y - pointPressed.getY();
int scrollY = scrollYPressed - yDist;
float velocity = getVelocity(scrollY);
motion = Motion.createFrictionMotion(scrollY, Integer.MIN_VALUE, velocity, 0.0007f);
motion.start();
getComponentForm().registerAnimated(this);
pointPressed = null;
}
@Override
public boolean animate() {
boolean animate = super.animate();
if (null != motion) {
int scrollY = motion.getValue();
setScrollY(scrollY);
int target = 0;
if (scrollY < target && !bounce) {
createRubberbandMotion(scrollY, target);
bounce = true;
motion.start();
}
int maxScrollY = Math.max(target, getScrollDimension().getHeight() - getHeight());
if (scrollY > maxScrollY && !bounce) {
createRubberbandMotion(scrollY, maxScrollY);
bounce = true;
motion.start();
}
scrollYPrevious = scrollY;
if (motion.isFinished()) {
motion = null;
}
return true;
}
return animate;
}
private void createRubberbandMotion(int source, int target) {
motion = Motion.createCubicBezierMotion(source, target, 500, 0.0f, 1.0f, 1.2f, 1.0f);
}
@Override
public Component getComponentAt(int x, int y) {
return this;
}
@Override
public void paint(Graphics aGraphics) {
super.paint(aGraphics);
}
};
for (int index = 0; index < 100; index++) {
container.add(new Label("Zeile " + index));
}
add(BorderLayout.CENTER, container);
}
}
导入com.codename1.ui.Component;
导入com.codename1.ui.Container;
导入com.codename1.ui.Form;
导入com.codename1.ui.Graphics;
导入com.codename1.ui.Label;
导入com.codename1.ui.animations.Motion;
导入com.codename1.ui.geom.Point;
导入com.codename1.ui.layouts.BorderLayout;
导入com.codename1.ui.layouts.BoxLayout;
公共类FormOverscroll扩展了表单{
FormOverscroll(){
超级(“FormOverscroll”);
设置可滚动(假);
setLayout(新的BorderLayout());
Container Container=新容器(BoxLayout.y()){
布尔反弹=假;
运动=零;
点按下=空;
长毫点=0;
int scrollYPressed=0,scrollYPrevious=0;
{
//setTensileDragEnabled(假);
setScrollableY(真);
}
@凌驾
受保护的布尔值isStickyDrag(){
返回true;
}
@凌驾
压缩的公共空指针(整数x,整数y){
点按下=新点(x,y);
scrollYPressed=scrollYPrevious=getScrollY();
millisPoint=System.currentTimeMillis();
运动=零;
反弹=假;
}
@凌驾
公共空指针参差不齐(整数x,整数y){
如果(null==点按下){
返回;
}
int yDist=y-pointPressed.getY();
int scrollY=scrollYPressed-yDist;
如果(滚动<0){
Motion motionRubberband=Motion.createCubicBezierMotion(0,滚动,getHeight(),0f,1.2f,0.5f,0.6f);
motionRubberband.setStartTime(0);
motionRubberband.setCurrentMotionTime(Math.abs(scrollY));
scrollY=motionRubberband.getValue();
}
设置滚动(滚动);
}
浮点getVelocity(int滚动){
long millisNow=System.currentTimeMillis();
长时间差=毫雪-毫点;
float diff=scrollYPrevious-scrollY;
浮动速度=(差值/时间差值)*-1f;
scrollYPrevious=滚动的;
毫点=毫雪;
返回速度;
}
@凌驾
公共无效点删除(整数x,整数y){
如果(null==点按下){
返回;
}
int yDist=y-pointPressed.getY();
int scrollY=scrollYPressed-yDist;
浮动速度=getVelocity(滚动);
motion=motion.createFrictionMotion(滚动,Integer.MIN_值,速度,0.0007f);
运动。开始();
getComponentForm();
pointPressed=null;
}
@凌驾
公共布尔动画(){
布尔animate=super.animate();
如果(空!=运动){
int scrollY=motion.getValue();
设置滚动(滚动);
int目标=0;
如果(滚动最大滚动&&!反弹){
createRubberbandMotion(滚动、最大滚动);
反弹=真;
运动。开始();
}
scrollYPrevious=滚动的;
if(motion.isFinished()){
运动=零;
}
返回true;
}
返回动画;
}
私有void createRubberbandMotion(int源,int目标){
运动=运动。CreateCCubicBezierMotion(源、目标、500、0.0f、1.0f、1.2f、1.0f);
}
@凌驾
公共组件getComponentAt(int x,int y){
归还这个;
}
@凌驾
公共空白油漆(图形失写){
超级油漆(失写);
}
};
对于(int-index=0;index<100;index++){
添加(新标签(“Zeile”+索引));
}
添加(BorderLayout.CENTER、container);
}
}
Codename One的“拉伸拖动”有哪些问题?“拉伸拖动”和“橡皮筋滚动”看起来是一样的。。。但是,可以使用主题常量tensileDragBool
禁用拉伸拖动。要设置Y滚动位置,这里有一个相关的讨论:不,代号为One的拉伸滚动根本没有橡皮筋效果。看看一个本地应用程序,或者看看这个-拉伸是我们对橡皮筋效应的术语,好吧,它和真正的ThingFlatter做得不一样。如果有的话