Android图库 - 使用淡入等不同的过渡

时间:2011-11-24 09:08:11

标签: android gallery

我正在使用Gallery UI控制器来提供全屏幻灯片。 它设置为仅在照片上显示。

之间的标准转换是滑出旧照片并滑入新照片。 这很顺利,在大多数平台上运行良好,但我在其他平台上有一些性能问题(分辨率非常高)。

我的解决方法是使用不同的转换,最好是淡出和淡入。

我已经扩展了Gallery控件并覆盖了 onKeyDown方法。

通过使用setSelection,我可以跳过默认转换,但是我无法将转换包含在内。

是否有人添加了与图库组件的不同过渡?

是否有其他组件可能会给我更好的结果?

图库来源:http://grepcode.com/file/repository.grepcode.com/java/ext/com.google.android/android/2.1_r2/android/widget/Gallery.java

1 个答案:

答案 0 :(得分:1)

想想,我已经成功破解了这个坚果。 策略如下:

  1. 图库中的所有项目都必须以ViewAnimator为根。
  2. 扩展标准图库,并覆盖onKeyDown
  3. 确保在一个时间作业后使用Gallery中的onKeyDown,例如gallery.onKeyDown(KeyEvent.KEYCODE_DPAD_RIGHT,new KeyEvent(0,0));
  4. 在扩展图库中捕获密钥时,获取当前View作为ViewAnimator的实例,从Gallery的适配器获取下一个视图并为下一个视图创建动画
  5. 动画完成后,调用setSelection(getSelectedItemPosition()+ 1); (需要检查包装)
  6. 这是一个有效的概念证明,虽然缺少一些检查。

    <ViewAnimator 
        xmlns:android="http://schemas.android.com/apk/res/android"
        android:id="@+id/slideshow_animator"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent">
    
    
    
    package com.elsewhat.slideshow.api;
    
    
    
    public class CustomGallery extends Gallery implements AnimationListener {
    boolean mDoTransition=false;
    
    public CustomGallery(Context context) {
        super(context);
    }
    
    public CustomGallery(Context context, AttributeSet attrs) {
        super(context, attrs);
    }
    
    public CustomGallery(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
    }
    
    /*
     * (non-Javadoc)
     * 
     * @see android.widget.Gallery#onKeyDown(int, android.view.KeyEvent)
     */
    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if(mDoTransition==true){
            //leave the transition to the super class
            return super.onKeyDown(keyCode, event);
        }else {
            //handle the switch without transition ourselves
            switch (keyCode) {
            case KeyEvent.KEYCODE_DPAD_LEFT:
                //to be implemented similar as below
                return true;
    
            case KeyEvent.KEYCODE_DPAD_RIGHT:
                if (getCount() > 0 && getSelectedItemPosition() < getCount() - 1) {
                    View currentView = getSelectedView();
    
                    View unboundNewView= getAdapter().getView(getSelectedItemPosition()+1, null, null);
    
    
    
                    ViewAnimator viewAnimator = (ViewAnimator)currentView;
                    Animation inAnimation = new AlphaAnimation(0.0f, 1.0f);
                    inAnimation.setDuration(1000);
                    inAnimation.setAnimationListener(this);
                    viewAnimator.setInAnimation(inAnimation);
    
                    Animation outAnimation = new AlphaAnimation(1.0f, 0.0f);
                    outAnimation.setDuration(1000);
                    viewAnimator.setOutAnimation(outAnimation);
    
                    viewAnimator.addView(unboundNewView);
                    viewAnimator.showNext();
    
    
                }
                return true;
    
            case KeyEvent.KEYCODE_DPAD_CENTER:
            case KeyEvent.KEYCODE_ENTER:
            }
    
            return super.onKeyDown(keyCode, event);
        }
    
    }
    
    public void setDoTransition(boolean doTransition){
        mDoTransition= doTransition;
    }
    
    public boolean getDoTransition(){
        return mDoTransition;
    }
    
    @Override
    public void onAnimationEnd(Animation arg0) {
        setSelection(getSelectedItemPosition()+1);
    
    }
    
    @Override
    public void onAnimationRepeat(Animation animation) {
        // TODO Auto-generated method stub
    
    }
    
    @Override
    public void onAnimationStart(Animation animation) {
        // TODO Auto-generated method stub
    
    }
    
    }
    
相关问题