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 1072 (不一样的入队条件) Nightmare

    之前的BFS都是需要一个标记数组,但这个题不一样,因为可能一个格子不止走一次. 那么我们就要寻找新的入队条件:left比上次经过的时候大才入队(left表示上次经过该点时剩余的时间). 为什么呢?我们 ...

  2. 51nod1476 括号序列的最小代价

    这题应该可以用费用流写吧?不过我想不出贪心来TAT.其实还是单调队列乱搞啊T_T //ÍøÉϵÄ̰ÐÄËã·¨ºÃÉñ°¡¡£¡£¡£ÎÒÖ»»áÓÃ×îС·ÑÓÃ×î´óÁ÷ÅÜTAT #in ...

  3. OK335xS ethtool 移植

    /******************************************************************* * OK335xS ethtool 移植 * 声明: * 由于 ...

  4. LeetCode Linked List Cycle II 单链表环2 (找循环起点)

    题意:给一个单链表,若其有环,返回环的开始处指针,若无环返回NULL. 思路: (1)依然用两个指针的追赶来判断是否有环.在确定有环了之后,指针1跑的路程是指针2的一半,而且他们曾经跑过一段重叠的路( ...

  5. 并行编译 Xoreax IncrediBuild

    好东西... http://pan.baidu.com/s/1BtZ4s

  6. 使用Nodejs+mongodb开发地图瓦片服务器

    原先地图瓦片服务器采用的是arcgisserver发布的地图服务并进行切片,但ags发布的地图服务很占内存,发布太多的话服务器压力很大.再一个就是ags价太高了. 学习Nodejs之后,发现这是一个可 ...

  7. makefile实例(2)-多个文件实例

    1,源文件依赖关系 defs.h command.h buffer.h main.cpp * util.cpp * kde.cpp * * command.cpp * * display.cpp * ...

  8. c/c++ 编译器内存对齐问题

    C语言结构体对齐问题详解 转载自:http://blog.csdn.net/tiany524/article/details/6295551 测试环境32位机 WinXP: 编译器VC6(MS cl. ...

  9. HDU3333 Turing Tree 离线树状数组

    题意:统计一段区间内不同的数的和 分析:排序查询区间,离线树状数组 #include <cstdio> #include <cmath> #include <cstrin ...

  10. Drupal如何更新注册表?

    Drupal的注册表是指registry和registry_file两个数据表.前一个表保存所有可用的类和接口以及它们所对应的文件,后一个表保存每个文件的hash码. 1. 将所有需要更新的文件都汇总 ...