ITeye博客

JavaFX学习之Animation

lujin55 · 2013-01-15 16:00 · javafx · 编程语言 · Markdown

Animation 抽象类,提供动画功能。 方法 setAutoReverse(true); //设置是否可以反转,反转算一次循环 setRate(1) //设置完成时间的比率,1是在Duration时间内完成,2是比duration快一倍时间完成。 setDelay(Duration.seconds(3)) //设置延迟时间执行 setCycleDuration(Duration value) //循环的间隔 setOnFinished(EventHandler value) //完成后处理事情 属性: autoReverse currentRate currentTime cycleCount cycleDuration delay onFinished rate status totalDuration

AnimationTimer


public class Animator extends AnimationTimer{
	
	Duration delayTime;
	private long last;
	
	public Animator(Duration delayTime){
		this.delayTime = delayTime;
	}

	@Override
	public void handle(long now) {
		if((now - last)/1000000>delayTime.toMillis()){
			last = now;
			pulse();
		}
	}
	
	public void pulse(){
                  //logic
		System.out.println("pulse");
	}

}

可以自己在handle中控制,什么时候调用你自己的逻辑。

Transition 抽象类,继承Animation,拥有一些实现子类,实现了特定的动画。 方法: setInterpolator(Interpolator value) getParentTargetNode()

FadeTransition 淡入淡出,其实就是修改opacity透明度属性


 	     Rectangle rect = new Rectangle (100, 40, 100, 100);
	     rect.setArcHeight(50);
	     rect.setArcWidth(50);
	     rect.setFill(Color.VIOLET);
	     FadeTransition ft = new FadeTransition(Duration.millis(3000), rect);
	     ft.setFromValue(1.0);
	     ft.setToValue(0.3);
	     ft.setCycleCount(4);
	     ft.setAutoReverse(true);
	     ft.play();
	     root.getChildren().add(rect);
  

opacity的值是0到1,所以设置fromvalue和tovalue在这之间。 FillTransition 颜色变化,针对shape,修改的fill属性。


public void buildFillTransition(Group root){
		Rectangle rec = new Rectangle(50,50,100,100);
		FillTransition ft = new FillTransition();
		ft.setDuration(Duration.seconds(5));
		ft.setFromValue(Color.BLACK);
		ft.setToValue(Color.WHITE);
		ft.setShape(rec);
		ft.play();
		root.getChildren().add(rec);
}

PathTransition按path移动,主要是调节translateX,Y


 	     PathTransition pathTransition = new  PathTransition();
	     pathTransition.setDuration(Duration.millis(10000));
	     pathTransition.setNode(rect);
	     pathTransition.setPath(path);
	     pathTransition.setOrientation(OrientationType.ORTHOGONAL_TO_TANGENT);
	     pathTransition.setCycleCount(4);
	     pathTransition.setAutoReverse(true);
	 
	     pathTransition.play();
	

属性: duration node orientation path

PauseTransition停止,就相当sleep了一下


 	     Rectangle rect = new Rectangle (100, 40, 100, 100);
	     rect.setArcHeight(50);
	     rect.setArcWidth(50);
	     rect.setFill(Color.VIOLET);
	 
	     RotateTransition rt = new RotateTransition(Duration.millis(3000), rect);
	     rt.setByAngle(180);
	     rt.setCycleCount(4);
	     rt.setAutoReverse(true);
	     SequentialTransition seqTransition = new SequentialTransition (
	         new PauseTransition(Duration.millis(1000)), // wait a second
	         rt
	     );
	     seqTransition.play();
	

方法: setDuration(Duration value) interpolate(double frac) RotateTransition旋转,修改rotate属性,实现旋转


 	     Rectangle rect = new Rectangle (100, 40, 100, 100);
	     rect.setArcHeight(50);
	     rect.setArcWidth(50);
	     rect.setFill(Color.VIOLET);
	     RotateTransition rt = new RotateTransition(Duration.millis(3000), rect);
	     rt.setByAngle(180);
	     rt.setCycleCount(4);
	     rt.setAutoReverse(true);
	     rt.play();
	     root.getChildren().add(rect);
 

属性: fromAngle toAngle byAngle duration axis node

ScaleTransition大小,修改scale属性,实现大小变化


 		Rectangle rect = new Rectangle (100, 40, 100, 100);
	     rect.setArcHeight(50);
	     rect.setArcWidth(50);
	     rect.setFill(Color.VIOLET);
	     ScaleTransition st = new ScaleTransition(Duration.millis(2000), rect);
	     st.setByX(1.5f);
	     st.setByY(1.5f);
	     st.setCycleCount(4);
	     st.setAutoReverse(true);
	     st.play();	 	

StrokeTransition画笔的颜色变动,针对shape,修改stroke属性。



		Rectangle rec = new Rectangle(50,50,100,100);
		StrokeTransition st = new StrokeTransition();
		st.setDuration(Duration.seconds(5));
		st.setFromValue(Color.BLACK);
		st.setToValue(Color.RED);
		st.setShape(rec);
		st.play();
		root.getChildren().add(rec);
	

TranslateTransition位置移动(translateXYZ)修改translateX,Y,X


 		Rectangle rec = new Rectangle(50,50,100,100);
		TranslateTransition tt = new TranslateTransition();
		tt.setDuration(Duration.seconds(5));
		tt.setByX(400);
		tt.setNode(rec);
		tt.setAutoReverse(true);
		tt.setRate(2);
		//tt.setDelay(Duration.seconds(3));
		tt.setOnFinished(new EventHandler<ActionEvent>(){
			@Override
			public void handle(ActionEvent arg0) {
				System.out.println("finished");
			}
			
		});
		tt.play();
	

ParallelTransition同时运行多个animation
SequentialTransition逐个运行animation

Interpolator 插值器,一般动画的变化都是线性增长,如你想改变增长的方式,则通过该类定义。 Timeline 通过timeline你可以自己实现一些动画。 KeyFrame timeline最关键的是KeyFrame,代表一帧,可以看下它的构造方法 KeyFrame(Duration time, EventHandler onFinished, KeyValue... values) //这一帧所执行的时间,执行完后做什么事情,还有一堆KeyValue在这一帧内的变化。 KeyValue 定义一个值在帧内的变化 KeyValue(WritableValue target, T endValue) //目标值,结束值。 目标值在这一帧内往结束值变化。

评论(0)

暂无评论。

评论功能维护中,当前仅展示历史评论。