如何翻转安卓按钮

发布于 2024-12-09 07:56:55 字数 120 浏览 1 评论 0原文

我想在android扩展按钮中制作一个自定义按钮。当我点击按钮时,它会翻转 180 度并显示按钮的背面图像。这意味着该按钮将有两个图像。当点击它时,它会随着沿 y 轴旋转的动画而变化。

任何帮助表示赞赏。谢谢。

I wanna make a custom button in android extending buttons. When I will click on button, it will be flipped 180 degree and showed the back image of the buttons. That means the button will have two images. When it is clicked it will change with animation of rotation along y axis.

Any help are appreciated. Thanks.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

遇到 2024-12-16 07:56:56

首先为我可怜的恩感到抱歉。
然后...

你的布局 --> Flip.xml

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/root"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent">

<Button
    android:id="@+id/front"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:background="#ff00546c"
    android:clickable="true"
    android:onClick="onButtonClick"/>



<Button
    android:id="@+id/back"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:background="#000"
    android:clickable="true"
    android:onClick="onButtonClick"
    android:visibility="gone"/>


</RelativeLayout>

您定义按钮或任何视图的第一侧和第二侧。

在您的活动中添加这部分

public void onButtonClick(View view)
{
    flipView();
}

private void flipView()
{
    View rootLayout = findViewById(R.id.root);
    View viewFace = findViewById(R.id.front);
    View viewBack = findViewById(R.id.back);

    FlipAnimation flipAnimation = new FlipAnimation(viewFace , viewBack );

    if (viewFace.getVisibility() == View.GONE)
    {
        flipAnimation.reverse();
    }
    rootLayout.startAnimation(flipAnimation);
}

这部分代码启动翻转动画,最后一部分...

FlipAnimation.java

public class FlipAnimation extends Animation {
private Camera camera;

private View fromView;
private View toView;

private float centerX;
private float centerY;

private boolean forward = true;

public FlipAnimation(View fromView, View toView) {
    this.fromView = fromView;
    this.toView = toView;

    setDuration(700);
    setFillAfter(false);
    setInterpolator(new AccelerateDecelerateInterpolator());
}

public void reverse() {
    forward = false;
    View switchView = toView;
    toView = fromView;
    fromView = switchView;
}

@Override
public void initialize(int width, int height, int parentWidth, int parentHeight) {
    super.initialize(width, height, parentWidth, parentHeight);
    centerX = width / 2;
    centerY = height / 2;
    camera = new Camera();
}

@Override
protected void applyTransformation(float interpolatedTime, Transformation t)
{
    final double radians = Math.PI * interpolatedTime;
    float degrees = (float) (180.0 * radians / Math.PI);

    if (interpolatedTime >= 0.5f) {
        degrees -= 180.f;
        fromView.setVisibility(View.GONE);
        toView.setVisibility(View.VISIBLE);
    }

    if (forward)
        degrees = -degrees; //determines direction of rotation when flip begins

    final Matrix matrix = t.getMatrix();
    camera.save();
    camera.translate(0, 0, Math.abs(degrees)*2);
    camera.getMatrix(matrix);
    camera.rotateY(degrees);
    camera.getMatrix(matrix);
    camera.restore();
    matrix.preTranslate(-centerX, -centerY);
matrix.postTranslate(centerX, centerY);

}
}

享受!

还有一件事,我之前在堆栈中找到了这段代码,但没有找到链接的答案。其他人编写了这段有用的代码。

at first sorry for my poor En.
and after that...

your layout --> flip.xml

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/root"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent">

<Button
    android:id="@+id/front"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:background="#ff00546c"
    android:clickable="true"
    android:onClick="onButtonClick"/>



<Button
    android:id="@+id/back"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:background="#000"
    android:clickable="true"
    android:onClick="onButtonClick"
    android:visibility="gone"/>


</RelativeLayout>

you define your first and second side of your button or any view hear.

add this part in your activity

public void onButtonClick(View view)
{
    flipView();
}

private void flipView()
{
    View rootLayout = findViewById(R.id.root);
    View viewFace = findViewById(R.id.front);
    View viewBack = findViewById(R.id.back);

    FlipAnimation flipAnimation = new FlipAnimation(viewFace , viewBack );

    if (viewFace.getVisibility() == View.GONE)
    {
        flipAnimation.reverse();
    }
    rootLayout.startAnimation(flipAnimation);
}

this part of code start the flip animation, and the last part ...

FlipAnimation.java

public class FlipAnimation extends Animation {
private Camera camera;

private View fromView;
private View toView;

private float centerX;
private float centerY;

private boolean forward = true;

public FlipAnimation(View fromView, View toView) {
    this.fromView = fromView;
    this.toView = toView;

    setDuration(700);
    setFillAfter(false);
    setInterpolator(new AccelerateDecelerateInterpolator());
}

public void reverse() {
    forward = false;
    View switchView = toView;
    toView = fromView;
    fromView = switchView;
}

@Override
public void initialize(int width, int height, int parentWidth, int parentHeight) {
    super.initialize(width, height, parentWidth, parentHeight);
    centerX = width / 2;
    centerY = height / 2;
    camera = new Camera();
}

@Override
protected void applyTransformation(float interpolatedTime, Transformation t)
{
    final double radians = Math.PI * interpolatedTime;
    float degrees = (float) (180.0 * radians / Math.PI);

    if (interpolatedTime >= 0.5f) {
        degrees -= 180.f;
        fromView.setVisibility(View.GONE);
        toView.setVisibility(View.VISIBLE);
    }

    if (forward)
        degrees = -degrees; //determines direction of rotation when flip begins

    final Matrix matrix = t.getMatrix();
    camera.save();
    camera.translate(0, 0, Math.abs(degrees)*2);
    camera.getMatrix(matrix);
    camera.rotateY(degrees);
    camera.getMatrix(matrix);
    camera.restore();
    matrix.preTranslate(-centerX, -centerY);
matrix.postTranslate(centerX, centerY);

}
}

enjoy!

and one thing else, i found this code in stack before, but not found that answer to link. some one else wright this useful code.

最笨的告白 2024-12-16 07:56:56

https://github.com/stormzhang/FlipLayout -- 使用此库。它易于使用且适合您。

https://github.com/stormzhang/FlipLayout -- use this library. Its easy to use and for you.

秋凉 2024-12-16 07:56:55

我会使用 ScaleAnimation,它将按钮拉伸到 0 px 宽度,然后拉伸回 100%。当按钮具有最小宽度时,应更改背景和文本,以显示背面。

I would use a ScaleAnimation, which will stretch the button to 0 px width and then stretch it back to 100 %. When the button hast the minimal width, the background and text should be changed, in order to show the back side.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文