Android TranslateAnimation top segment movement animation,

Source: Internet
Author: User
Tags getcolor

Android TranslateAnimation top segment movement animation,

Here, the function is to jump from the fragment click in the homepage layout to an acitivity, and then at the top is a switching segment, and at the bottom is a listview. Click segment to let the listview load different content. Viewpager is no longer used here. You can also use viewpager + listview. I am using a custom viewpager. Next I will talk about how to switch the animated class by using TranslateAnimation segment. There are a lot of things, which I originally developed for android. This class implements many bugs in animation effects, the effect bug directly indicates that the android animation class is not as good as ios. The effect bugs I encounter mainly occur in the calculation of the control moving distance and moving time. For example, if a mobile animation has a buffer or two or more parts, there is no animation effect.

 

The layout will be posted below, mainly

<? Xml version = "1.0" encoding = "UTF-8"?> <LinearLayout xmlns: android = "http://schemas.android.com/apk/res/android" android: layout_width = "match_parent" android: layout_height = "match_parent" xmlns: app = "http://schemas.android.com/apk/res-auto" android: background = "@ color/white_color" android: orientation = "vertical"> <TextView android: layout_width = "match_parent" android: layout_height = "@ dimen/navigationbar_height" android: textSize = "@ dimen/title_fontsize" android: text = "online videos" android: textColor = "@ color/navigation_title_color" android: background = "@ color/navigationbar_backround_color" android: gravity = "center"/> <LinearLayout android: layout_width = "match_parent" android: layout_height = "53dp" android: orientation = "vertical" android: animateLayoutChanges = "true"> <LinearLayout android: layout_width = "match_parent" android: layout_height = "50dp" android: orientation = "horizontal"> <TextView android: layout_width = "match_parent" android: layout_height = "@ dimen/navigationbar_height" android: layout_weight = "1" android: textSize = "@ dimen/title_fontsize" android: text = "recommended" android: id = "@ + id/tuijianVideo" android: textColor = "@ color/hot_price_color" android: background = "@ color/navigationbar_backround_color" android: gravity = "center"/> <TextView android: layout_width = "match_parent" android: layout_height = "@ dimen/navigationbar_height" android: textSize = "@ dimen/title_fontsize" android: layout_weight = "1" android: text = "movie" android: id = "@ + id/dianying" android: textColor = "@ color/navigation_title_color" android: background = "@ color/navigationbar_backround_color" android: gravity = "center"/> <TextView android: layout_width = "match_parent" android: layout_height = "@ dimen/navigationbar_height" android: textSize = "@ dimen/title_fontsize" android: text = "TV" android: id = "@ + id/dianshi" android: layout_weight = "1" android: textColor = "@ color/navigation_title_color" android: background = "@ color/navigationbar_backround_color" android: gravity = "center"/> <TextView android: layout_width = "match_parent" android: layout_height = "@ dimen/navigationbar_height" android: textSize = "@ dimen/title_fontsize" android: text = "Anime" android: layout_weight = "1" android: id = "@ + id/dongman" android: textColor = "@ color/navigation_title_color" android: background = "@ color/navigationbar_backround_color" android: gravity = "center"/> <TextView android: layout_width = "match_parent" android: layout_height = "@ dimen/navigationbar_height" android: textSize = "@ dimen/title_fontsize" android: text = "" android: id = "@ + id/zongyi" android: layout_weight = "1" android: textColor = "@ color/navigation_title_color" android: background = "@ color/navigationbar_backround_color" android: gravity = "center"/> </LinearLayout> <ImageView android: layout_width = "match_parent" android: id = "@ + id/Imagezhishiqi" android: layout_height = "3dp" android: background = "@ color/hot_price_color"/> </LinearLayout> <com. lt. wbtaobake. custom. xPullRefresh. XListView android: id = "@ + id/xListView" android: layout_width = "fill_parent" android: layout_height = "fill_parent" android: cacheColorHint = "#00000000"> </com. lt. wbtaobake. custom. xPullRefresh. XListView> </LinearLayout>
View Code

Paste the following code and watch it in detail with an animation description.

Public class HomeFragment_VideoActivity extends Activity implements View. onClickListener {private ListViewAdapter listViewAdapter; private ViewPagerAdapter viewPagerAdapter; private Context context; private LinearLayout. layoutParams zhishiqilinearParams; private int zhishiqiWidth; private ImageView zhishiqi; private TextView tuijianView; private TextView dianying; private TextView dianshi; private TextView dongman; private TextView zongyi; private int currentTopItemIndex; private TranslateAnimation moveAnimation; private int moveStepValue; // video data private XListView; private JSONArray CurrentVideoDataArray; private int currentVideoPageIndex; private String refreshTime = "first refresh"; private ViewGroup. marginLayoutParams margin; @ Override protected void onCreate (final Bundle savedInstance State) {super. onCreate (savedInstanceState); context = getApplicationContext (); setContentView (R. layout. home_video_activity); // indicator zhishiqi = (ImageView) findViewById (R. id. imagezhishiqi); currentTopItemIndex = 1; DisplayMetrics dm = new DisplayMetrics (); getWindowManager (). getdefadisplay display (). getMetrics (dm); int width = dm. widthPixels; // because our segments are fixed to 5 segments, we first use the cell phone screen width/5 to calculate the width of the segment indicator zhishiqiWidth = wi Dth/5; // because the width of the indicator cannot be accurately set in a fixed layout, you need to use the calculated width to reset the width of the indicator zhishiqilinearParams = (LinearLayout. layoutParams) zhishiqi. getLayoutParams (); zhishiqilinearParams. width = zhishiqiWidth; zhishiqi. setLayoutParams (zhishiqilinearParams); // The following is the textview control that displays text in each segment: tuijianView = (TextView) findViewById (R. id. tuijianVideo); dianying = (TextView) findViewById (R. id. dianying); dianshi = (TextView) findViewById (R. Id. dianshi); dongman = (TextView) findViewById (R. id. dongman); zongyi = (TextView) findViewById (R. id. zongyi); tuijianView. setTag (1); dianying. setTag (2); dianshi. setTag (3); dongman. setTag (4); zongyi. setTag (5); tuijianView. setOnClickListener (this); dianying. setOnClickListener (this); dianshi. setOnClickListener (this); dongman. setOnClickListener (this); zongyi. setOnClickListener (this);} // textview point of each segment Click Event @ Override public void onClick (View v) {int tag = (Integer) v. getTag (); // get the number of segments to move the control to determine the direction of movement. If it is a positive number, it indicates moving to the right, and the negative number moves to the left. moveStepValue = tag-currentTopItemIndex; switch (tag) {case 1: if (currentTopItemIndex = 1) {return;} else {currentTopItemIndex = tag; tuijianView. setTextColor (this. getResources (). getColor (R. color. hot_price_color); dianying. setTextColor (this. getResources (). getColor (R. color. white _ Color); dianshi. setTextColor (this. getResources (). getColor (R. color. white_color); dongman. setTextColor (this. getResources (). getColor (R. color. white_color); zongyi. setTextColor (this. getResources (). getColor (R. color. white_color);} break; case 2: if (currentTopItemIndex = 2) {return;} else {currentTopItemIndex = tag; tuijianView. setTextColor (this. getResources (). getColor (R. color. white_color); dia Nying. setTextColor (this. getResources (). getColor (R. color. hot_price_color); dianshi. setTextColor (this. getResources (). getColor (R. color. white_color); dongman. setTextColor (this. getResources (). getColor (R. color. white_color); zongyi. setTextColor (this. getResources (). getColor (R. color. white_color);} break; case 3: if (currentTopItemIndex = 3) {return;} else {currentTopItemIndex = tag; tuijianView. se TTextColor (this. getResources (). getColor (R. color. white_color); dianying. setTextColor (this. getResources (). getColor (R. color. white_color); dianshi. setTextColor (this. getResources (). getColor (R. color. hot_price_color); dongman. setTextColor (this. getResources (). getColor (R. color. white_color); zongyi. setTextColor (this. getResources (). getColor (R. color. white_color);} break; case 4: if (currentTopItemIndex = 4) {return;} else {currentTopItemIndex = tag; tuijianView. setTextColor (this. getResources (). getColor (R. color. white_color); dianying. setTextColor (this. getResources (). getColor (R. color. white_color); dianshi. setTextColor (this. getResources (). getColor (R. color. white_color); dongman. setTextColor (this. getResources (). getColor (R. color. hot_price_color); zongyi. setTextColor (this. getResources (). getColo R (R. color. white_color);} break; default: if (currentTopItemIndex = 5) {return;} else {currentTopItemIndex = tag; tuijianView. setTextColor (this. getResources (). getColor (R. color. white_color); dianying. setTextColor (this. getResources (). getColor (R. color. white_color); dianshi. setTextColor (this. getResources (). getColor (R. color. white_color); dongman. setTextColor (this. getResources (). getColor (R. color. White_color); zongyi. setTextColor (this. getResources (). getColor (R. color. hot_price_color);} break;} margin = new ViewGroup. marginLayoutParams (tuijianView. getLayoutParams (); // Animation. RELATIVE_TO_SELF, 0.0f, these two are actually one parameter (combined to see, indicating the relative positioning + coordinate change value), so only the four parameters 2/4/6/8 // the first value: 0.0f indicates that the original x coordinate of the control remains unchanged. // The second value moveStepValue * 0.5f indicates the value changed under the original x coordinate. // The third value 0.0f indicates the y coordinate change. // The fourth value 0.0f indicates the change under the condition of the original y coordinate. // key: here, because we only move the x coordinate left and right, the value of the Y axis does not change to 0.0f 0.0f. // when moveStepValue is negative, the moveStepValue * 0.5f segment moves to the left and the positive number moves to the right, in this way, there will be no bug in the Animation effect, moveAnimation = new TranslateAnimation (Animation. RELATIVE_TO_SELF, 0.0f, Animation. RELATIVE_TO_SELF, moveStepValue * 0.5f, Animation. RELATIVE_TO_SELF, 0.0f, Animation. RELATIVE_TO_SELF, 0.0f); // the following code indicates Calculate the animation execution time. If no calculation is performed, the number of switching parts is inconsistent. The animation execution speed of the indicator is too fast or too slow. If (moveStepValue <1) {moveStepValue = moveStepValue *-1;} moveAnimation. setDuration (moveStepValue * 70); moveAnimation. setAnimationListener (new Animation. animationListener () {@ Override public void onAnimationStart (Animation animation) {}@ Override public void Merge (Animation animation) {zhishiqi. clearAnimation (); // shift After the animation effect is implemented, you also need to set the margin of the mobile segment once. If no animation is set, it will be reversed. Why do I think it is because the xhtml layout is used with android. ZhishiqilinearParams. setMargins (currentTopItemIndex-1) * zhishiqiWidth, margin. topMargin, zhishiqiWidth, zhishiqilinearParams. height); zhishiqi. setLayoutParams (zhishiqilinearParams) ;}}); zhishiqi. startAnimation (moveAnimation); zhishiqi. setVisibility (View. VISIBLE );}}

 

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.