編輯:關於 Android 程式設計
系統預設的點擊回饋偏樸素,想讓介面更有手感,可以替 View 加上「放大 + 淡出」的過場動畫,動畫播完再執行原本的點擊邏輯。整個思路不複雜,但改動之後互動質感提升明顯。
底層依賴 NineOldAndroids(在 3.0 之前的系統上補齊 ViewPropertyAnimator 能力),一行就能同時驅動縮放與透明度:
animate(v).scaleX(factor).scaleY(factor).alpha(0);
把原本的點擊行為包成回呼,讓動畫與業務邏輯解耦:
public interface ClickAnimation {
public void onClick(View v);
}
動畫結束時把 View 的縮放與透明度還原成初始狀態,再觸發回呼。還原這一步很關鍵,否則第二次點擊時 View 已經是透明且放大的狀態:
private void animateClickView(final View v, final ClickAnimation callback) {
float factor = 2;
animate(v).scaleX(factor).scaleY(factor).alpha(0)
.setListener(new AnimatorListenerAdapter() {
@Override
public void onAnimationEnd(Animator animation) {
ViewHelper.setScaleX(v, 1);
ViewHelper.setScaleY(v, 1);
ViewHelper.setAlpha(v, 1);
if (callback != null) {
callback.onClick(v);
}
super.onAnimationEnd(animation);
}
});
}
有了這層封裝,要再往裡頭塞搖擺、震動、旋轉等效果都很方便,只要在 animate() 鏈式呼叫上追加屬性即可。
效果參考影片:https://v.youku.com/v_show/id_XNjYzOTI4NzMy.html
Activity 部分,把所有 View 的點擊都統一走 animateClickView:
package com.buptfarmer.devapp;
import static com.nineoldandroids.view.ViewPropertyAnimator.animate;
import com.nineoldandroids.animation.Animator;
import com.nineoldandroids.animation.AnimatorListenerAdapter;
import com.nineoldandroids.view.ViewHelper;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;
public class ClickAnimationExample extends Activity implements OnClickListener {
private Button aaaBtn, bbbBtn, cccBtn;
private ImageView bbbImg;
private TextView aaaText;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.click_animation_example);
initView();
}
private void initView() {
aaaBtn = (Button) findViewById(R.id.aaa_btn);
aaaBtn.setOnClickListener(this);
aaaText = (TextView) findViewById(R.id.aaa_text);
aaaText.setOnClickListener(this);
bbbBtn = (Button) findViewById(R.id.bbb_btn);
bbbBtn.setOnClickListener(this);
bbbImg = (ImageView) findViewById(R.id.bbb_img);
bbbImg.setOnClickListener(this);
}
private void animateClickView(final View v, final ClickAnimation callback) {
float factor = 2;
animate(v).scaleX(factor).scaleY(factor).alpha(0)
.setListener(new AnimatorListenerAdapter() {
@Override
public void onAnimationEnd(Animator animation) {
ViewHelper.setScaleX(v, 1);
ViewHelper.setScaleY(v, 1);
ViewHelper.setAlpha(v, 1);
if (callback != null) {
callback.onClick(v);
}
super.onAnimationEnd(animation);
}
});
}
@Override
public void onClick(View v) {
animateClickView(v, new ClickAnimation() {
@Override
public void onClick(View v) {
if (v == aaaBtn) {
Toast.makeText(getApplicationContext(), "aaaBtn has been clicked", Toast.LENGTH_SHORT).show();
} else if (v == aaaText) {
Toast.makeText(getApplicationContext(), "aaaText has been clicked", Toast.LENGTH_SHORT).show();
} else if (v == bbbBtn) {
Toast.makeText(getApplicationContext(), "bbbBtn has been clicked", Toast.LENGTH_SHORT).show();
} else if (v == bbbImg) {
Toast.makeText(getApplicationContext(), "bbbImg has been clicked", Toast.LENGTH_SHORT).show();
}
}
});
}
public interface ClickAnimation {
public void onClick(View v);
}
}
對應的布局,注意每個 View 都掛上監聽器,動畫才會生效:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="https://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin">
<ImageView
android:id="@+id/bbb_img"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:contentDescription="bbb_img"
android:src="@drawable/ic_launcher" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/hello_world" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<Button
android:id="@+id/aaa_btn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="aaa" />
<TextView
android:id="@+id/aaa_text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="aaa_text" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<Button
android:id="@+id/bbb_btn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="bbb" />
</LinearLayout>
<Button
android:id="@+id/ccc_btn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="ccc" />
</LinearLayout>
深入解析 Android 智能指針:sp 與 wp 的 RefBase 機制
閱讀 Android 源碼時,經常會看到 sp<xxx>、wp<xxx> 這類型別宣告,它們其實就是 Android 中的智能指針。智能指針是 C
Android 自定義 View 繪圖:用 Path 實作無縫拖影動畫
幾天前在「Android 繪圖之漸隱動畫」中,我用畫線的方式做出漸隱效果,但留了個問題:畫筆粗度大於 1 時,相鄰線段之間會出現裂隙。這次改用 Path 重寫,效果改善很
Android 系統開發(4)——Autotools 建置工具鏈完全解析
在 Android 系統與 Linux 環境開發中,難免會遇到需要自行編譯第三方 C/C++ 函式庫的情況,而這類專案大多採用 Autotools 作為建置系統。本文整理
Android Socket UDP 傳送實作
Android 可使用 DatagramSocket 與 DatagramPacket 進行 UDP 傳輸。按下按鈕後,將文字內容轉為位元組,設定目標位址與埠號,再送出封