2014-01-18 90 views
-1

嗨,我正在開發一個應用程序使用圖像,我的要求是每當我點擊按鈕圖像必須移動底部到頂部,每當我點擊停止按鈕圖像必須停止position.but我我只得到無論是圖像移動底部或頂部移動圖像從下到上在android

我的代碼是

activity_main.xml中:

<?xml version="1.0" encoding="utf-8"?> 
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:id="@+id/rl_main" 
android:layout_width="match_parent" 
android:layout_height="match_parent" 
    android:background="@drawable/graphics" 
    android:orientation="vertical" > 
<LinearLayout android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:orientation="horizontal"> 
<Button android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:id="@+id/startButton" 
     android:text="START"/> 
<Button android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:id="@+id/stopButton" 
     android:text="STOP" 
     android:layout_marginLeft="150dip"/> 

</LinearLayout> 
<RelativeLayout 
android:id="@+id/rl_footer" 
android:layout_width="50dp" 
android:layout_height="50dp" 
android:layout_alignParentBottom="true" 
> 

    <ImageView 
    android:id="@+id/iv_up_arrow" 
    android:layout_width="45dp" 
    android:layout_height="45dp" 
    android:layout_centerHorizontal="true" 
    android:layout_centerVertical="true" 
    android:paddingBottom="10dp" 
    android:src="@drawable/download" /> 

    </RelativeLayout> 
    </RelativeLayout> 

MainActivity.java:

public class MainActivity extends Activity { 

RelativeLayout rl_footer; 
ImageView iv_header; 
boolean isBottom = true; 
Button btn1; 

@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_main); 
    rl_footer = (RelativeLayout) findViewById(R.id.rl_footer); 
    iv_header = (ImageView) findViewById(R.id.iv_up_arrow); 
    btn1 = (Button)findViewById(R.id.startButton); 
    btn1.setOnClickListener(new OnClickListener() { 

     @Override 
     public void onClick(View v) { 
      // TODO Auto-generated method stub 
      iv_header.setImageResource(R.drawable.download); 
      iv_header.setPadding(0, 10, 0, 0); 
      rl_footer.setBackgroundResource(R.drawable.download); 
      if (isBottom) { 
       SlideToAbove(); 

       isBottom = false; 
       iv_header.setImageResource(R.drawable.download); 

      }else { 
       iv_header.setImageResource(R.drawable.download); 
       iv_header.setPadding(0, 0, 0, 10); 
       rl_footer.setBackgroundResource(R.drawable.download); 
       SlideToDown(); 
       isBottom = true; 
      } 

     } 
    }); 

} 

public void SlideToAbove() { 
    final Animation slide ; 
    slide = new TranslateAnimation(Animation.RELATIVE_TO_SELF, 0.0f, 
      Animation.RELATIVE_TO_SELF, 0.0f, Animation.RELATIVE_TO_SELF, 
      0.0f, Animation.RELATIVE_TO_SELF, 0.0f); 

    slide.setDuration(400); 
    slide.setFillAfter(true); 
    slide.setFillEnabled(true); 
    rl_footer.startAnimation(slide); 

    slide.setAnimationListener(new AnimationListener() { 

     @Override 
     public void onAnimationStart(Animation animation) { 

     } 

     @Override 
     public void onAnimationRepeat(Animation animation) { 
     } 

     @Override 
     public void onAnimationEnd(Animation animation) { 

      rl_footer.clearAnimation(); 

      RelativeLayout.LayoutParams lp = new RelativeLayout.LayoutParams(
        rl_footer.getWidth(), rl_footer.getHeight()); 
      lp.setMargins(0, 0, 0, 0); 
      lp.addRule(RelativeLayout.ALIGN_PARENT_TOP); 
      rl_footer.setLayoutParams(lp); 

     } 

    }); 

} 

public void SlideToDown() { 
    final Animation slide ; 
    slide = new TranslateAnimation(Animation.RELATIVE_TO_SELF, 0.0f, 
      Animation.RELATIVE_TO_SELF, 0.0f, Animation.RELATIVE_TO_SELF, 
      0.0f, Animation.RELATIVE_TO_SELF, 5.2f); 

    slide.setDuration(500); 
    slide.setFillAfter(true); 
    slide.setFillEnabled(true); 
    rl_footer.startAnimation(slide); 

    slide.setAnimationListener(new AnimationListener() { 

     @Override 
     public void onAnimationStart(Animation animation) { 

     } 

     @Override 
     public void onAnimationRepeat(Animation animation) { 


     } 

     @Override 
     public void onAnimationEnd(Animation animation) { 

      rl_footer.clearAnimation(); 

      RelativeLayout.LayoutParams lp = new RelativeLayout.LayoutParams(
        rl_footer.getWidth(), rl_footer.getHeight()); 
      lp.setMargins(0, rl_footer.getWidth(), 0, 0); 
      lp.addRule(RelativeLayout.ALIGN_PARENT_BOTTOM); 
      rl_footer.setLayoutParams(lp); 

     } 

    }); 

} 

@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; 
} 

    } 

回答

0

刪除此行

slide.setFillAfter(true); 

這使圖像當你停止動畫跳到結尾。

您必須添加代碼以在停止後設置圖像位置。

+0

你刪除仍然不工作 – Durga

+0

你還添加了代碼來設置圖像的位置?看到我的回答 –

+0

什麼代碼我必須添加 – Durga

0

我建議你看看Object Animator這比普通的更強大。

如果您的應用程序的API級別小於9,您可以參考this library仍然可以使用這些方法。這個庫也有一些關於如何管理你想要的例子。