android实现translate位移动画效果

来源:互联网 发布:网络新词及意思 编辑:程序博客网 时间:2024/06/06 12:33

位移,也就是移动,可以帮助我们实现一个空间或者布局的移动效果,在动画里面可谓是应用最多的一个吧。下面我们来仔细研究一下这个动画。

实现过程依然有两个,一个是自定义anim文件。如下:

<translate xmlns:android="http://schemas.android.com/apk/res/android"    android:fromYDelta="0"    android:toYDelta="45%p"    android:fillAfter="true"    android:duration="2000"></translate>
<translate xmlns:android="http://schemas.android.com/apk/res/android"    android:fromXDelta="0"    android:fromYDelta="45%p"    android:toYDelta="45%p"    android:toXDelta="45%p"    android:fillAfter="true"    android:duration="2000"></translate>
<translate xmlns:android="http://schemas.android.com/apk/res/android"    android:fromXDelta="45%p"    android:toXDelta="0"    android:fromYDelta="45%p"    android:toYDelta="0"    android:fillAfter="true"    android:duration="2000"></translate>
以上是三种效果的动画文件,有需要的盆友们可以分别运行看看效果,属性改的多了自然也就知道每个属性分别代表什么意思了。以下给出activity中的代码,至于xml的代码,我想只要接触过的都会写,很简单,最下方会有界面图片展示。
public class TranslateAnimationDemoActivity extends Activity implementsOnClickListener, AnimationListener {private ImageView mImageView;private TranslateAnimation translateAnim1;private TranslateAnimation translateAnim2;private TranslateAnimation translateAnim3;private int t = 1;/** Called when the activity is first created. */@Overridepublic void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.main);findViewById(R.id.btn_translate).setOnClickListener(this);mImageView = (ImageView) findViewById(R.id.imageview);}public void onClick(View v) {startTranslateAnimationJavaCode();// stertTranslateAnimationXml();// android:fromXDelta="45%p" 动画的初始x位置// android:toXDelta="0" 动画结束时x的位置// android:fromYDelta="45%p" 动画开始时y的位置 20% 相对于自身 20%p相对于屏幕// android:toYDelta="0" 动画结束时Y的位置// android:fillAfter="true" 动画播放后的位置// android:duration="2000" 动画播放时间// 动画设置监听// setListen();}private void stertTranslateAnimationXml() {translateAnim1 = (TranslateAnimation) AnimationUtils.loadAnimation(this, R.anim.todown);mImageView.setAnimation(translateAnim1);translateAnim2 = (TranslateAnimation) AnimationUtils.loadAnimation(this, R.anim.toright);// mImageView.setAnimation(translateAnim2);translateAnim3 = (TranslateAnimation) AnimationUtils.loadAnimation(this, R.anim.totop);// mImageView.setAnimation(translateAnim3);}private void setListen() {translateAnim1.setAnimationListener(this);translateAnim2.setAnimationListener(this);translateAnim3.setAnimationListener(this);}private void startTranslateAnimationJavaCode() {// 动画移动相当于自己大小的多少 3f 代表组件的3倍宽 或 高 移动TranslateAnimation translateAnim = new TranslateAnimation(Animation.RELATIVE_TO_SELF, 0f, Animation.RELATIVE_TO_SELF,10f, Animation.RELATIVE_TO_SELF, 0f,Animation.RELATIVE_TO_SELF, 10f);translateAnim.setDuration(2000);mImageView.setAnimation(translateAnim);}public void onAnimationStart(Animation animation) {// TODO Auto-generated method stub}public void onAnimationEnd(Animation animation) {if (t == 1) {mImageView.startAnimation(translateAnim2);t = 2;} else if (t == 2) {mImageView.startAnimation(translateAnim3);t = 3;} else if (t == 3) {mImageView.startAnimation(translateAnim1);t = 1;} else {Log.e("TranslateAnimationDemoActivity", "见鬼了");}}public void onAnimationRepeat(Animation animation) {// TODO Auto-generated method stub}}
图片设置的有点小,还望广大博友加以理解,当时现成看到的图片就直接用了。

里面涉及几种效果,可以把注销掉的内容还原,把原来的动画效果注释掉继续运行,你会看到不一样的结果。应该这里面还涉及了动画的监听,可以监听到动画的开始,结束,以及重放。具体的在以后单独说明。有不好之处还请见谅,以后会多多修改。


                                           完毕!


0 2
原创粉丝点击