編輯:關於 Android 程式設計
需求:Activity(或 Fragment)跳轉時,當前界面從某個位置裂開,上下各自拉出手機螢幕,之後跳轉到目標 Activity。整體效果如下圖。

思路:
1、在當前 Activity 中截取當前手機螢幕取得 bitmap,然後根據具體位置(比如這裡是掃碼圖標中間裂開)計算中間裂開處距手機上下的距離,並傳遞給跳轉後的 Activity。
也就是說,跳轉前的 Activity 要做兩件事:其一,截取螢幕取得 bitmap;其二,計算出具體裂開位置距螢幕上下的距離,傳給第二個 Activity 以便切割整張截圖。
2、跳轉後的 Activity 執行動畫即可。



整體效果如上圖:1、點擊掃碼;2、中間裂開並執行上下拉伸動畫(此時可以看到需要跳轉的 Activity);3、動畫結束。
// 這個 bitmap 用 public static Bitmap bitmap; 接收,方便第二個 activity 直接獲取 bitmap = ScreenShot.takeScreenShot(getActivity());
以下是工具類,直接調用即可獲取手機螢幕:
package com.lzyc.ybtappcal.util;
import android.app.Activity;
import android.graphics.Bitmap;
import android.graphics.Rect;
import android.view.View;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
/**
* Created by lovelin on 2016/7/20.
*/
public class ScreenShot {
private static int mHiddenViewMeasureHeight; // 中間切割的高度
private static int screenHeightPixels; // 螢幕高度
// 獲取指定 Activity 的截屏
public static Bitmap takeScreenShot(Activity activity) {
View view = activity.getWindow().getDecorView();
view.setDrawingCacheEnabled(true);
view.buildDrawingCache();
Bitmap b1 = view.getDrawingCache();
// 獲取狀態欄高度
Rect frame = new Rect();
activity.getWindow().getDecorView().getWindowVisibleDisplayFrame(frame);
int statusBarHeight = frame.top;
// 獲取螢幕長和高
int width = activity.getWindowManager().getDefaultDisplay().getWidth();
int height = activity.getWindowManager().getDefaultDisplay().getHeight();
// 去掉標題欄
Bitmap b = Bitmap.createBitmap(b1, 0, statusBarHeight, width, height - statusBarHeight);
view.destroyDrawingCache();
return b;
}
// 保存到 sdcard
private static void savePic(Bitmap b, String strFileName) {
FileOutputStream fos = null;
try {
fos = new FileOutputStream(strFileName);
if (null != fos) {
b.compress(Bitmap.CompressFormat.PNG, 90, fos);
fos.flush();
fos.close();
}
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
}
private void getMessureHeight() {
v.id_linea_top.getViewTreeObserver().addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
@TargetApi(Build.VERSION_CODES.JELLY_BEAN)
@Override
public void onGlobalLayout() {
int imageHanlfHeight = (v.iv_fg_top.getBottom() - v.iv_fg_top.getTop()) / 2; // image 的一半高度
int bottom = v.iv_fg_top.getBottom();
/**
* imageview 掃描按鈕底部距離 - 按鈕本身一半的距離 = 距離手機頂部的距離
* (即播放動畫需要截取的上半圖片的高度)
*/
midData = bottom - imageHanlfHeight;
v.id_linea_top.getViewTreeObserver().removeOnGlobalLayoutListener(this);
}
});
}
Bundle mBundle = new Bundle();
mBundle.putInt(Contants.KEY_PAGE_SEARCH, Contants.VAL_PAGE_SEARCH_TOP);
mBundle.putInt("midData", midData);
mBundle.putInt("h", h);
mBundle.putInt("topSplitHeight", topSplitHeight);
openActivityNoAnim(CaptureActivity.class, mBundle);
public void openActivityNoAnim(Class ActivityClass, Bundle b) {
Intent intent = new Intent(mContext, ActivityClass);
intent.setFlags(Intent.FLAG_ACTIVITY_NO_ANIMATION);
intent.putExtras(b);
startActivity(intent);
}
private void cutting() {
// 切割第一個圖(上半部分)
bitmapTop = Bitmap.createBitmap(TopFragment.bitmap, 0, 0, TopFragment.bitmap.getWidth(), this.midData);
// 切割第二個圖(下半部分)
bitmapBottom = Bitmap.createBitmap(TopFragment.bitmap, 0, this.midData, TopFragment.bitmap.getWidth(), TopFragment.bitmap.getHeight() - midData);
}
以下是播放動畫的佈局(外層還有一個相對佈局,裡面需要顯示的佈局此處略去):
/** 播放動畫 */
private void startAnima() {
cutting();
final ImageView loading_iv_top = (ImageView) findViewById(R.id.loading_iv_top);
final ImageView loading_iv_bottm = (ImageView) findViewById(R.id.loading_iv_bottm);
id_linear_capture_top = (LinearLayout) findViewById(R.id.id_linear_capture_top);
id_linear_capture_under = (LinearLayout) findViewById(R.id.id_linear_capture_under);
final RelativeLayout id_relative_capture = (RelativeLayout) findViewById(R.id.id_relative_capture);
// 設置上下播放拉伸圖片
loading_iv_top.setImageBitmap(this.bitmapTop);
loading_iv_bottm.setImageBitmap(this.bitmapBottom);
id_relative_capture.getViewTreeObserver().addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
@TargetApi(Build.VERSION_CODES.JELLY_BEAN)
@Override
public void onGlobalLayout() {
// 設置了圖片,所以在此獲取它們兩個的高,即執行動畫的距離
topHeight = loading_iv_top.getHeight();
bottonHeight = loading_iv_bottm.getHeight();
ObjectAnimator animator = ObjectAnimator.ofFloat(id_linear_capture_top, "translationY", 0, -topHeight);
ObjectAnimator animator1 = ObjectAnimator.ofFloat(id_linear_capture_under, "translationY", 0, bottonHeight);
AnimatorSet animSet = new AnimatorSet();
animSet.play(animator).with(animator1);
animSet.setDuration(400);
animSet.start();
id_relative_capture.getViewTreeObserver().removeOnGlobalLayoutListener(this);
}
});
}
1、當前 Activity 截屏獲取 bitmap;
2、第二個 Activity 剪切上下需要執行動畫的 bitmap;
3、執行動畫(這裡只寫打開的動畫,關閉同理,也是一個動畫而已)。
透過截屏、切割與屬性動畫的組合,就能實現這種「畫面裂開、上下拉伸」的過場效果,讓 Activity 跳轉變得更具視覺衝擊力。
Android 自訂註解:概念、元註解與兩個實戰案例
前言 註解的使用頻率一直很高。ButterKnife 這類注入框架、ActiveAndroid 這類資料庫框架,都是靠註解把開發效率拉起來的。因此理解註解的運作方式並能熟
打造開發神器:五個值得裝的 Android Studio 插件
插件用得好,能省下大量重複敲擊。以下五個按實用程度排序。 1. Android Parcelable code generator 顧名思義,生成 Parcelabl
微信授權登入接入第三方 Android App:完整流程與踩坑筆記
這陣子開發需要用到微信授權,於是實際跑了一輪。過程中發現微信開放平台的接入指南有幾處描述得過於簡略,容易卡住,因此把整個流程與容易出錯的地方整理成一篇,給需要的人參考。
Android 多線程下載原理與實作:分段 + RandomAccessFile
單線程下載有個明顯的缺點:一條連接的速度往往吃不满帶寬。多線程下載的核心思路很簡單——把一個大文件切成若干段,每段用一條獨立連接去取,最後寫入同一個文件的對應位置。 一