2012-07-30 81 views
2

我有一個下面的代碼翻頁,但顯示相同的佈局和相同的活動,我想改變這個代碼時,按下按鈕顯示另一個屏幕不同的佈局如何可能?下面源代碼中給出如何在按下按鈕時顯示另一個佈局?

package com.samir; 

import android.app.Activity; 
import android.os.Bundle; 
import android.view.View; 
import android.view.View.OnClickListener; 
import android.view.animation.AccelerateInterpolator; 
import android.widget.Button; 
import android.widget.RelativeLayout; 

import com.samir.anim.DisplayNextView; 
import com.samir.anim.Rotate3dAnimation; 

public class MainActivity extends Activity implements OnClickListener { 

Button btnRotateit, btnRotateBack; 
RelativeLayout mContainer, relChild1, relChild2; 
private boolean isFirstView; 

@Override 
public void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.main); 

    mContainer = (RelativeLayout) findViewById(R.id.mainlayout); 
    relChild1 = (RelativeLayout) findViewById(R.id.child1); 
    relChild2 = (RelativeLayout) findViewById(R.id.child2); 

    btnRotateit = (Button) findViewById(R.id.btnrotate); 
    btnRotateit.setOnClickListener(this); 
    btnRotateBack = (Button) findViewById(R.id.btnback); 
    btnRotateBack.setOnClickListener(this); 
} 

@Override 
public void onClick(View view) { 
    switch (view.getId()) { 
    case R.id.btnrotate: 
     isFirstView = true; 
     applyRotation(0, 90); 
     break; 
    case R.id.btnback: 
     isFirstView = false; 
     applyRotation(0, -90); 
     break; 
    default: 
     break; 
    } 

} 

private void applyRotation(float start, float end) { 
    final float centerX = mContainer.getWidth()/2.0f; 
    final float centerY = mContainer.getHeight()/2.0f; 

    // The animation listener is used to trigger the next animation 
    final Rotate3dAnimation rotation = new Rotate3dAnimation(start, end, 
      centerX, centerY, 310.0f, true); 
    rotation.setDuration(1200); 
    rotation.setFillAfter(true); 
    rotation.setInterpolator(new AccelerateInterpolator()); 
    rotation.setAnimationListener(new DisplayNextView(isFirstView, 
      mContainer, relChild1, relChild2)); 

    mContainer.startAnimation(rotation); 

} 
    } 



    package com.samir.anim; 

    import android.view.View; 
import android.view.animation.Animation; 
import android.view.animation.Animation.AnimationListener; 
import android.view.animation.DecelerateInterpolator; 
import android.widget.RelativeLayout; 

public class DisplayNextView implements AnimationListener { 
RelativeLayout mContainer, relChild1, relChild2; 
boolean isFirstView; 

public DisplayNextView(boolean isFirstView, RelativeLayout mContainer, 
     RelativeLayout relChild1, RelativeLayout relChild2) { 
    this.mContainer = mContainer; 
    this.relChild1 = relChild1; 
    this.relChild2 = relChild2; 

    this.isFirstView = isFirstView; 
} 

@Override 
public void onAnimationEnd(Animation animation) { 
    mContainer.post(new SwapViews()); 
} 

@Override 
public void onAnimationRepeat(Animation animation) { 
} 

@Override 
public void onAnimationStart(Animation animation) { 
} 

public class SwapViews implements Runnable { 
    @Override 
    public void run() { 

     final float centerX = mContainer.getWidth()/2.0f; 
     final float centerY = mContainer.getHeight()/2.0f; 
     Rotate3dAnimation rotation; 

     if (isFirstView) { 
      rotation = new Rotate3dAnimation(-90, 0, centerX, centerY, 
        310.0f, false); 
     } else { 
      rotation = new Rotate3dAnimation(90, 0, centerX, centerY, 
        310.0f, false); 
     } 
     rotation.setDuration(1500); 
     rotation.setFillAfter(true); 
     rotation.setInterpolator(new DecelerateInterpolator()); 

     mContainer.startAnimation(rotation); 

     if (isFirstView) { 
      relChild1.setVisibility(View.GONE); 
      relChild2.setVisibility(View.VISIBLE); 
     } else { 
      relChild2.setVisibility(View.GONE); 
      relChild1.setVisibility(View.VISIBLE); 

     } 
    } 

} 

    } 




    package com.samir.anim; 

import android.graphics.Camera; 
    import android.graphics.Matrix; 
    import android.view.animation.Animation; 
import android.view.animation.Transformation; 

public class Rotate3dAnimation extends Animation { 
private final float mFromDegrees; 
private final float mToDegrees; 
private final float mCenterX; 
private final float mCenterY; 
private final float mDepthZ; 
private final boolean mReverse; 
private Camera mCamera; 

public Rotate3dAnimation(float fromDegrees, float toDegrees, float centerX, 
     float centerY, float depthZ, boolean reverse) { 
    mFromDegrees = fromDegrees; 
    mToDegrees = toDegrees; 
    mCenterX = centerX; 
    mCenterY = centerY; 
    mDepthZ = depthZ; 
    mReverse = reverse; 
} 

@Override 
public void initialize(int width, int height, int parentWidth, 
     int parentHeight) { 
    super.initialize(width, height, parentWidth, parentHeight); 
    mCamera = new Camera(); 
} 

@Override 
protected void applyTransformation(float interpolatedTime, Transformation t) { 
    final float fromDegrees = mFromDegrees; 
    float degrees = fromDegrees 
      + ((mToDegrees - fromDegrees) * interpolatedTime); 

    final float centerX = mCenterX; 
    final float centerY = mCenterY; 
    final Camera camera = mCamera; 

    final Matrix matrix = t.getMatrix(); 

    camera.save(); 
    if (mReverse) { 
     camera.translate(0.0f, 0.0f, mDepthZ * interpolatedTime); 
    } else { 
     camera.translate(0.0f, 0.0f, mDepthZ * (1.0f - interpolatedTime)); 
    } 
    camera.rotateY(degrees); 
    camera.getMatrix(matrix); 
    camera.restore(); 

    matrix.preTranslate(-centerX, -centerY); 
    matrix.postTranslate(centerX, centerY); 
} 
     } 


     <?xml version="1.0" encoding="utf-8"?> 
    <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
android:id="@+id/mainlayout" 
android:layout_width="fill_parent" 
android:layout_height="fill_parent" 
android:background="#DCDCDC" 
android:orientation="vertical" > 

<RelativeLayout 
    android:id="@+id/child1" 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent" > 

    <Button 
     android:id="@+id/btnrotate" 
     android:layout_width="170dp" 
     android:layout_height="40dp" 
     android:layout_centerHorizontal="true" 
     android:layout_centerVertical="true" 
     android:background="@drawable/btnrotate" 
     android:text="@string/rotateit" /> 
</RelativeLayout> 

<RelativeLayout 
    android:id="@+id/child2" 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent" 
    android:visibility="gone" > 

    <Button 
     android:id="@+id/btnback" 
     android:layout_width="170dp" 
     android:layout_height="40dp" 
     android:layout_centerHorizontal="true" 
     android:layout_centerVertical="true" 
     android:background="@drawable/btnback" 
     android:text="@string/rotateback" /> 
    </RelativeLayout> 

     </RelativeLayout> 

回答

1
onClick(View v) 
{ 
    setContentView(R.layout.another); 
          ^^^^^^^ 
} 

編輯: 例如,你有兩個視圖中,可以設置可見INVISIBLEGONE

和的onClick應用組件的旋轉和變化能見度

+0

,但如何在 使用不同勢佈局值ublic DisplayNextView(布爾isFirstView,RelativeLayout的mContainer, RelativeLayout的relChild1,RelativeLayout的relChild2){ 功能 – 2012-07-30 08:24:23

+0

我嘗試它的代碼是不工作bcoz爆炸這裏1父母的佈局和兩個孩子的佈局如何在這段代碼中使用2個不同的佈局?任何想法?? – 2012-07-30 08:25:48

+0

看到我編輯的答案 – MAC 2012-07-30 08:29:26

0

檢查此密碼

private View view = null ; 
Button addbutton = null; 
ViewGroup parent = null; 
LayoutInflater inflater = null; 

inflater= (LayoutInflater)this.getSystemService(LAYOUT_INFLATER_SERVICE); 
addbutton = (Button)findViewById(R.id.btnAdd); 

parent = (ViewGroup) findViewById(R.id.mainxml); 


addbutton.setOnClickListener(new View.OnClickListener() { 
     @Override 
     public void onClick(View view) { 
     view = inflater.inflate(R.layout.other, null); 
     parent.addView(view); 
    } 
}); 
+0

告訴我如何設置 rotation.setAnimationListener(新的DisplayNextView(isFirstView, mContainer,relChild1,relChild2))另一個佈局的參數; suppsoe我craete新佈局main2.xml和另一個活動secondpage.java所以我怎麼稱呼這個頁面時,按鈕預先在mainacitivity? – 2012-07-30 08:29:18

2

您onclicklistener方法使用setContentView像:

setContentView(R.layout.layout); 

編輯:我想你想的時候btnRotateit是按那麼你想顯示relChild1relChild2並再次按下btnRotateBack然後出現mContainer,那就試試像:

添加代碼的btnRotateit的onclick方法:

relChild1.setVisibility(View.VISIBLE); 
relChild2.setVisibility(View.VISIBLE); 
mContainer.setVisibility(View.GONE); 

,並添加上btnRotateBack的onclick方法的代碼:

relChild1.setVisibility(View.GONE); 
relChild2.setVisibility(View.GONE); 
mContainer.setVisibility(View.VISIBLE); 

試試這個,讓我知道。

0

從您對vishwa的迴應中,您似乎不希望更改現有活動的佈局,以至於無法更改爲其他活動。在其中追逐:

Intent intent = new Intent(this, secondpage.class); 
startActivity(intent); 
相關問題