編輯:關於 Android 程式設計
上一節我們用 Gallery 搭配 ImageView 來展示圖片。但有時候,直接讓一張圖「啪」地出現在 ImageView 裡,視覺上顯得有點突兀。如果你希望切換圖片時能有一點過場動畫,這時候就該請出 ImageSwitcher 了。它天生就是為「帶動畫的圖片切換」而設計的。
把 Gallery 與 ImageSwitcher 一起放進版面:Gallery 負責陳列縮圖,ImageSwitcher 則負責大圖的切換顯示。
<LinearLayout xmlns:android="https://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<Gallery
android:id="@+id/gallery1"
android:layout_width="fill_parent"
android:layout_height="wrap_content" />
<ImageSwitcher
android:id="@+id/switcher1"
android:layout_width="fill_parent"
android:layout_height="fill_parent" />
</LinearLayout>
另外別忘了在 res/values 下新增一個 attrs.xml,宣告 Gallery 的樣式屬性:
<?xml version="1.0" encoding="utf-8"?>
<resources>
<declare-styleable name="Gallery1">
<attr name="android:galleryItemBackground" />
</declare-styleable>
</resources>
使用 ImageSwitcher 最重要的一步,是讓 Activity 實作 ViewFactory 介面。這個介面為 ImageSwitcher 產生要顯示的 view,我們只需實作 makeView(),回傳一個設定好外觀的 ImageView:
public class ImageSwitcherActivity extends Activity implements ViewFactory {
Integer[] imageIDs = {
R.drawable.pic1, R.drawable.pic2, R.drawable.pic3,
R.drawable.pic4, R.drawable.pic5, R.drawable.pic6, R.drawable.pic7
};
private ImageSwitcher imageSwitcher;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
imageSwitcher = (ImageSwitcher) findViewById(R.id.switcher1);
imageSwitcher.setFactory(this);
// 設定左右滑動的切換動畫
imageSwitcher.setInAnimation(AnimationUtils.loadAnimation(
this, android.R.anim.slide_in_left));
imageSwitcher.setOutAnimation(AnimationUtils.loadAnimation(
this, android.R.anim.slide_out_right));
Gallery gallery = (Gallery) findViewById(R.id.gallery1);
gallery.setAdapter(new ImageAdapter(this));
gallery.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View v,
int position, long id) {
imageSwitcher.setImageResource(imageIDs[position]);
}
});
}
public View makeView() {
ImageView imageView = new ImageView(this);
imageView.setBackgroundColor(0xFF000000);
imageView.setScaleType(ImageView.ScaleType.FIT_CENTER);
imageView.setLayoutParams(new ImageSwitcher.LayoutParams(
LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT));
return imageView;
}
}
ImageSwitcher 的動畫透過 setInAnimation 與 setOutAnimation 設定。除了本例使用的左右滑動(slide_in_left / slide_out_right),系統也提供了淡入淡出效果可以替換:
imageSwitcher.setInAnimation(AnimationUtils.loadAnimation(
this, android.R.anim.fade_in));
imageSwitcher.setOutAnimation(AnimationUtils.loadAnimation(
this, android.R.anim.fade_out));
至於 ImageAdapter 的實作,則與上一節 Gallery 的範例幾乎相同,負責把 Gallery 與圖片資源綁定。差別在於:Gallery 中的縮圖由 Adapter 產生,而大圖的顯示則交給 ImageSwitcher 透過 makeView() 生成的 view 來負責。
完成後按 F11 在模擬器上執行,點選 Gallery 中的縮圖,上方的大圖就會帶著滑動動畫切換呈現,過場效果自然順滑許多。
Android 多 Tab 頁籤切換與懸浮頭部實作解析
整體架構 這個實作主要使用 ViewPager 搭配多個 Fragment,每個 Fragment 內再放入可下拉刷新的 ListView。頁面上方另外覆蓋一層懸浮頭部與
榮耀Note8怎麼預約購買 時間、平台與規則整理
華為榮耀於8月1日下午正式發表了6.6吋大屏手機榮耀Note8。想入手的朋友大概最關心怎麼預約,下面小編就整理了完整的預約購買攻略。 華為榮耀Note8預約購買攻略 預
通訊錄原型實作(二):打造類似 QQ 的分組懸浮好友列表
在上一節通訊錄原型實作中,我們談到了最基礎、最簡單的通訊錄做法;這一節則要進一步挑戰類似 QQ 好友列表那種「分組名稱懸浮在最頂部」的效果。先來聊聊我的基本實作思路: 1
從零自學 Android 到月薪破萬:一個普通人的真實翻盤記錄
寫這篇文字,是想給兩種人看:一種是剛剛決定從零開始學 Android、卻遲遲不敢邁出第一步的人;另一種是已經上路、正在半途苦撐的人。我不敢說自己有什麼過人天賦,恰恰相反,