After a long time I am back again with new stuffs. I have seen that JavaFX has got so many demand nowadays. Lots of people are requesting for something new something wow effect. In the same way one of my colleagues told me what if we have listview got some effects on scrolling the list. I got some dig around JavaFX Animation API and did some animation with ListCell but I thought it would be great if I share my works to you guys.

First We got to revamp what is the Listcell. ListCell are designed for making user to display text content in list format. But we can override these and make our own like displaying images,shapes and other controls as well.

ListCell inherits the character of Labeled so in default ListCell only displays the text content.If you want some control in your listcell other than label then there are some bunch of cellfactory in javafx.scene.control.cell package.

Every ListCell are being rendered according to the cellFactory implementation so you are free to implement your own cellFactory to make the listcell even more customizable. I had posted about TableCell customization which utilizes use of cellFactory (TableView Cell Modify)

Lets roll with the ListCell customization.

——————————————————————————————————————————————————————

public class AnimatedListCell<T> extends AbstractAnimatedListCell<T> {

    //... other codes ...
/**
* Get cellfactory of AbstractAnimatedListCell for ListView
*
* @param type
* @return
*/
public static Callback<ListView<String>, ListCell<String>> forListView(final AnimationType... type) {
return new Callback<ListView<String>, ListCell<String>>() {
@Override
public ListCell<String> call(
ListView<String> p) {
return new AnimatedListCell<>(new DefaultStringConverter(), type);
}
};
} /**
* Get cellfactory of AbstractAnimatedListCell for ListView with StringConverter
*
* @param <T>
* @param sc
* @param type
* @return
*/
public static <T extends Object> Callback<ListView<T>, ListCell<T>> forListView(
final StringConverter<T> sc, final AnimationType... type) {
return new Callback<ListView<T>, ListCell<T>>() {
@Override
public ListCell<T> call(
ListView<T> p) {
return new AnimatedListCell<>(sc, type);
}
}; } /**
* For getting the KeyFrames of specific AnimationType
*
* @param types
* @return
*/
@Override
protected KeyFrame[] getKeyFrames(AnimationType[] types) {
if (types == null) {
return null;
}
KeyFrame[] frames = null;
for (AnimationType type : types) {
switch (type) {
case FADE_OUT:
frames = anim.getFadeOut(frames);
break;
case FLAP_RIGHT:
frames = anim.getFlapRight(frames);
break;
case FLATTERN_OUT:
frames = anim.getFlatternOut(frames);
break;
case FLY_FROM_DOWN:
frames = anim.getFlyFromDown(frames);
break;
case FLY_FROM_UP:
frames = anim.getFlyFromUp(frames);
break;
case ROTATE_RIGHT:
frames = anim.getRotateYRight(frames);
break;
case SPEED_LEFT:
frames = anim.getSpeedLeft(frames);
break;
case SPEED_RIGHT:
frames = anim.getSpeedRight(frames);
break;
case TRANSITION_DOWN:
frames = anim.getTransitionDown(frames);
break;
case TRANSITION_LEFT:
frames = anim.getTransitionLeft(frames);
break;
case TRANSITION_RIGHT:
frames = anim.getTransitionRight(frames);
break;
case TRANSITION_TOP:
frames = anim.getTransitionTop(frames);
break;
case ZOOM_IN:
frames = anim.getZoomIn(0, frames);
break;
case POP_OUT:
frames = anim.getPopOut(frames);
break; }
}
return frames; } @Override
protected void updateItem(T t, boolean bln) {
//overriding the super interface
super.updateItem(t, bln); }
}

Above Class is subclass of AbstractAnimatedListCell so you can implement this in your cellFactory. Currently AbstractAnimatedListCell is subclass of ListCell which helps to make the animation possible. Now Lets directly move to the animation implementation.

/**
*
* @author Narayan G. Maharjan <me@ngopal.com.np>
*/
public class ListViewAnimation extends Application {
ObservableList list = FXCollections.observableArrayList(); ListView<String> listView; ComboBox<AnimationType> box; HBox hbox; AnchorPane root; Button btn; /**
* For initializing Containers
*/
public void initContainers() {
root = new AnchorPane();
hbox = new HBox(10); AnchorPane.setBottomAnchor(listView, 50d);
AnchorPane.setTopAnchor(listView, 10d);
AnchorPane.setLeftAnchor(listView, 10d);
AnchorPane.setRightAnchor(listView, 10d);
AnchorPane.setBottomAnchor(hbox, 10d);
AnchorPane.setLeftAnchor(hbox, 10d);
} /**
* For initializing controls
*/
public void initControls() {
listView = new ListView<>();
listView.setCellFactory(AnimatedListCell.forListView(AnimationType.ROTATE_RIGHT, AnimationType.FADE_OUT)); box = new ComboBox<>();
box.valueProperty().addListener(new ChangeListener<AnimationType>() {
@Override
public void changed(
ObservableValue<? extends AnimationType> ov, AnimationType t, AnimationType t1) {
if (!t1.equals(t)) {
listView.setCellFactory(AnimatedListCell.forListView(t1));
}
}
}); btn = new Button("Add New");
btn.setOnAction(new EventHandler<ActionEvent>() {
@Override
public void handle(ActionEvent event) {
list.add("Added New");
}
}); } @Override
public void start(Stage stage) throws Exception {
//Loading custom fonts
Font.loadFont(getClass().getResource("fonts/segoesc.ttf").toExternalForm(), 12); //adding values to list
for (int i = 0; i < 10; i++) {
list.add("" + i);
} //Initializing Controls
initControls();
initContainers(); //Adding Values
listView.setItems(list);
box.getItems().addAll(AnimationType.values()); //Adding controls to container
hbox.getChildren().addAll(new Label("Animation Type:"), box, btn);
root.getChildren().addAll(listView, hbox); Scene scene = new Scene(root);
scene.getStylesheets().add(getClass().getResource("css/style.css").toExternalForm());
scene.setCamera(new PerspectiveCamera());
stage.setTitle("List Animation!");
stage.setScene(scene);
stage.show(); } public static void main(String[] args) {
launch(args);
}
}

Well after implementing those Animation you can get animation instantly on list cell update . However I have added some few styling to make it even more better.

If you want to try out yourself . Grab the source code from here:

ListCell Animation in ListView的更多相关文章

  1. Animation显示ListView的每一条记录

    activity_main.xml <?xml version="1.0" encoding="utf-8"?> <LinearLayout ...

  2. Android5.0 ListView特效的简单实现

    Android5.0中对于动画可所谓是情有独钟,在设计规范中大量展现了listview的动画,其实也就是一个目的:将items动画显示出来.这个看起来很炫的效果,其实实现也蛮简单的,我下面就来用动画简 ...

  3. android3D动画,绕y轴旋转

    原文地址:http://blog.csdn.net/x_i_a_o_h_a_i/article/details/40449847 其实网上的3D旋转的例子很多,在这里我只是想把其代码做一个解释. 先上 ...

  4. Android中轴旋转特效实现,制作别样的图片浏览器

    转载请注明出处:http://blog.csdn.net/guolin_blog/article/details/10766017 Android API Demos中有很多非常Nice的例子,这些例 ...

  5. javaFX笔记----ComboBox模仿qq账号下拉框删除账号

    myComboBox.setCellFactory( new Callback<ListView<String>, ListCell<String>>() { @O ...

  6. javafx ComboBox Event and change cell color

    public class EffectTest extends Application { public static void main(String[] args) { launch(args); ...

  7. ListView Animation

    简单介绍一下4种动画效果方式AnimationSet set = new AnimationSet(false); Animation animation = new AlphaAnimation(0 ...

  8. ListView的淡入淡出和Activity的淡入淡出补间动画效果Animation

    //=========主页面======================= package com.bw.lianxi7; import android.os.Bundle;import androi ...

  9. Android Animation动画实战(一): 从布局动画引入ListView滑动时,每一Item项的显示动画

    前言: 之前,我已经写了两篇博文,给大家介绍了Android的基础动画是如何实现的,如果还不清楚的,可以点击查看:Android Animation动画详解(一): 补间动画 及 Android An ...

随机推荐

  1. hdu 4810 Wall Painting (组合数学+二进制)

    题目链接 下午比赛的时候没有想出来,其实就是int型的数分为30个位,然后按照位来排列枚举. 题意:求n个数里面,取i个数异或的所有组合的和,i取1~n 分析: 将n个数拆成30位2进制,由于每个二进 ...

  2. Linux查看CPU信息

    1.  查看物理CPU的个数 #cat /proc/cpuinfo |grep "physical id"|sort |uniq|wc –l 2.   查看逻辑CPU的个数 #ca ...

  3. js设置与获取Cookie

    /*设置与获取Cookie*/ var Cookie ={} Cookie.write = function(key, value, duration){ var d = new Date(); d. ...

  4. MVC+Ef项目(1) 项目的框架搭建

    一:首先我们来搭建最基本的项目框架,这里使用MVC3作为web项目,然后我们添加几个类库项目 最后的项目如下, 其中有一个 YouJiao.MvcWeb.Repository 实际就当做是 DAL层即 ...

  5. Localizing Astah – Chinese version(simplified) is now available!

    Thanks to Abbey, now GUI in Astah Community can be shown in Chinese. As Abbey created Chinese one, a ...

  6. JNDI绑定数据库

    经过3个多小时的努力,配置JNDI数据源(主要是通过DBCP连接池)终于搞定- 还是Tomcat官方的说明好,不过全是英文的,大概还看得懂. 百度上那么花花绿绿的太多了,一个也没成功!... 本例使用 ...

  7. 聊聊Dataguard的三种保护模式实验(上)

    Data Guard是Oracle高可用性HA的重要解决方案.针对不同的系统保护需求,DG提供了三种不同类型的保护模式(Protection Mode),分别为:最大保护(Maximum Protec ...

  8. Shell教程3-Shell特殊变量

    前面已经讲到,变量名只能包含数字.字母和下划线,因为某些包含其他字符的变量有特殊含义,这样的变量被称为特殊变量. 例如,$ 表示当前Shell进程的ID,即pid,看下面的代码:   $echo $$ ...

  9. python 传入参数返回的时候好像有些时候会出现莫名其妙的循环

    def handle_field(name, s_len, s): #some code #return s would error but return not.... #return s for ...

  10. myeclipse 10 优化

    一.Myeclipse10修改字体 MyEclipse10是基于Eclipse3.7内核,但在Eclipse的Preferences-〉general-〉 Appearance->Colors ...