在Android Gallery控件上收听动画结束
我有一个标准的Android Gallery控件:在Android Gallery控件上收听动画结束,android,android-layout,android-animation,Android,Android Layout,Android Animation,我有一个标准的Android Gallery控件: <Gallery android:id="@+id/galArt" android:spacing="10dp" android:fadingEdgeLength="0dp" android:unselectedAlpha="1" android:layout_width="fill_parent" android:layout_height="fill_parent" /> 从
<Gallery
android:id="@+id/galArt"
android:spacing="10dp"
android:fadingEdgeLength="0dp"
android:unselectedAlpha="1"
android:layout_width="fill_parent"
android:layout_height="fill_parent" />
从中,我用以下代码收听事件:
galArt.setOnItemSelectedListener(new OnItemSelectedListener() {
public void onItemSelected(AdapterView<?> parent, View view, int pos, long id) {
showMessageToast("Selected: " + pos);
}
public void onNothingSelected(AdapterView<?> arg0) {}
});
galArt.setOnItemSelectedListener(新的OnItemSelectedListener(){
已选择公共位置(AdapterView父项、视图、整数位置、长id){
showMessageToast(“选定:“+pos”);
}
未选择的公共无效(AdapterView arg0){}
});
我猜它的作用是这样的:当我滑动图像时,我会得到一个祝酒词,告诉我现在选择了哪个图像
但是,当动画仍在运行时,此土司会在图像停止滑动之前显示。我想在它停止滑动后采取行动,以避免中断动画
动画制作完成后,我可以听什么来获得通知?好的,我找到了一个解决方案。请注意,这只是一个解决方案,不一定是最佳解决方案 我的解决方案是忽略所有逻辑事件(比如onScroll或onAnimationEnd,因为我无法让它们中的任何一个正常工作),并侦听子视图位置中的更改。当子视图处于静止状态时,动画结束 这样做的一个实际好处是,这对拖动和投掷都有效 问题是,onItemSelected函数将从UI线程以外的其他线程调用。通过使用活动的runOnUIThread函数解决此问题,如示例所示 监听更改的方法(请注意,这不是常规的onimerselected函数,而是我自己的onimerreallyselected):
海尔伦德,我试过你的解决方案,效果很好。唯一的问题是,当你启动Gallery时,它不会在第一个项目上启动。你有解决这个问题的方法吗?没有,我从来不需要在画廊初始化时启动它。我建议您在第一次设置函数()后手动调用它。或者,您可以在ArtGallery类中重写setAdapter或类似的内容,然后从那里调用fireOnSelected。
galArt.setOnItemReallySelectedListener(new OnItemSelectedListener() {
public void onItemSelected(AdapterView<?> parent, View view, int pos, long id) {
_activity.runOnUiThread(new Runnable() {
public void run() {
//Do your stuff here ...
}
});
}
public void onNothingSelected(AdapterView<?> arg0) {
//... or here.
}
});
import java.util.Timer;
import java.util.TimerTask;
import android.content.Context;
import android.util.AttributeSet;
import android.util.Log;
import android.view.MotionEvent;
import android.widget.Gallery;
public class ArtGallery extends Gallery {
OnItemSelectedListener _listener;
Timer _timer = new Timer();
public ArtGallery(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
}
public ArtGallery(Context context, AttributeSet attrs) {
super(context, attrs);
}
public ArtGallery(Context context) {
super(context);
}
@Override
public boolean onTouchEvent(MotionEvent event) {
if(event.getAction()==MotionEvent.ACTION_UP){
setTimer();
}
return super.onTouchEvent(event);
}
private int _lastScrollX = Integer.MIN_VALUE;
private void setTimer() {
//Cancel existing tasks (if any), and create a new timer.
_timer.cancel();
_timer = new Timer();
//Schedule our animation check.
_timer.schedule(new TimerTask() {
@Override
public void run() {
//Just some value that will change while the animation is running.
int x = getSelectedView().getLeft();
if(_lastScrollX != x){
//Value has changed; save current value, and reset the timer.
_lastScrollX = x;
setTimer();
}else{
//The value hasn't changed during the last 50ms. That probably means that the animation has stopped.
fireOnSelected();
}
}
}, 50);
}
public void setOnItemReallySelectedListener(OnItemSelectedListener listener){
_listener = listener;
}
//This function is copied from the Android Gallery source code, and works exactly like the original one.
private void fireOnSelected() {
if (_listener == null)
return;
int selection = this.getSelectedItemPosition();
if (selection >= 0) {
_listener.onItemSelected(this, getSelectedView(), selection, getAdapter().getItemId(selection));
} else {
_listener.onNothingSelected(this);
}
}
}