編輯:關於 Android 程式設計
先來看效果:選擇不同模組時,滑塊會以動畫形式平滑滑過去,這種效果特別適合新聞客戶端展示眾多欄目。
佈局這樣寫:
<RelativeLayout xmlns:android="https://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#ffffff" >
<include layout="@layout/title_layout" />
<RelativeLayout android:id="@+id/column_navi"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:background="@drawable/top_column_bg" >
<ImageButton android:id="@+id/column_to_left"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_centerVertical="true"
android:layout_marginLeft="5.0dp"
android:background="#00000000"
android:src="@drawable/arr_left"
android:visibility="visible" />
<ImageButton android:id="@+id/column_to_right"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_centerVertical="true"
android:layout_marginRight="5.0dp"
android:background="#00000000"
android:src="@drawable/arr_right"
android:visibility="visible" />
<HorizontalScrollView android:id="@+id/column_scrollview"
android:layout_width="fill_parent"
android:layout_height="35.0dp"
android:layout_toLeftOf="@+id/column_to_right"
android:layout_toRightOf="@+id/column_to_left"
android:fadingEdge="vertical"
android:scrollbars="none" >
<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" >
<ImageView android:id="@+id/column_slide_bar"
android:layout_width="49.0dp"
android:layout_height="29.0dp" />
<LinearLayout android:id="@+id/column_title_layout"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="horizontal"
android:layout_gravity="center_vertical" />
</FrameLayout>
</HorizontalScrollView>
</RelativeLayout>
</RelativeLayout>
其中的 column_slide_bar 就是那個會滑動的滑塊,而 column_title_layout 則用來容納所有子欄目。
在 string.xml 中加入欄目資料:
<string-array name="all_choice" translatable="false">
<item>科技</item>
<item>財經</item>
<item>體育</item>
<item>本地</item>
<item>最新</item>
<item>百家</item>
<item>娛樂</item>
</string-array>
動態建立各個欄目標籤:
private void initTab() {
String[] resource = this.getResources().getStringArray(R.array.all_choice);
for (int j = 0; j < resource.length; j++) {
array.add(resource[j]);
}
this.columnTitleLayout.removeAllViews();
int j = this.array.size();
if (j <= 5) {
this.scrollToRight.setVisibility(View.INVISIBLE);
this.scrollToLeft.setVisibility(View.INVISIBLE);
}
currTabIndex = 0;
animImage.setBackgroundResource(R.drawable.slidebar);
for (int i = 0; i < array.size(); i++) {
TextView ColumnTextView = new TextView(this);
ColumnTextView.setText(array.get(i));
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);
MoreColumnTextView.setText(getResources().getText(R.string.more_column));
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;
if (lastTabIndex != currTabIndex) {
if (currTabIndex == array.size()) {
return;
}
showAnimation();
}
}
動畫採用 TranslateAnimation,並設定 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;
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);
}
核心思路就是:以「欄目寬度 × 索引」算出滑塊的起點與終點 X 座標,再用 TranslateAnimation 平滑移動,同時切換文字顏色,就能呈現如新聞客戶端般流暢的欄目切換效果。
《Android Studio 實用指南》8.20 提煉接口
提煉接口(Extract Interface)是一項重構手法:從一個已存在的類中挑選若干方法,把它們提取到一個獨立的接口里,讓這個類去實現該接口。對於需要抽出統一契約、方
Android 百度地圖 SDK 接入教學
百度地圖 Android SDK 簡介此 SDK 可讓開發者在 Android 裝置中嵌入地圖、定位與搜尋能力。服務本身免費,但需先申請密鑰。申請密鑰進入開發平台,閱讀條
Android 粒子系統實作:焰火綻放與瀑布效果
焰火、瀑布、下雪、爆炸——這些特效背後都是同一套技術:粒子系統。它與單個物體的運動模擬有相似之處,都是靠數學公式推算軌跡;差別在於粒子系統關注的是「整體視覺氛圍」,而非單
手機QQ瀏覽器開啟廣告過濾,告別彈窗騷擾
網頁側邊欄的浮動廣告、底部彈窗、突然彈出的促銷浮層,確實很影響閱讀體驗。手機QQ瀏覽器內建了廣告過濾功能,幾步就能開啟。 1. 打開手機QQ瀏覽器,點擊底部工具列的【菜單