Android教程網
  1. 首頁
  2. Android 技術
  3. Android 手機
  4. Android 系統教程
  5. Android 游戲
 Android教程網 >> Android技術 >> 關於Android編程 >> android Title滑塊動畫實現(適合新聞客戶端多種欄目的展示)

android Title滑塊動畫實現(適合新聞客戶端多種欄目的展示)

編輯:關於Android編程

先上效果圖,選擇不同的模塊,滑動會通過動畫形式滑過去,這種適合新聞客戶端多種欄目的展示:

這麼寫Layout:



    

    

        

        

        

            <frameLayout
                android:layout_width="fill_parent"
                android:layout_height="wrap_content"
                android:layout_gravity="center_vertical"
                android:paddingLeft="5.0dp"
                android:paddingRight="5.0dp"
                android:paddingTop="3.0dp" >

                
		//這個可以把子欄目都加到column_title_layout中
                
            </frameLayout>
        
    


代碼中在string.xml中加入數據:

    
        科技
        財經
        體育
        本地
        最新
        百家
        娛樂
    

	private void initTab() {
		String[] resource = this.getResources().getStringArray(R.array.all_choice);
		for (int j = 0; j < resource.length; j++) {
			String name = resource[j];
			array.add(name);
		}
		
		this.columnTitleLayout.removeAllViews();
		int j = this.array.size();
		if (j <= 5) {
			this.scrollToRight.setVisibility(View.INVISIBLE);
			this.scrollToLeft.setVisibility(View.INVISIBLE);

		}
		currTabIndex = 0;
		int i = 0;
		animImage.setBackgroundResource(R.drawable.slidebar);
		for (i = 0; i < array.size(); i++) {
			String str = array.get(i);
			TextView ColumnTextView = new TextView(this);
			ColumnTextView.setText(str);
			ColumnTextView.setTag(i);
			ColumnTextView.setPadding(18, 2, 15, 4);
			ColumnTextView.setOnClickListener(this);
			ColumnTextView.setTextAppearance(this, R.style.column_tx_style);
			LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT);
			columnTitleLayout.addView(ColumnTextView, params);
		}

		TextView MoreColumnTextView = new TextView(this);
		MoreColumnTextView.setTag(i);
		CharSequence localCharSequence = getResources().getText(R.string.more_column);
		MoreColumnTextView.setText(localCharSequence);
		MoreColumnTextView.setPadding(18, 2, 15, 4);
		MoreColumnTextView.setTextAppearance(this, R.style.column_tx_style);
		LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT);
		columnTitleLayout.addView(MoreColumnTextView, params);
		
	}


在點擊子欄目的時候啟動動畫:

@Override
	public void onClick(View v) {
		int k = (Integer)v.getTag();
		lastTabIndex = currTabIndex;
		currTabIndex = k;
		String text = ((TextView) v).getText().toString();
		if (lastTabIndex != currTabIndex) {


			if (currTabIndex == array.size()) {
				return;
			}
			showAnimation();


		}
	}
	


動畫采用TranslateAnimation animation.setFillAfter(true);

	private void showAnimation() {

		if (lastTabIndex == currTabIndex) {
			return;
		}
		((TextView) columnTitleLayout.getChildAt(lastTabIndex)).setTextColor(R.drawable.white);
		int widgetItemWidth = ((TextView) columnTitleLayout.getChildAt(lastTabIndex)).getWidth();
		int fromX = lastTabIndex * widgetItemWidth;
		int toX = currTabIndex * widgetItemWidth;
		Log.v("test", "widgetItemWidth" + widgetItemWidth + "fromX:" + fromX + " toX:" + toX);
		TranslateAnimation animation = new TranslateAnimation(fromX, toX, 0, 0);
		animation.setDuration(500);
		animation.setFillAfter(true);
		animation.setAnimationListener(new AnimationListener() {

			@Override
			public void onAnimationStart(Animation animation) {
				((TextView) columnTitleLayout.getChildAt(lastTabIndex)).setTextColor(MainActivity.this.getResources().getColor(R.drawable.gray2));
			}

			@Override
			public void onAnimationRepeat(Animation animation) {
			}

			@Override
			public void onAnimationEnd(Animation animation) {
				((TextView) columnTitleLayout.getChildAt(currTabIndex)).setTextColor(MainActivity.this.getResources().getColor(R.drawable.white));
				lastTabIndex = currTabIndex;
			}
		});
		animImage.startAnimation(animation);
	}

代碼可以在http://download.csdn.net/detail/baidu_nod/7576663下載

  1. 上一頁:
  2. 下一頁:
熱門文章
閱讀排行版
Copyright © Android教程網 All Rights Reserved