首页 > 代码库 > TranslateAnimation类:位置变化动画类 (类似tab切换效果)
TranslateAnimation类:位置变化动画类 (类似tab切换效果)
TranslateAnimation类是Android系统中的位置变化动画类,用于控制View对象的位置变化,该类继承于Animation类。TranslateAnimation类中的很多方法都与Animation类一致,该类中最常用的方法便是TranslateAnimation构造方法。
【基本语法】public TranslateAnimation (float fromXDelta, float toXDelta, float fromYDelta, float toYDelta)
参数说明
fromXDelta:位置变化的起始点X坐标。
toXDelta:位置变化的结束点X坐标。
fromYDelta:位置变化的起始点Y坐标。
toYDelta:位置变化的结束点Y坐标。
注意:特别是动画连续切换的时候,初始坐标很关键,后续动画都以初始坐标为参考系切记,坐标原点很关键
多个类似tab 切换的位置效果,本来可以封装成radio 选择的性质,使用起来会更方便
private void InitWidth() {
ivBottomLine = (ImageView) findViewById(R.id.iv_bottom_line);
bottomLineWidth = ivBottomLine.getLayoutParams().width;
DisplayMetrics dm = new DisplayMetrics();
getWindowManager().getDefaultDisplay().getMetrics(dm);
int screenW = dm.widthPixels;
position_one = (int) (screenW / 3.0);
position_two = position_one * 2;
position_three = position_one * 3;
// 获取屏幕宽度,
Display displaey = StaticMethod.getDisplay(this);
int width = (displaey.getWidth() / 3);
LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(
width, LayoutParams.FILL_PARENT);
int kk = width*2;
switch (type) {
case FROM_LAUNCHER_GONGBIAO_SEARCH:
// mPager.setCurrentItem(1);
layoutParams.setMargins(0, 0, 0, 0);
break;
case FROM_LAUNCHER_GONGLIST_SEARCH:
// mPager.setCurrentItem(1);
layoutParams.setMargins(kk, 0, 0, 0);
break;
default:
layoutParams.setMargins(width, 0, 0, 0);
break;
}
// layoutParams.setMargins(width, 0, 0, 0);
ivBottomLine.setLayoutParams(layoutParams);
// String model = Build.MODEL;
// String mmodel = null;
// try {
// mmodel = URLEncoder.encode(model, "utf-8");
// } catch (UnsupportedEncodingException e) {
// e.printStackTrace();
// } catch (Exception e) {
// }
// if (mmodel != null && mmodel.equals("GT-N5100")) {
// LinearLayout.LayoutParams txt_params1 = new
// LinearLayout.LayoutParams(190,36);
// txt_params1.setMargins(106, 0, 0, 0);
// ivBottomLine.setLayoutParams(txt_params1);
// }
}
坐标处理
public class MyOnPageChangeListener implements OnPageChangeListener {
@Override
public void onPageSelected(int arg0) {
Animation animation = null;
Display displaey = A_StaticMethod
.getDisplay(BaiduSearchActivity.this);
int width = displaey.getWidth() / 3;
switch (arg0) {
case 0:
if (currIndex == 1) {
switch (type) {
case FROM_LAUNCHER_GONGBIAO_SEARCH:
animation = new TranslateAnimation(position_one,
0, 0, 0);
break;
case FROM_LAUNCHER_GONGLIST_SEARCH:
Log.i("xx", "k===dsds=====currIndex========"+currIndex);
animation = new TranslateAnimation(-position_one, -position_two, 0, 0);
break;
default:
animation = new TranslateAnimation(position_one,
-width, 0, 0);
break;
}
tvTabHotKey
.setTextColor(resources
.getColor(R.color.m_bd_baidu_search_tab_normal_textc));
} else if (currIndex == 2) {
switch (type) {
case FROM_LAUNCHER_GONGBIAO_SEARCH:
animation = new TranslateAnimation(position_two,
0, 0, 0);
break;
case FROM_LAUNCHER_GONGLIST_SEARCH:
animation = new TranslateAnimation(0,
-position_two, 0, 0);
break;
default:
animation = new TranslateAnimation(position_two, -width, 0,
0);
break;
}
tvTabApp.setTextColor(resources
.getColor(R.color.m_bd_baidu_search_tab_normal_textc));
}
tvTabLocal.setTextColor(resources
.getColor(R.color.m_bd_baidu_search_tab_select_textc));
boolean conStatus = StaticMethod
.getNetworkConnectionStatus(BaiduSearchActivity.this);
String completeText = editText.getText().toString();
completeText = A_StaticMethod.StringFilterByRegEx(completeText);
try {
if (localFragment != null) {
((M_bd_SearchLocalFragment) localFragment)
.initData(completeText);
MobclickAgent
.onEvent(BaiduSearchActivity.this,
"BaiduSearchActivity_widget_localsearch_app_420");// 百度搜索本地搜索应用页点击搜索的次数
}
} catch (Exception e) {
}
downManager.setVisibility(View.GONE);
downloadNotInstallLayout.setVisibility(View.GONE);
titleIcon.setImageDrawable(getResources().getDrawable(
R.drawable.m_bd_baidu_search_page2icon_bg));
editText.setHint(getResources().getString(
R.string.a_appstore_search_hint));
closeIme();
break;
case 1:
if (currIndex == 0) {
switch (type) {
case FROM_LAUNCHER_GONGBIAO_SEARCH:
animation = new TranslateAnimation(0, position_one, 0,
0);
break;
case FROM_LAUNCHER_GONGLIST_SEARCH:
animation = new TranslateAnimation(-position_two,
-position_one, 0, 0);
break;
default:
animation = new TranslateAnimation(0, position_one
- width, 0, 0);
break;
}
tvTabLocal
.setTextColor(resources
.getColor(R.color.m_bd_baidu_search_tab_normal_textc));
} else if (currIndex == 2) {
switch (type) {
case FROM_LAUNCHER_GONGBIAO_SEARCH:
animation = new TranslateAnimation(position_two,
position_one, 0, 0);
break;
case FROM_LAUNCHER_GONGLIST_SEARCH:
animation = new TranslateAnimation(0,
-position_one, 0, 0);
break;
default:
animation = new TranslateAnimation(position_two,
position_one - width, 0, 0);
break;
}
tvTabApp.setTextColor(resources
.getColor(R.color.m_bd_baidu_search_tab_normal_textc));
}
tvTabHotKey.setTextColor(resources
.getColor(R.color.m_bd_baidu_search_tab_select_textc));
conStatus = StaticMethod
.getNetworkConnectionStatus(BaiduSearchActivity.this);
if (conStatus) {
completeText = editText.getText().toString();
completeText = A_StaticMethod
.StringFilterByRegEx(completeText);
if (appsFragment != null) {
((M_bd_BaiduHotTagFragment) appsFragment)
.initHint(completeText);
}
} else {
Toast.makeText(BaiduSearchActivity.this,
getString(R.string.M_bd_net_set),
Toast.LENGTH_SHORT).show();
}
downManager.setVisibility(View.GONE);
downloadNotInstallLayout.setVisibility(View.GONE);
titleIcon.setImageDrawable(getResources().getDrawable(
R.drawable.widget_baidu_activity_logo));
editText.setHint(getResources().getString(
R.string.baidusb_widget_search_hint));
closeIme();
break;
case 2:
if (currIndex == 0) {
Log.i("xx", "k========currIndex========"+currIndex);
switch (type) {
case FROM_LAUNCHER_GONGBIAO_SEARCH:
animation = new TranslateAnimation(0,
position_two, 0, 0);
break;
case FROM_LAUNCHER_GONGLIST_SEARCH:
animation = new TranslateAnimation(0,
0, 0, 0);
break;
default:
animation = new TranslateAnimation(0, position_two - width,
0, 0);
break;
}
tvTabLocal
.setTextColor(resources
.getColor(R.color.m_bd_baidu_search_tab_normal_textc));
} else if (currIndex == 1) {
Log.i("xx", "m========currIndex========"+currIndex);
switch (type) {
case FROM_LAUNCHER_GONGBIAO_SEARCH:
animation = new TranslateAnimation(position_one,
position_two, 0, 0);
break;
case FROM_LAUNCHER_GONGLIST_SEARCH:
animation = new TranslateAnimation(-position_one,
0, 0, 0);
break;
default:
animation = new TranslateAnimation(position_one,
position_two - width, 0, 0);
break;
}
tvTabHotKey
.setTextColor(resources
.getColor(R.color.m_bd_baidu_search_tab_normal_textc));
}
TranslateAnimation类:位置变化动画类 (类似tab切换效果)