- Android 基礎
- Android - 首頁
- Android - 概覽
- Android - 環境設定
- Android - 架構
- Android - 應用程式元件
- Android - Hello World 示例
- Android - 資源
- Android - 活動
- Android - 服務
- Android - 廣播接收器
- Android - 內容提供程式
- Android - 碎片
- Android - 意圖/過濾器
- Android - 使用者介面
- Android - UI 佈局
- Android - UI 控制元件
- Android - 事件處理
- Android - 樣式和主題
- Android - 自定義元件
- Android 高階概念
- Android - 拖放
- Android - 通知
- 基於位置的服務
- Android - 傳送電子郵件
- Android - 傳送簡訊
- Android - 電話呼叫
- 釋出 Android 應用程式
- Android 有用示例
- Android - 警報對話方塊
- Android - 動畫
- Android - 音訊捕獲
- Android - AudioManager
- Android - 自動完成
- Android - 最佳實踐
- Android - 藍牙
- Android - 相機
- Android - 剪貼簿
- Android - 自定義字型
- Android - 資料備份
- Android - 開發人員工具
- Android - 模擬器
- Android - Facebook 整合
- Android - 手勢
- Android - Google 地圖
- Android - 影像效果
- Android - ImageSwitcher
- Android - 內部儲存
- Android - JetPlayer
- Android - JSON 解析器
- Android - Linkedin 整合
- Android - 載入微調器
- Android - 本地化
- Android - 登入螢幕
- Android - MediaPlayer
- Android - 多點觸控
- Android - 導航
- Android - 網路連線
- Android - NFC 指南
- Android - PHP/MySQL
- Android - 進度圓圈
- Android - 進度條
- Android - 推送通知
- Android - RenderScript
- Android - RSS 閱讀器
- Android - 螢幕錄製
- Android - SDK 管理器
- Android - 感測器
- Android - 會話管理
- Android - 共享首選項
- Android - SIP 協議
- Android - 拼寫檢查器
- Android - SQLite 資料庫
- Android - 支援庫
- Android - 測試
- Android - 文字轉語音
- Android - TextureView
- Android - Twitter 整合
- Android - UI 設計
- Android - UI 模式
- Android - UI 測試
- Android - WebView 佈局
- Android - Wi-Fi
- Android - 小部件
- Android - XML 解析器
- Android 有用資源
- Android - 問答
- Android - 有用資源
- Android - 討論
Android - 圖片切換器
有時您不希望影像突然出現在螢幕上,而是希望在影像從一個影像過渡到另一個影像時對影像應用某種動畫。Android 透過 ImageSwitcher 的形式支援此功能。
影像切換器允許您透過影像出現在螢幕上的方式在其上新增一些過渡。為了使用影像切換器,您需要首先定義其 XML 元件。其語法如下所示:
<ImageSwitcher android:id="@+id/imageSwitcher1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_centerHorizontal="true" android:layout_centerVertical="true" > </ImageSwitcher>
現在我們在 Java 檔案中建立 ImageSwithcer 的例項並獲取此 XML 元件的引用。其語法如下所示:
private ImageSwitcher imageSwitcher; imageSwitcher = (ImageSwitcher)findViewById(R.id.imageSwitcher1);
接下來我們需要做的是實現 ViewFactory 介面並實現未實現的方法,該方法返回一個 imageView。其語法如下所示:
imageSwitcher.setImageResource(R.drawable.ic_launcher);
imageSwitcher.setFactory(new ViewFactory() {
public View makeView() {
ImageView myView = new ImageView(getApplicationContext());
return myView;
}
}
您需要做的最後一件事是向 ImageSwitcher 新增動畫。您需要透過 AnimationUtilities 類呼叫靜態方法 loadAnimation 來定義 Animation 類的物件。其語法如下所示:
Animation in = AnimationUtils.loadAnimation(this,android.R.anim.slide_in_left); imageSwitcher.setInAnimation(in); imageSwitcher.setOutAnimation(out);
setInAnimaton 方法設定物件出現在螢幕上的動畫,而 setOutAnimation 方法則執行相反的操作。loadAnimation() 方法建立一個動畫物件。
除了這些方法之外,ImageSwitcher 類中還定義了其他方法。它們定義如下:
| 序號 | 方法和描述 |
|---|---|
| 1 |
setImageDrawable(Drawable drawable) 使用影像切換器設定影像。影像以點陣圖形式傳遞 |
| 2 |
setImageResource(int resid) 使用影像切換器設定影像。影像以整數 ID 形式傳遞 |
| 3 |
setImageURI(Uri uri) 使用影像切換器設定影像。影像以 URI 形式傳遞 |
| 4 |
ImageSwitcher(Context context, AttributeSet attrs) 返回一個影像切換器物件,並已設定方法中傳遞的一些屬性 |
| 5 |
onInitializeAccessibilityEvent (AccessibilityEvent event) 使用有關此 View 的資訊(即事件源)初始化 AccessibilityEvent |
| 6 |
onInitializeAccessibilityNodeInfo (AccessibilityNodeInfo info) 使用有關此 View 的資訊初始化 AccessibilityNodeInfo |
示例
以下示例演示了點陣圖上的一些影像切換器效果。它建立了一個基本的應用程式,允許您檢視影像上的動畫效果。
要試驗此示例,您需要在實際裝置上執行它。
| 步驟 | 描述 |
|---|---|
| 1 | 您將使用 Android Studio IDE 在 com.example.sairamkrishna.myapplication 包下建立一個 Android 應用程式。 |
| 2 | 修改 src/MainActivity.java 檔案以新增必要的程式碼。 |
| 3 | 修改 res/layout/activity_main 以新增相應的 XML 元件 |
| 4 | 執行應用程式並選擇正在執行的 Android 裝置,並在其上安裝應用程式並驗證結果 |
以下是修改後的主活動檔案 src/MainActivity.java 的內容。
在以下程式碼中,tp 和 abc 表示 tutorialspoint.com 的徽標
package com.example.sairamkrishna.myapplication;
import android.app.Activity;
import android.app.ActionBar.LayoutParams;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.ImageSwitcher;
import android.widget.ImageView;
import android.widget.Toast;
import android.widget.ViewSwitcher.ViewFactory;
public class MainActivity extends Activity {
private ImageSwitcher sw;
private Button b1,b2;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
b1 = (Button) findViewById(R.id.button);
b2 = (Button) findViewById(R.id.button2);
sw = (ImageSwitcher) findViewById(R.id.imageSwitcher);
sw.setFactory(new ViewFactory() {
@Override
public View makeView() {
ImageView myView = new ImageView(getApplicationContext());
myView.setScaleType(ImageView.ScaleType.FIT_CENTER);
myView.setLayoutParams(new
ImageSwitcher.LayoutParams(LayoutParams.WRAP_CONTENT,
LayoutParams.WRAP_CONTENT));
return myView;
}
});
b1.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Toast.makeText(getApplicationContext(), "previous Image",
Toast.LENGTH_LONG).show();
sw.setImageResource(R.drawable.abc);
}
});
b2.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Toast.makeText(getApplicationContext(), "Next Image",
Toast.LENGTH_LONG).show();
sw.setImageResource(R.drawable.tp);
}
});
}
}
以下是 xml res/layout/activity_main.xml 的修改後的內容。
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
android:paddingBottom="@dimen/activity_vertical_margin"
tools:context=".MainActivity">
<TextView android:text="Gestures Example"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/textview"
android:textSize="35dp"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Tutorials point"
android:id="@+id/textView"
android:layout_below="@+id/textview"
android:layout_centerHorizontal="true"
android:textColor="#ff7aff24"
android:textSize="35dp" />
<ImageSwitcher
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/imageSwitcher"
android:layout_below="@+id/textView"
android:layout_centerHorizontal="true"
android:layout_marginTop="168dp" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/left"
android:id="@+id/button"
android:layout_below="@+id/textView"
android:layout_centerHorizontal="true" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/right"
android:id="@+id/button2"
android:layout_alignParentBottom="true"
android:layout_alignLeft="@+id/button"
android:layout_alignStart="@+id/button" />
</RelativeLayout>
以下是 Strings.xml 檔案的內容。
<resources>
<string name="app_name">My Application</string>
<string name="left"><![CDATA[<]]></string>
<string name="right"><![CDATA[>]]></string>
</resources>
以下是 AndroidManifest.xml 檔案的內容。
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.sairamkrishna.myapplication"
<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name="com.example.sairamkrishna.myapplication.MainActivity"
android:label="@string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
讓我們嘗試執行我們剛剛修改的應用程式。我假設您在進行環境設定時建立了 AVD。要從 Android Studio 執行應用程式,請開啟專案的一個活動檔案並單擊工具欄中的執行
圖示。Android Studio 將應用程式安裝到您的 AVD 並啟動它,如果您的設定和應用程式一切正常,它將顯示以下模擬器視窗:
現在,如果您檢視裝置螢幕,您將看到兩個按鈕。
現在只需選擇右箭頭那個上面的按鈕。影像將從右側出現並向左移動。如下所示:
現在點選下面的按鈕,它將以一些過渡效果顯示上一張圖片。如下所示:
