2014-01-18 91 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仍然可以使用这些方法。这个库也有一些关于如何管理你想要的例子。