【发布时间】:2011-10-12 12:54:38
【问题描述】:
我想在 android 扩展按钮中制作一个自定义按钮。当我点击按钮时,它会翻转 180 度并显示按钮的背面图像。这意味着该按钮将有两个图像。当它被点击时,它会随着沿 y 轴的旋转动画而改变。
感谢任何帮助。谢谢。
【问题讨论】:
标签: android animation button viewflipper flip
我想在 android 扩展按钮中制作一个自定义按钮。当我点击按钮时,它会翻转 180 度并显示按钮的背面图像。这意味着该按钮将有两个图像。当它被点击时,它会随着沿 y 轴的旋转动画而改变。
感谢任何帮助。谢谢。
【问题讨论】:
标签: android animation button viewflipper flip
我会使用ScaleAnimation,它将按钮拉伸到 0 像素宽度,然后将其拉伸回 100%。当按钮达到最小宽度时,应该改变背景和文字,以显示背面。
【讨论】:
起初很抱歉我可怜的恩。 然后……
你的布局 --> 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);
}
}
享受吧!
还有一件事,我之前在堆栈中找到了这段代码,但没有找到链接的答案。其他人编写了这个有用的代码。
【讨论】:
https://github.com/stormzhang/FlipLayout -- 使用这个库。它易于使用,适合您。
【讨论】: