2

我想对屏幕侧面的箭头有滑动效果。当我单击箭头时,将显示一个带有滑动效果的按钮。我不确定我该怎么做。

我想我可以在这个上使用嵌入式片段,但我不知道如何实现幻灯片效果。

在此处输入图像描述

4

3 回答 3

7

这是实现此目的的代码。我也写了评论。

TranslateAnimation anim = new TranslateAnimation(0, 150, 0, 0); //first 0 is start point, 150 is end point horizontal
anim.setDuration(1000); // 1000 ms = 1second
yourarrow.startAnimation(anim); // your imageview that you want to give the animation. call this when you want it to take effect

如果您希望它在动画之后保持这种状态,请输入:

anim.setFillAfter(true);
于 2013-07-26T08:19:58.073 回答
2

就像我在我的一个项目中所做的那样:

a) 创建动画

public Animation getEditModeAnimation() {
    TranslateAnimation animation = new TranslateAnimation(0,
            convertDpToPixel(57, this.context), 0, 0);
    animation.setDuration(300);
    animation.setFillAfter(true);

    return animation;
}

public Animation getNonEditModeAnimation() {
    TranslateAnimation animation = new TranslateAnimation(
            convertDpToPixel(57, this.context), 0, 0, 0);
    animation.setDuration(300);
    animation.setFillAfter(true);

    return animation;
}

b)与您的UI组件结合:

Animation editAnimation = getEditModeAnimation();
yourUIComponent.setAnimation(editAnimation);

Animation nonEditAnimation = getNonEditModeAnimation();
yourUIComponent.setAnimation(nonEditAnimation);

c) 辅助方法(如果需要)://simply converts dp to px - pretty convenient

public static float convertDpToPixel(float dp, Context context) {
    Resources resources = context.getResources();
    DisplayMetrics metrics = resources.getDisplayMetrics();
    float px = dp * (metrics.densityDpi / 160f);
    return px;
}

d) 加上一些表示组件状态的标志,因此您将知道何时使用一种或另一种模式

public enum MODES {
    EDIT_MODE(1), NON_EDIT_MODE(0);

    private int mode_identifier;

    private MODES(int mode_identifier) {
        this.mode_identifier = mode_identifier;
    }
}

e) 和一个保存当前状态的全局变量

private MODES yourUIComponentState;

f) 动态创建一些 UI 组件:

Button yourUIComponent = new Button(yourContextReferenceHere);
TextView yourUIComponent = new TextView(yourContextReferenceHere);
RelativeLayout(yourUIComponent) = new RelativeLayout(yourContextReferenceHere);
....

g) 在 XML 中创建一些 UI 组件:

 <Button android:id="@+id/yourUIComponent"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
 />

 <TextView android:id="@+id/yourUIComponent"
           android:layout_width="wrap_content"
           android:layout_height="wrap_content"
 />

h) 要引用在 Activity 中创建的 UI 组件:

Button yourUIComponent = (Button) findViewById(R.id.yourUIComponent);
TextView yourUIComponent = (TextView) findViewById(R.id.yourUIComponent);
于 2013-07-26T08:16:32.777 回答
2
Animation anim = E.getAnimation(anim_duration, 0, your_layout.getWidth(), 0, 0);
anim.setAnimationListener(new AnimationListener() {     
   @Override
   public void onAnimationStart(Animation animation) {}                            
   @Override
   public void onAnimationRepeat(Animation animation) {}   
   @Override
   public void onAnimationEnd(Animation animation) {}
});
your_layout.startAnimation(anim);

your_layout.getWidth()当您想将布局向右滑动时,这将是积极的。如果您需要额外的功能,请覆盖这些元素。

希望这能让你知道如何去做。

编辑:“E”类使用这种方法:

    public static Animation getAnimation(long duration, float fromX, float toX, float fromY, float toY){ 
        TranslateAnimation tAnimation = new TranslateAnimation(fromX,toX,fromY,toY);
        tAnimation.setDuration(duration); 
        tAnimation.setFillEnabled(true); 
        return tAnimation; 
    }
于 2013-07-26T08:21:50.577 回答