偶然的机会搞了下位移动画之循环往复的动画,不多说 直接上代码!
XML文件。
activity_base.xml文件
1 <?xml version="1.0" encoding="utf-8"?> 2 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 3 android:id="@+id/baseActivityLinearLayout" 4 android:layout_width="fill_parent" 5 android:layout_height="fill_parent" 6 android:background="@drawable/base_bg" 7 android:orientation="vertical" > 8 9 <RelativeLayout 10 android:id="@+id/baseActivityTitleBarRelativeLayout" 11 android:layout_width="fill_parent" 12 android:layout_height="50dip" 13 android:layout_gravity="top" 14 android:background="@drawable/ic_pulltorefresh_arrow" 15 android:gravity="center_vertical" > 16 17 <LinearLayout 18 android:id="@+id/baseActivityTitleBarLeftView" 19 android:layout_width="wrap_content" 20 android:layout_height="fill_parent" 21 android:layout_alignParentLeft="true" 22 android:layout_centerVertical="true" 23 android:layout_marginLeft="5dip" 24 android:orientation="vertical" > 25 26 <LinearLayout 27 android:id="@+id/baseActivityTitleBarLeftViewLinearLayout" 28 android:layout_width="wrap_content" 29 android:layout_height="fill_parent" 30 android:orientation="vertical" /> 31 32 <Button 33 android:id="@+id/baseActivityTitleBarLeftButton" 34 android:layout_width="wrap_content" 35 android:layout_height="fill_parent" 36 android:background="@null" 37 android:textColor="#FFFFFFFF" 38 android:textSize="20sp" /> 39 </LinearLayout> 40 41 <LinearLayout 42 android:id="@+id/baseActivityTitleBarRightView" 43 android:layout_width="wrap_content" 44 android:layout_height="fill_parent" 45 android:layout_alignParentRight="true" 46 android:layout_centerVertical="true" 47 android:layout_marginRight="5dip" 48 android:orientation="vertical" > 49 50 <LinearLayout 51 android:id="@+id/baseActivityTitleBarRightViewLinearLayout" 52 android:layout_width="wrap_content" 53 android:layout_height="fill_parent" 54 android:orientation="vertical" /> 55 56 <Button 57 android:id="@+id/baseActivityTitleBarRightButton" 58 android:layout_width="wrap_content" 59 android:layout_height="fill_parent" 60 android:background="@null" 61 android:textColor="#FFFFFFFF" 62 android:textSize="20sp" /> 63 </LinearLayout> 64 65 <LinearLayout 66 android:id="@+id/baseActivityTitleBarCenterView" 67 android:layout_toLeftOf="@id/baseActivityTitleBarRightView" 68 android:layout_toRightOf="@id/baseActivityTitleBarLeftView" 69 android:layout_width="wrap_content" 70 android:layout_height="fill_parent" 71 android:layout_alignParentRight="true" 72 android:layout_centerInParent="true" 73 android:orientation="vertical" 74 android:gravity="center"> 75 76 <TextView 77 android:id="@+id/baseActivityTitleBarCenterTextView" 78 android:layout_width="wrap_content" 79 android:layout_height="fill_parent" 80 android:gravity="center" 81 android:textColor="#FFFFFFFF" 82 android:textSize="20sp" /> 83 84 <LinearLayout 85 android:id="@+id/baseActivityTitleBarCenterViewLinearLayout" 86 android:layout_width="wrap_content" 87 android:layout_height="fill_parent" 88 android:orientation="vertical" /> 89 </LinearLayout> 90 </RelativeLayout> 91 92 <LinearLayout 93 android:id="@+id/baseActivityContentLinearLayout" 94 android:layout_width="fill_parent" 95 android:layout_height="fill_parent" 96 android:orientation="vertical" /> 97 98 </LinearLayout>
activity_main.xml
1 <?xml version="1.0" encoding="utf-8"?> 2 3 <LinearLayout 4 xmlns:android="http://schemas.android.com/apk/res/android" 5 android:layout_width="fill_parent" 6 android:layout_height="fill_parent" 7 > 8 <LinearLayout 9 android:layout_width="300dip" 10 android:layout_height="300dip" 11 android:background="#FF0000FF" 12 android:layout_margin="10dip" 13 > 14 <ImageView 15 android:id="@+id/imageView" 16 android:layout_width="fill_parent" 17 android:layout_height="wrap_content" 18 android:src="@drawable/ic_launcher" 19 android:scaleType="center" 20 /> 21 22 </LinearLayout> 23 </LinearLayout>
BaseActivity.xml
1 package com.xinye.ui; 2 3 import android.app.Activity; 4 import android.content.res.Resources; 5 import android.os.Bundle; 6 import android.view.LayoutInflater; 7 import android.view.Window; 8 import android.widget.Button; 9 import android.widget.LinearLayout; 10 import android.widget.RelativeLayout; 11 import android.widget.TextView; 12 13 import com.xinye.R; 14 /** 15 * 所有Activity的父类 16 * @author xinye 17 * 18 */ 19 public abstract class BaseActivity extends Activity { 20 /** 21 * TitleBar 22 */ 23 protected TitleBar titleBar; 24 /** 25 * 布局填充器 26 */ 27 protected LayoutInflater layoutInflater; 28 /** 29 * 页面主要内容的显示View 30 */ 31 protected LinearLayout contentView; 32 /** 33 * 布局参数 34 */ 35 protected LinearLayout.LayoutParams contentViewDefaultParams; 36 /** 37 * Resource对象 38 */ 39 protected Resources resources; 40 41 @Override 42 protected void onCreate(Bundle savedInstanceState) { 43 super.onCreate(savedInstanceState); 44 // 设置无标题 45 requestWindowFeature(Window.FEATURE_NO_TITLE); 46 // 设置内容显示的View 47 setContentView(R.layout.activity_base); 48 // Resource对象 49 resources = getResources(); 50 // 得到LayoutInflater 51 layoutInflater = getLayoutInflater(); 52 // 初始化TitleBar 53 initTitleBar(); 54 // 初始化ContentView 55 initContentView(); 56 } 57 /** 58 * 初始化ContentView 59 */ 60 private void initContentView() { 61 contentView = (LinearLayout) findViewById(R.id.baseActivityContentLinearLayout); 62 contentViewDefaultParams = new LinearLayout.LayoutParams( 63 LinearLayout.LayoutParams.FILL_PARENT, 64 LinearLayout.LayoutParams.FILL_PARENT); 65 inflateContentView(); 66 } 67 /** 68 * 填充view到ContentView 69 * @param view 70 */ 71 protected abstract void inflateContentView(); 72 /** 73 * 初始化TitleBar 74 */ 75 private void initTitleBar() { 76 titleBar = new TitleBar(layoutInflater); 77 78 titleBar.titleBar = (RelativeLayout) findViewById(R.id.baseActivityTitleBarRelativeLayout); 79 80 titleBar.leftButton = (Button) findViewById(R.id.baseActivityTitleBarLeftButton); 81 titleBar.leftView = (LinearLayout) findViewById(R.id.baseActivityTitleBarLeftViewLinearLayout); 82 83 titleBar.centerTextView = (TextView) findViewById(R.id.baseActivityTitleBarCenterTextView); 84 titleBar.centerView = (LinearLayout) findViewById(R.id.baseActivityTitleBarCenterViewLinearLayout); 85 86 titleBar.rightButton = (Button) findViewById(R.id.baseActivityTitleBarRightButton); 87 titleBar.rightView = (LinearLayout) findViewById(R.id.baseActivityTitleBarRightViewLinearLayout); 88 } 89 90 }
MainActivity.java
1 package com.xinye.ui; 2 3 import java.util.ArrayList; 4 5 import android.os.Handler; 6 import android.view.View; 7 import android.view.animation.Animation; 8 import android.view.animation.TranslateAnimation; 9 import android.widget.ImageView; 10 import android.widget.ListView; 11 12 import com.xinye.R; 13 import com.xinye.adapter.MainActivityListViewAdapter; 14 /** 15 * MainActivity 16 * @author xinye 17 * 18 */ 19 public class MainActivity extends BaseActivity{ 20 private Handler mHandler = new Handler(); 21 private ImageView mImageView; 22 private View mView; 23 @Override 24 protected void inflateContentView() { 25 mView = layoutInflater.inflate(R.layout.activity_main, null); 26 mImageView = (ImageView) mView.findViewById(R.id.imageView); 27 contentView.addView(mView, contentViewDefaultParams); 28 mView.measure(50, 50); 29 mImageView.measure(50, 50); 30 new Thread(new Runnable() { 31 @Override 32 public void run() { 33 mHandler.post(new Runnable() { 34 @Override 35 public void run() { 36 TranslateAnimation ta1 = new TranslateAnimation( 37 TranslateAnimation.RELATIVE_TO_PARENT, 0, 38 TranslateAnimation.RELATIVE_TO_PARENT, 0 , 39 TranslateAnimation.RELATIVE_TO_PARENT, 0, 40 TranslateAnimation.RELATIVE_TO_PARENT, 41 1 - (mImageView.getMeasuredHeight()) * 1.0f / mView.getMeasuredHeight()); 42 ta1.setDuration(2000); 43 ta1.setStartTime(0); 44 ta1.setRepeatCount(Integer.MAX_VALUE); 45 ta1.setRepeatMode(Animation.REVERSE); 46 mImageView.startAnimation(ta1); 47 } 48 }); 49 } 50 }).start(); 51 52 } 53 }
效果图: