Java tutorial
/* * Flan.Zeng 2011-2016 http://git.oschina.net/signup?inviter=flan * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ package com.flan.stock.view; import com.flan.stock.R; import android.content.Context; import android.content.res.TypedArray; import android.support.v4.view.ViewPager; import android.util.AttributeSet; import android.util.DisplayMetrics; import android.widget.ImageView; import android.widget.RelativeLayout; import android.widget.TextView; import android.view.View; import android.view.View.OnClickListener; import android.view.animation.Animation; import android.view.animation.TranslateAnimation; /** * viewpager Tab * @author flan * @date 20151028 */ public class AnimationTabWidget extends RelativeLayout implements OnClickListener { //private Context context; private ViewPager viewPager; private ImageView img_cursor; private TextView tv_tag1, tv_tag2, tv_tag3, tv_tag4, tv_tag5; private int whiteColor = getResources().getColor(R.color.menu_gray); private int redColor = getResources().getColor(R.color.menu_red); private int currentTab = 0; private int imgWidth; public AnimationTabWidget(Context context) { super(context, null); } public AnimationTabWidget(Context context, AttributeSet attrs) { super(context, attrs); //this.context = context; TypedArray typedArray = context.obtainStyledAttributes(attrs, R.styleable.animationTabWidget); currentTab = typedArray.getInt(R.styleable.animationTabWidget_current_index, 0); typedArray.recycle(); inflate(context, R.layout.layout_animation_tab_widget, this); img_cursor = (ImageView) this.findViewById(R.id.tag_img_cursor); tv_tag1 = (TextView) this.findViewById(R.id.tv_tag1); tv_tag2 = (TextView) this.findViewById(R.id.tv_tag2); tv_tag3 = (TextView) this.findViewById(R.id.tv_tag3); tv_tag4 = (TextView) this.findViewById(R.id.tv_tag4); tv_tag5 = (TextView) this.findViewById(R.id.tv_tag5); tv_tag1.setOnClickListener(this); tv_tag2.setOnClickListener(this); tv_tag3.setOnClickListener(this); tv_tag4.setOnClickListener(this); tv_tag5.setOnClickListener(this); initDefaultIndex(); } /** * ? ? */ private void initDefaultIndex() { //?? DisplayMetrics dm = new DisplayMetrics(); dm = getResources().getDisplayMetrics(); int screenW = dm.widthPixels; imgWidth = screenW / 5; LayoutParams para = (LayoutParams) img_cursor.getLayoutParams(); para.width = imgWidth; img_cursor.setLayoutParams(para); setCurrentTab(currentTab); } @Override public void onClick(View v) { switch (v.getId()) { case R.id.tv_tag1: currentTab = 0; break; case R.id.tv_tag2: currentTab = 1; break; case R.id.tv_tag3: currentTab = 2; break; case R.id.tv_tag4: currentTab = 3; break; case R.id.tv_tag5: currentTab = 4; break; } if (viewPager != null) { viewPager.setCurrentItem(currentTab); } } public void setViewPager(ViewPager viewPager) { this.viewPager = viewPager; } /** * ? ? * @param position */ public void setCurrentTab(int position) { tv_tag1.setTextSize(12); tv_tag2.setTextSize(12); tv_tag3.setTextSize(12); tv_tag4.setTextSize(12); tv_tag5.setTextSize(12); tv_tag1.setTextColor(whiteColor); tv_tag2.setTextColor(whiteColor); tv_tag3.setTextColor(whiteColor); tv_tag4.setTextColor(whiteColor); tv_tag5.setTextColor(whiteColor); switch (position) { case 0: tv_tag1.setTextColor(redColor); tv_tag1.setTextSize(16); break; case 1: tv_tag2.setTextColor(redColor); tv_tag2.setTextSize(16); break; case 2: tv_tag3.setTextColor(redColor); tv_tag3.setTextSize(16); break; case 3: tv_tag4.setTextColor(redColor); tv_tag4.setTextSize(16); break; case 4: tv_tag5.setTextColor(redColor); tv_tag5.setTextSize(16); break; } Animation animation = null; animation = new TranslateAnimation(imgWidth * currentTab, imgWidth * position, 0, 0); animation.setFillAfter(true);//True:??? animation.setDuration(300); //? img_cursor.startAnimation(animation); currentTab = position; } }