Android教程網
  1. 首頁
  2. Android 技術
  3. Android 手機
  4. Android 系統教程
  5. Android 游戲

Activity 跳轉動畫:從介面某位置「裂開拉伸」的炫酷過場

編輯:關於 Android 程式設計

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

思路:

1、在當前 Activity 中截取當前手機螢幕取得 bitmap,然後根據具體位置(比如這裡是掃碼圖標中間裂開)計算中間裂開處距手機上下的距離,並傳遞給跳轉後的 Activity。

也就是說,跳轉前的 Activity 要做兩件事:其一,截取螢幕取得 bitmap;其二,計算出具體裂開位置距螢幕上下的距離,傳給第二個 Activity 以便切割整張截圖。

2、跳轉後的 Activity 執行動畫即可。

整體效果如上圖:1、點擊掃碼;2、中間裂開並執行上下拉伸動畫(此時可以看到需要跳轉的 Activity);3、動畫結束。

具體實現

一、跳轉前 Activity 截屏獲取 bitmap,並取得裂開位置數值傳給第二個 Activity

// 這個 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();
        }
    }
}

1、獲取當前掃描按鈕中間的距離

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);
        }
    });
}

2、緊接著界面跳轉,傳遞參數 midData

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);

3、跳轉時取消系統動畫

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);
}

二、截取上一個 Activity(Fragment)的 bitmap,分成兩個執行動畫的 bitmap

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);
}

1、在第二個 Activity 最外層蓋上一個相對佈局用來執行動畫

以下是播放動畫的佈局(外層還有一個相對佈局,裡面需要顯示的佈局此處略去):


    
        
        
    
    
        
        
    

2、找到 view,設置它們上下需要執行動畫的 bitmap

/** 播放動畫 */
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 跳轉變得更具視覺衝擊力。

  1. 上一篇文章:
  2. 下一篇文章:
Copyright © Android教程網 All Rights Reserved