編輯:關於 Android 程式設計
先上效果圖:

這裡要實現的效果是:點擊上方按鈕後,讓一個 TextView 沿著拋物線軌跡隨機移動到下方某個按鈕的位置。
首先,把下方的按鈕放進一個 list 中,方便後續隨機取值:
list = new ArrayList<Button>(); list.add(btn1); list.add(btn2); list.add(btn3); list.add(btn4);
接著就是重點——點擊按鈕後的拋物線動畫。先宣告一個整型陣列來存放被點擊按鈕的螢幕 X、Y 座標(也就是動畫的起始座標),再 new 一個用來承載拋物線的控件(這裡用 TextView,也可以換成任何 View):
int[] start_location = new int[2]; v.getLocationInWindow(start_location); TextView te = new TextView(this); te.setText("啊");
然後把要移動的控件放進一個動畫層中:
ViewGroup rootView = (ViewGroup) this.getWindow().getDecorView(); LinearLayout animLayout = new LinearLayout(this); LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams( LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.MATCH_PARENT); animLayout.setLayoutParams(lp); animLayout.setId(Integer.MAX_VALUE); animLayout.setBackgroundResource(android.R.color.transparent); rootView.addView(animLayout);
接著寫拋物線動畫。拋物線其實就是兩個位移動畫的組合:一個橫向移動、一個縱向移動,兩者同時執行,就產生了拋物線的效果。
int endX = 0 - start_location[0] + list.get(ra.nextInt(list.size())).getLeft(); int endY = end_location[1] - start_location[1]; TranslateAnimation translateAnimationX = new TranslateAnimation(0, endX, 0, 0); translateAnimationX.setInterpolator(new LinearInterpolator()); translateAnimationX.setRepeatCount(0); translateAnimationX.setFillAfter(true); TranslateAnimation translateAnimationY = new TranslateAnimation(0, 0, 0, endY); translateAnimationY.setInterpolator(new AccelerateInterpolator()); translateAnimationY.setRepeatCount(0); AnimationSet set = new AnimationSet(false); set.setFillAfter(false); set.addAnimation(translateAnimationY); set.addAnimation(translateAnimationX); set.setDuration(800); view.startAnimation(set);
最後,為動畫加上監聽:動畫執行時把要移動的控件顯示出來,動畫結束後再將它隱藏。
完整代碼如下:
package com.example.movetest;
import java.util.ArrayList;
import java.util.List;
import java.util.Random;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.view.animation.AccelerateInterpolator;
import android.view.animation.Animation;
import android.view.animation.Animation.AnimationListener;
import android.view.animation.AnimationSet;
import android.view.animation.LinearInterpolator;
import android.view.animation.TranslateAnimation;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.TextView;
public class MainActivity extends Activity implements OnClickListener {
private List<Button> list = null;
private ViewGroup viewGroup;
private Button btn1, btn2, btn3, btn4;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
findViewById(R.id.btn).setOnClickListener(this);
btn1 = (Button) findViewById(R.id.btn1);
btn2 = (Button) findViewById(R.id.btn2);
btn3 = (Button) findViewById(R.id.btn3);
btn4 = (Button) findViewById(R.id.btn4);
list = new ArrayList<Button>();
list.add(btn1); list.add(btn2); list.add(btn3); list.add(btn4);
}
@Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.btn:
int[] start_location = new int[2];
v.getLocationInWindow(start_location);
TextView te = new TextView(this);
te.setText("啊");
move(te, start_location);
break;
}
}
private void move(final View v, int[] start_location) {
viewGroup = createAnimLayout();
viewGroup.addView(v);
int[] end_location = new int[2];
Random ra = new Random();
end_location = ...; // 取隨機目標按鈕座標
int endX = 0 - start_location[0] + v.getLeft();
int endY = end_location[1] - start_location[1];
TranslateAnimation translateAnimationX = new TranslateAnimation(0, endX, 0, 0);
translateAnimationX.setInterpolator(new LinearInterpolator());
TranslateAnimation translateAnimationY = new TranslateAnimation(0, 0, 0, endY);
translateAnimationY.setInterpolator(new AccelerateInterpolator());
AnimationSet set = new AnimationSet(false);
set.addAnimation(translateAnimationY);
set.addAnimation(translateAnimationX);
set.setDuration(800);
view.startAnimation(set);
set.setAnimationListener(new AnimationListener() {
@Override public void onAnimationStart(Animation animation) { v.setVisibility(View.VISIBLE); }
@Override public void onAnimationRepeat(Animation animation) { }
@Override public void onAnimationEnd(Animation animation) { v.setVisibility(View.GONE); }
});
}
private ViewGroup createAnimLayout() {
ViewGroup rootView = (ViewGroup) this.getWindow().getDecorView();
LinearLayout animLayout = new LinearLayout(this);
LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams(
LinearLayout.LayoutParams.MATCH_PARENT,
LinearLayout.LayoutParams.MATCH_PARENT);
animLayout.setLayoutParams(lp);
animLayout.setId(Integer.MAX_VALUE);
animLayout.setBackgroundResource(android.R.color.transparent);
rootView.addView(animLayout);
return animLayout;
}
}
Android FakeID 簽名漏洞原理與防禦解析
FakeID 是 Android 早期簽名驗證機制中的一個重要安全議題。它與 APK 簽名、證書鏈以及系統如何判斷應用身份有關。理解這個漏洞,有助於開發者建立更安全的簽名
Android Studio 實用指南:文本搜索工具
Android Studio 提供強大的文本搜索功能,可在編輯器或日志窗口中使用。Windows/Linux 可使用 Ctrl+F,macOS 可使用 Command+F
Android Timer 基礎用法:定時更新 ProgressBar
在 Android 中,Timer 可用來週期性執行任務。以下範例用 Timer 控制 ProgressBar 的進度更新。 建立 Timer 後,使用 schedul
Android 播放視頻時的可拖拽懸浮窗口:DragVideo 與 ViewDragHelper 解析
為什麼做這個項目 在視頻網站看電影時有個細節很打動人:當頁面滑動遮住了播放窗口,播放器會縮成一個小窗固定在角落,而且這個小窗可以隨意拖動。既然 Web 端能做,移動端自然