編輯:關於 Android 程式設計
Metro 风格(磁贴风格)以其简洁明快的方块布局广受欢迎。今天我们就来在 Android 平台上实现这样一个炫酷的 Metro 风格菜单效果。
核心是利用动画来实现方块的移动效果,这里使用的是 TranslateAnimation 方法。先来看一下布局文件。
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="https://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="#000000"
android:orientation="vertical" >
<!-- 第一层 -->
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_weight="1"
android:orientation="horizontal" >
<!-- 第一层 横向左 -->
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_weight="1"
android:orientation="vertical" >
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_weight="1"
android:orientation="horizontal" >
<!-- 1 -->
<RelativeLayout
android:id="@+id/nine_one"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_weight="1"
android:background="#FFFF00" ></RelativeLayout>
<!-- 2 -->
<RelativeLayout
android:id="@+id/nine_two"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_weight="1"
android:background="#FFC0CB" ></RelativeLayout>
</LinearLayout>
<!-- 4 -->
<RelativeLayout
android:id="@+id/nine_four"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_weight="1"
android:background="#EE30A7" ></RelativeLayout>
<!-- 5 -->
<RelativeLayout
android:id="@+id/nine_five"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_weight="1"
android:background="#EE4000" ></RelativeLayout>
</LinearLayout>
<!-- 第一层 横向右 -->
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_weight="2"
android:orientation="vertical" >
<!-- 3 -->
<RelativeLayout
android:id="@+id/nine_three"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_weight="2"
android:background="#FF8C69" ></RelativeLayout>
<!-- 6 -->
<RelativeLayout
android:id="@+id/nine_six"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_weight="1"
android:background="#8C8C8C" ></RelativeLayout>
</LinearLayout>
</LinearLayout>
<!-- 第二层 -->
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_weight="3"
android:baselineAligned="false"
android:orientation="horizontal" >
<!-- 7 -->
<RelativeLayout
android:id="@+id/nine_seven"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_weight="1"
android:background="#8B3E2F" ></RelativeLayout>
<!-- 8 -->
<!-- 9 -->
<RelativeLayout
android:id="@+id/nine_nine"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_weight="1"
android:background="#A52A2A" ></RelativeLayout>
</LinearLayout>
</LinearLayout>
它的静态效果是这样的:
之后在 MainActivity 里对每个 Layout 进行动画移动,就能实现方块飞入的平移效果了。
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.util.DisplayMetrics;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.Window;
import android.view.animation.TranslateAnimation;
import android.widget.RelativeLayout;
import android.widget.Toast;
public class MainActivity extends Activity {
private View viewNine;
private LayoutInflater inflater;
private RelativeLayout nine_one, nine_two, nine_three, nine_four,
nine_five, nine_six, nine_seven, nine_nine;
private TranslateAnimation myAnimation_Right, myAnimation_Bottom;
private TranslateAnimation myAnimation_Left, myAnimation_Top;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
inflater = LayoutInflater.from(this);
viewNine = inflater.inflate(R.layout.activity_main, null);
nine_one = (RelativeLayout) viewNine.findViewById(R.id.nine_one);
nine_two = (RelativeLayout) viewNine.findViewById(R.id.nine_two);
nine_three = (RelativeLayout) viewNine.findViewById(R.id.nine_three);
nine_four = (RelativeLayout) viewNine.findViewById(R.id.nine_four);
nine_five = (RelativeLayout) viewNine.findViewById(R.id.nine_five);
nine_six = (RelativeLayout) viewNine.findViewById(R.id.nine_six);
nine_seven = (RelativeLayout) viewNine.findViewById(R.id.nine_seven);
nine_nine = (RelativeLayout) viewNine.findViewById(R.id.nine_nine);
setContentView(viewNine);
nine_four.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
Intent intent = new Intent(MainActivity.this, OneActivity.class);
startActivity(intent);
}
});
nine_six.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {}
});
myAnimation();
addAnimation();
}
// 启动动画
private void addAnimation() {
nine_one.startAnimation(myAnimation_Right);
nine_two.startAnimation(myAnimation_Bottom);
nine_three.startAnimation(myAnimation_Left);
nine_four.startAnimation(myAnimation_Bottom);
nine_five.startAnimation(myAnimation_Left);
nine_six.startAnimation(myAnimation_Top);
nine_seven.startAnimation(myAnimation_Left);
nine_nine.startAnimation(myAnimation_Left);
}
// 动画定义
private void myAnimation() {
DisplayMetrics displayMetrics = new DisplayMetrics();
displayMetrics = this.getResources().getDisplayMetrics();
// 获得屏幕宽度
int screenWidth = displayMetrics.widthPixels;
// 获得屏幕高度
int screenHeight = displayMetrics.heightPixels;
myAnimation_Right = new TranslateAnimation(screenWidth, 0, 0, 0);
myAnimation_Right.setDuration(1800);
myAnimation_Bottom = new TranslateAnimation(0, 0, screenHeight, 0);
myAnimation_Bottom.setDuration(1500);
myAnimation_Left = new TranslateAnimation(-screenWidth, 0, 0, 0);
myAnimation_Left.setDuration(2000);
myAnimation_Top = new TranslateAnimation(0, 0, -screenHeight, 0);
myAnimation_Top.setDuration(2500);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
return true;
}
}
虽然效果还不错,但由于布局文件使用了多层嵌套,这样会造成绘制窗口时的性能问题。所以如果你对程序性能要求很严格,建议改用 RelativeLayout 来布局,并尽量减少嵌套层级,这样能显著提升滑动与渲染的流畅度。
使用 AsyncTask 下載檔案並更新進度
AsyncTask 是 Android 早期常用於簡化背景任務與介面更新的工具類別。它適合處理不需要過於複雜背景邏輯的場景,例如下載檔案、更新進度條或執行短時間的非同步工
Android 自定義 View 實作圓形 Progress 進度條
先看成果: 目標很明確:在一個圓框內部畫一段會持續增長的扇形。整體可以拆成四個步驟。 一、建立類別與自定義屬性 新增 ProgressView 繼承自 View
Android 自製帶圓形標記的數字進度列完整教學
開發緣起 設計師丟來一個帶圓形標記的進度條需求,我在 GitHub 上翻了一圈沒找到合適的現成元件,於是決定自己動手做一個。 先看成品效果: 核心思路是寫一個繼
如何寫一個正經的 Android 音樂播放器(一)
以前寫過很多次音樂播放器,但總會遇到一些問題,例如: 1、音樂長時間播放問題(即便放在 Service 中播放,依然會被殺死); 2、音樂的播放進度如何掌握?(如何利用