2014-02-18 83 views
1

我在我所父佈局的按鈕的活動,則有在我想此父layout.What高度的相對佈局,寬度(100,100)是:調整大小通過動畫的視圖中的Android

  1. 當我點擊母版面上的按鈕。然後相對版面從屏幕位置滑到 屏幕。
  2. 和未來的後篩選當我點擊這個相對佈局它 動畫通過調整其寬度高度FILL_PARENT
  3. 然後後再次點擊此相對佈局它以動畫方式返回 原來的大小說(100,100)

我通過簡單的翻譯動畫實現了任務1。

任務2也是在這裏實現的是我的任務2碼

slide.setLayoutParams(new RelativeLayout.LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT)); 

slide.startAnimation(zoom_in); 

我這個任務縮放動畫是

<?xml version="1.0" encoding="utf-8"?> 
<set xmlns:android="http://schemas.android.com/apk/res/android" 
android:interpolator="@android:anim/decelerate_interpolator"> 
<scale android:fromXScale="0.5" android:toXScale="1.0" 
    android:fromYScale="0.5" android:toYScale="1.0" 
    android:pivotX="0%p" android:pivotY="0%p" 
    android:duration="800" /> 
</set> 

我要做的就是在這個相對佈局我改變用戶點擊這個佈局的佈局參數從100到匹配父項並應用動畫,並且它像魅力一樣工作。

我的問題是我沒有任何線索如何執行任務3對此將高度BTW,我使用以下方法來實現的任務3,但沒有達到:(

slide.setLayoutParams(new RelativeLayout.LayoutParams(200, 200)); 
slide.startAnimation(zoom_exit); 
讚賞

任何幫助

XML文件:

<?xml version="1.0" encoding="utf-8"?> 
<scale 
    xmlns:android="http://schemas.android.com/apk/res/android" 
    android:duration="1000" 
    android:fromXScale="1.0" 
    android:fromYScale="1.0" 
    android:pivotX="50%" 
    android:pivotY="50%" 
    android:toXScale="0.5" 
    android:toYScale="0.5" > 
</scale> 

我的完整的Java代碼

public class MainActivity extends Activity { 

Button b,b1; 
RelativeLayout slide; 
Animation move_in,zoom_out,zoom_exit; 
int x=0; 

@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_main); 
    b=(Button) findViewById(R.id.button1); 
    b1=(Button) findViewById(R.id.button2); 
    slide=(RelativeLayout) findViewById(R.id.slider); 
    move_in = AnimationUtils.loadAnimation(getApplicationContext(), 
      R.anim.move_in); 
    zoom_out = AnimationUtils.loadAnimation(getApplicationContext(), 
      R.anim.zoom_enter1); 
    zoom_exit = AnimationUtils.loadAnimation(getApplicationContext(), 
      R.anim.test2); 

    b1.setOnClickListener(new OnClickListener() { 

     @Override 
     public void onClick(View arg0) { 
      // TODO Auto-generated method stub 
      if(x == 0){ 



      slide.setLayoutParams(new RelativeLayout.LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT)); 


      slide.startAnimation(zoom_out); 

      x=1;} 
      else { 

       //slide.setLayoutParams(new RelativeLayout.LayoutParams(200, 200)); 


        slide.startAnimation(zoom_exit); 


        x=0; 


      } 

     } 
    }); 

    b.setOnClickListener(new OnClickListener() { 

     @Override 
     public void onClick(View arg0) { 
      // TODO Auto-generated method stub 

      /*Intent i2 = new Intent(MainActivity.this, MainActivity1.class); 
      startActivity(i2); 
      finish(); 
      overridePendingTransition(R.anim.zoom_enter1, R.anim.zoom_exit);*/ 

      slide.setVisibility(View.VISIBLE); 
      slide.startAnimation(move_in); 



     } 
    }); 





} 

@Override 
public boolean onCreateOptionsMenu(Menu menu) { 
    // Inflate the menu; this adds items to the action bar if it is present. 
    getMenuInflater().inflate(R.menu.main, menu); 
    return true; 
} 

}

我的完整的XML

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
xmlns:tools="http://schemas.android.com/tools" 
android:layout_width="match_parent" 
android:layout_height="match_parent" 

tools:context=".MainActivity" > 

<TextView 
    android:id="@+id/textView1" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:text="@string/hello_world" /> 

<Button 
    android:id="@+id/button1" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:layout_below="@+id/textView1" 
    android:layout_centerHorizontal="true" 
    android:layout_marginTop="64dp" 
    android:text="Button" /> 

<RelativeLayout 

    android:id="@+id/slider" 
    android:layout_height="200dp" 
    android:layout_width="200dp" 
    android:background="#111111" 
    android:visibility="gone" 
    android:layout_alignParentTop="true" 
    > 


    <Button 
    android:id="@+id/button2" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 

    android:layout_centerHorizontal="true" 
    android:layout_marginTop="64dp" 
    android:text="Button" /> 


</RelativeLayout> 




</RelativeLayout> 

移動動畫

<?xml version="1.0" encoding="utf-8"?> 
<set xmlns:android="http://schemas.android.com/apk/res/android"> 
<translate android:fromYDelta="-100%" android:toYDelta="0%" android:duration="400"/> 
</set> 

回答

1

主要類

package com.testanimation; 

    import android.app.Activity; 
    import android.os.Bundle; 
    import android.view.View; 
    import android.view.View.OnClickListener; 
    import android.view.animation.Animation; 
    import android.view.animation.Animation.AnimationListener; 
    import android.view.animation.AnimationUtils; 
    import android.widget.Button; 
    import android.widget.RelativeLayout; 
    import android.widget.RelativeLayout.LayoutParams; 

    public class MainActivity extends Activity { 
     Button Btn, btn1; 
     RelativeLayout slide; 
     Animation move_in, zoom_out, zoom_exit; 
     int x = 0; 

     @Override 
     protected void onCreate(Bundle savedInstanceState) { 
      super.onCreate(savedInstanceState); 
      setContentView(R.layout.activity_main); 

      Btn = (Button) findViewById(R.id.button1); 
      btn1 = (Button) findViewById(R.id.button2); 
      slide = (RelativeLayout) findViewById(R.id.slider); 
      move_in = AnimationUtils.loadAnimation(getApplicationContext(), 
        R.anim.move); 
      zoom_out = AnimationUtils.loadAnimation(getApplicationContext(), 
        R.anim.zoomin); 
      zoom_exit = AnimationUtils.loadAnimation(getApplicationContext(), 
        R.anim.zoomout); 

      zoom_exit.setAnimationListener(new AnimationListener() { 

       @Override 
       public void onAnimationStart(Animation animation) { 
        // TODO Auto-generated method stub 

       } 

       @Override 
       public void onAnimationRepeat(Animation animation) { 
        // TODO Auto-generated method stub 

       } 

       @Override 
       public void onAnimationEnd(Animation animation) { 
        // TODO Auto-generated method stub 
        slide.setLayoutParams(new RelativeLayout.LayoutParams(400, 410)); 

       } 
      }); 
      btn1.setOnClickListener(new OnClickListener() { 

       @Override 
       public void onClick(View arg0) { 
        // TODO Auto-generated method stub 
        if (x == 0) { 

         slide.setLayoutParams(new RelativeLayout.LayoutParams(
           LayoutParams.MATCH_PARENT, 
           LayoutParams.MATCH_PARENT)); 

         slide.startAnimation(zoom_out); 

         x = 1; 
        } else { 

         slide.startAnimation(zoom_exit); 

         x = 0; 

        } 

       } 
      }); 

      Btn.setOnClickListener(new OnClickListener() { 

       @Override 
       public void onClick(View arg0) { 
        slide.setVisibility(View.VISIBLE); 
        slide.startAnimation(move_in); 

       } 
      }); 

     } 

    } 

移動的Xml

<?xml version="1.0" encoding="utf-8"?> 
<set xmlns:android="http://schemas.android.com/apk/res/android" > 

    <translate 
     android:duration="400" 
     android:fromYDelta="-100%" 
     android:toYDelta="0%" /> 

</set> 

縮小(ZoomOut)

<?xml version="1.0" encoding="utf-8"?> 
<scale xmlns:android="http://schemas.android.com/apk/res/android" 
    android:duration="1000" 
    android:fromXScale="1.0" 
    android:fromYScale="1.0" 
    android:pivotX="-5%" 
    android:pivotY="-20%" 
    android:toXScale="0.6" 
    android:toYScale="0.5" > 

</scale> 

Zoomin

<?xml version="1.0" encoding="utf-8"?> 
<set xmlns:android="http://schemas.android.com/apk/res/android" 
    android:interpolator="@android:anim/decelerate_interpolator" > 

    <scale 
     android:duration="800" 
     android:fromXScale="0.5" 
     android:fromYScale="0.5" 
     android:pivotX="0%p" 
     android:pivotY="0%p" 
     android:toXScale="1.0" 
     android:toYScale="1.0" /> 

</set> 
+0

不工作的兄弟:( – hussi

+1

你能給我整個代碼,所以我可以嘗試 –

+0

添加完整的代碼的問題PLZ檢查 – hussi