Android-按钮滑动效果

时间:2013-07-26 08:08:47

标签: java android android-layout android-fragments

我想对屏幕一侧的箭头产生滑动效果。当我点击箭头时,将显示一个带滑动效果的按钮。我不知道我怎么做到这一点。

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

enter image description here

3 个答案:

答案 0 :(得分:6)

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

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);

答案 1 :(得分: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);

答案 2 :(得分: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; 
    }