2011-10-12 56 views
0

我想在android延伸按鈕做一個自定義按鈕。當我點擊按鈕時,它會翻轉180度並顯示按鈕的後面圖像。這意味着按鈕將有兩個圖像。當它被點擊時,它將隨着沿着y軸的旋轉動畫而改變。如何翻轉android按鈕

任何幫助表示讚賞。謝謝。

回答

0

我會使用ScaleAnimation,它會將按鈕拉伸到0 px寬度,然後將其拉伸回100%。當按鈕具有最小寬度時,背景和文本應該改變,以顯示背面。

0

起初對我可憐的恩抱歉。 ,之後...

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

} 
} 

享受!

還有一件事,我發現這個代碼在堆棧之前,但沒有找到答案鏈接。有些人願意這個有用的代碼。