如何正确创建动画(在listview中)?
我有这段代码:
public void blink(final View v)
{
final Animation animation = new AlphaAnimation(1, 0);
animation.setDuration(500);
animation.setInterpolator(new LinearInterpolator());
animation.setRepeatCount(Animation.INFINITE);
animation.setRepeatMode(Animation.REVERSE);
v.startAnimation(animation);
new Thread(new Runnable()
{
@Override
public void run()
{
try { Thread.sleep(2000); }
catch (InterruptedException e) { e.printStackTrace(); }
v.clearAnimation();
}
}).start();
}
View在“getView”函数(ArrayListAdapter)中动画:
if(!alarm.isAnimated())
{
blink(view);
alarm.setAnimated(true);
}
这非常好用,但我需要经常更新视图,每次更新都会取消动画...
notifyDataSetChanged(); // calls getView, isAnimated = true, animation is canceled
而且我也不想重置动画(它可能会在中间结束而且看起来不太好)而且我也想在我将项目添加到列表视图时(而不是在更新时)进行动画处理。
感谢您的帮助。
编辑:
整个适配器:
public class AlertListAdapter extends ArrayAdapter<AlarmEntity>
{
private ArrayList<AlarmEntity> data;
public AlertListAdapter(Context context, ArrayList<AlarmEntity> data)
{
super(context, android.R.layout.simple_list_item_2, data);
this.data = (data);
}
@NonNull
@Override
public View getView(int position, View convertView, @NonNull ViewGroup parent)
{
LayoutInflater inflater = LayoutInflater.from(getContext());
@SuppressLint("ViewHolder") View view = inflater.inflate(R.layout.alert_row, parent, false);
return getView(getItem(position), view);
}
private View getView(AlarmEntity alarm, View view)
{
if(!alarm.isAnimated())
{
blink(view);
alarm.setAnimated(true);
}
return (view);
}
public void update()
{
for(AlarmEntity e : data)
e.setDistance(Coords.getDistance(CarEntity.location, e.getLocation()));
notifyDataSetChanged();
}
public void blink(final View v)
{
final Animation animation = new AlphaAnimation(1, 0);
animation.setDuration(500);
animation.setInterpolator(new LinearInterpolator());
animation.setRepeatCount(Animation.INFINITE);
animation.setRepeatMode(Animation.REVERSE);
v.startAnimation(animation);
new Thread(new Runnable()
{
@Override
public void run()
{
try { Thread.sleep(2000); }
catch (InterruptedException e) { e.printStackTrace(); }
v.clearAnimation();
}
}).start();
}
}
答案 0 :(得分:0)
最简单的方法是不使用长时间动画将单元格添加到ListView中。 :-D
public void pushIn(final View v)
{
Animation animation = AnimationUtils.loadAnimation(MainActivity.getContext(), R.anim.push_left_anim);
animation.setDuration(500);
v.startAnimation(animation);
}
它看起来比眨眼更好:-D
<set xmlns:android="http://schemas.android.com/apk/res/android">
<translate android:fromXDelta="100%p" android:toXDelta="0"
android:duration="300"/>
<alpha android:fromAlpha="0.0" android:toAlpha="1.0"
android:duration="300" />
</set>