Android實(shí)現(xiàn)dialog的3D翻轉(zhuǎn)示例
本文實(shí)現(xiàn)了Android中dialog的3D翻轉(zhuǎn)效果。這里通過(guò)一個(gè)簡(jiǎn)單的應(yīng)用場(chǎng)景記錄下。
效果圖

起初自己的思路是Activity進(jìn)行界面跳轉(zhuǎn)實(shí)現(xiàn)旋轉(zhuǎn)效果,網(wǎng)上看了很多,寫(xiě)下來(lái)發(fā)現(xiàn)效果不對(duì)。之后又看到Google上面的Card Flid Animation效果是這樣的。

看著確實(shí)不錯(cuò),然而拿下來(lái)demo放慢翻轉(zhuǎn)速度后發(fā)現(xiàn),不是我想要的。但是跟我看到的一個(gè)app里面的效果一樣
然后想改成dialog試試效果,發(fā)現(xiàn)更是不行了。
Card Flid Animation效果如下:
這個(gè)是通過(guò)Activity來(lái)切換Fragment實(shí)現(xiàn)的,可以看到區(qū)別是翻轉(zhuǎn)時(shí)候貌似會(huì)變大,其實(shí)沒(méi)用,只是翻轉(zhuǎn)后的視覺(jué)問(wèn)題。

聽(tīng)說(shuō)openGl比較麻煩,并且沒(méi)有用過(guò)。然后就搜了下Rotate3DAnimaitons。
搜到了這篇文章http://www.dbjr.com.cn/article/77195.htm
所以這篇文章里的實(shí)現(xiàn)方法不是我的原創(chuàng),是參考人家的。在這里感謝這位大神。
不過(guò)他這個(gè)是activity里的,我就想要一個(gè)dialog效果,因?yàn)殡娔X上TIM的打開(kāi)紅包這個(gè)3D效果看著不錯(cuò),其實(shí)大同小異,就拿過(guò)來(lái)改成Dialog。
對(duì)于Rotate3DAnimaitons這篇文章已經(jīng)很詳細(xì)了,有需要的可以參考下。
這里也貼下Rotate3dAnimation 的代碼
簡(jiǎn)單加了兩行注釋
/**
* An animation that rotates the view on the Y axis between two specified angles.
* This animation also adds a translation on the Z axis (depth) to improve the effect.
*/
public class Rotate3dAnimation extends Animation {
private final float mFromDegrees;
private final float mToDegrees;
private final float mCenterX;
private final float mCenterY;
private final float mDepthZ;
private final boolean mReverse;
private Camera mCamera;
/**
* Creates a new 3D rotation on the Y axis. The rotation is defined by its
* start angle and its end angle. Both angles are in degrees. The rotation
* is performed around a center point on the 2D space, definied by a pair
* of X and Y coordinates, called centerX and centerY. When the animation
* starts, a translation on the Z axis (depth) is performed. The length
* of the translation can be specified, as well as whether the translation
* should be reversed in time.
*
* @param fromDegrees the start angle of the 3D rotation //起始角度
* @param toDegrees the end angle of the 3D rotation //結(jié)束角度
* @param centerX the X center of the 3D rotation //x中軸線
* @param centerY the Y center of the 3D rotation //y中軸線
* @param reverse true if the translation should be reversed, false otherwise//是否反轉(zhuǎn)
*/
public Rotate3dAnimation(float fromDegrees, float toDegrees,
float centerX, float centerY, float depthZ, boolean reverse) {
mFromDegrees = fromDegrees;
mToDegrees = toDegrees;
mCenterX = centerX;
mCenterY = centerY;
mDepthZ = depthZ;//Z軸移動(dòng)的距離,這個(gè)來(lái)影響視覺(jué)效果,可以解決flip animation那個(gè)給人看似放大的效果
mReverse = reverse;
}
@Override
public void initialize(int width, int height, int parentWidth, int parentHeight) {
super.initialize(width, height, parentWidth, parentHeight);
mCamera = new Camera();
}
@Override
protected void applyTransformation(float interpolatedTime, Transformation t) {
final float fromDegrees = mFromDegrees;
float degrees = fromDegrees + ((mToDegrees - fromDegrees) * interpolatedTime);
final float centerX = mCenterX;
final float centerY = mCenterY;
final Camera camera = mCamera;
final Matrix matrix = t.getMatrix();
Log.i("interpolatedTime", interpolatedTime+"");
camera.save();
if (mReverse) {
camera.translate(0.0f, 0.0f, mDepthZ * interpolatedTime);
} else {
camera.translate(0.0f, 0.0f, mDepthZ * (1.0f - interpolatedTime));
}
camera.rotateY(degrees);
camera.getMatrix(matrix);
camera.restore();
matrix.preTranslate(-centerX, -centerY);
matrix.postTranslate(centerX, centerY);
}
}
dialog實(shí)現(xiàn)3D翻轉(zhuǎn)代碼,
說(shuō)明:動(dòng)畫(huà)部分的代碼是拿的搜的的那篇文章的
public class MyDialog extends Dialog {
@BindView(R.id.et_user_name)
EditText etUserName;
@BindView(R.id.et_password)
EditText etPassword;
@BindView(R.id.cb_auto_login)
CheckBox cbAutoLogin;
@BindView(R.id.tv_forget_pwd)
TextView tvForgetPwd;
@BindView(R.id.ll_content)
LinearLayout llContent;
@BindView(R.id.et_email)
EditText etEmail;
@BindView(R.id.btn_back)
Button btnBack;
@BindView(R.id.container)
RelativeLayout container;
private Context context;
@BindView(R.id.ll_register)
LinearLayout llRegister;
//接口回調(diào)傳遞參數(shù)
private OnClickListenerInterface mListener;
private View view;
//
private String strContent;
private int centerX;
private int centerY;
private int depthZ = 700;//修改此處可以改變距離來(lái)達(dá)到你滿意的效果
private int duration = 300;//動(dòng)畫(huà)時(shí)間
private Rotate3dAnimation openAnimation;
private Rotate3dAnimation closeAnimation;
private boolean isOpen = false;
public interface OnClickListenerInterface {
/**
* 確認(rèn),
*/
void doConfirm();
/**
* 取消
*/
// public void doCancel();
}
public MyDialog(Context context) {
super(context);
this.context = context;
}
public MyDialog(Context context, String content) {
super(context);
this.context = context;
this.strContent = content;
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
//去掉系統(tǒng)的黑色矩形邊框
getWindow().setBackgroundDrawableResource(android.R.color.transparent);
requestWindowFeature(Window.FEATURE_NO_TITLE);
init();
}
public void init() {
LayoutInflater inflater = LayoutInflater.from(context);
view = inflater.inflate(R.layout.dialog_my, null);
setContentView(view);
ButterKnife.bind(this);
etPassword.setTypeface(Typeface.DEFAULT);
etPassword.setTransformationMethod(new PasswordTransformationMethod());
tvForgetPwd.setOnClickListener(new OnWidgetClickListener());
btnBack.setOnClickListener(new OnWidgetClickListener());
Window dialogWindow = getWindow();
WindowManager.LayoutParams lp = dialogWindow.getAttributes();
DisplayMetrics d = context.getResources().getDisplayMetrics(); // 獲取屏幕寬、高用
lp.width = (int) (d.widthPixels * 0.8); // 寬度設(shè)置為屏幕的0.8
lp.height = (int) (d.heightPixels * 0.6); // 高度設(shè)置為屏幕的0.6
dialogWindow.setAttributes(lp);
setCanceledOnTouchOutside(false);
setCancelable(true);
}
public void setClicklistener(OnClickListenerInterface clickListenerInterface) {
this.mListener = clickListenerInterface;
}
private class OnWidgetClickListener implements View.OnClickListener {
@Override
public void onClick(View v) {
int id = v.getId();
switch (id) {
case R.id.tv_forget_pwd:
startAnimation();
break;
case R.id.btn_back:
startAnimation();
break;
}
}
}
private void startAnimation() {
//接口回調(diào)傳遞參數(shù)
centerX = container.getWidth() / 2;
centerY = container.getHeight() / 2;
if (openAnimation == null) {
initOpenAnim();
initCloseAnim();
}
//用作判斷當(dāng)前點(diǎn)擊事件發(fā)生時(shí)動(dòng)畫(huà)是否正在執(zhí)行
if (openAnimation.hasStarted() && !openAnimation.hasEnded()) {
return;
}
if (closeAnimation.hasStarted() && !closeAnimation.hasEnded()) {
return;
}
//判斷動(dòng)畫(huà)執(zhí)行
if (isOpen) {
container.startAnimation(openAnimation);
} else {
container.startAnimation(closeAnimation);
}
isOpen = !isOpen;
}
/**
*注意旋轉(zhuǎn)角度
*/
private void initOpenAnim() {
//從0到90度,順時(shí)針旋轉(zhuǎn)視圖,此時(shí)reverse參數(shù)為true,達(dá)到90度時(shí)動(dòng)畫(huà)結(jié)束時(shí)視圖變得不可見(jiàn),
openAnimation = new Rotate3dAnimation(0, 90, centerX, centerY, depthZ, true);
openAnimation.setDuration(duration);
openAnimation.setFillAfter(true);
openAnimation.setInterpolator(new AccelerateInterpolator());
openAnimation.setAnimationListener(new Animation.AnimationListener() {
@Override
public void onAnimationStart(Animation animation) {
}
@Override
public void onAnimationRepeat(Animation animation) {
}
@Override
public void onAnimationEnd(Animation animation) {
llRegister.setVisibility(View.GONE);
llContent.setVisibility(View.VISIBLE);
//從270到360度,順時(shí)針旋轉(zhuǎn)視圖,此時(shí)reverse參數(shù)為false,達(dá)到360度動(dòng)畫(huà)結(jié)束時(shí)視圖變得可見(jiàn)
Rotate3dAnimation rotateAnimation = new Rotate3dAnimation(270, 360, centerX, centerY, depthZ, false);
rotateAnimation.setDuration(duration);
rotateAnimation.setFillAfter(true);
rotateAnimation.setInterpolator(new DecelerateInterpolator());
container.startAnimation(rotateAnimation);
}
});
}
private void initCloseAnim() {
closeAnimation = new Rotate3dAnimation(360, 270, centerX, centerY, depthZ, true);
closeAnimation.setDuration(duration);
closeAnimation.setFillAfter(true);
closeAnimation.setInterpolator(new AccelerateInterpolator());
closeAnimation.setAnimationListener(new Animation.AnimationListener() {
@Override
public void onAnimationStart(Animation animation) {
}
@Override
public void onAnimationRepeat(Animation animation) {
}
@Override
public void onAnimationEnd(Animation animation) {
llRegister.setVisibility(View.VISIBLE);
llContent.setVisibility(View.GONE);
Rotate3dAnimation rotateAnimation = new Rotate3dAnimation(90, 0, centerX, centerY, depthZ, false);
rotateAnimation.setDuration(duration);
rotateAnimation.setFillAfter(true);
rotateAnimation.setInterpolator(new DecelerateInterpolator());
container.startAnimation(rotateAnimation);
}
});
}
}
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
Android自定義帶加載動(dòng)畫(huà)效果的環(huán)狀進(jìn)度條
這篇文章主要介紹了Android自定義帶加載動(dòng)畫(huà)效果的環(huán)狀進(jìn)度條,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-12-12
android使用Jsoup 抓取頁(yè)面的數(shù)據(jù)
本篇文章主要介紹了android使用Jsoup 抓取頁(yè)面的數(shù)據(jù),jsoup 是一款Java的HTML解析器,有需要的朋友可以了解一下。2016-11-11
Android中系統(tǒng)自帶鎖WalkLock與KeyguardLock用法實(shí)例詳解
這篇文章主要介紹了Android中系統(tǒng)自帶鎖WalkLock與KeyguardLock用法,結(jié)合實(shí)例形式較為詳細(xì)的分析了WalkLock與KeyguardLock的功能、作用、使用方法與相關(guān)注意事項(xiàng),需要的朋友可以參考下2016-01-01
android自定義控件ImageView實(shí)現(xiàn)圓形圖片
這篇文章主要為大家詳細(xì)介紹了android自定義控件ImageView實(shí)現(xiàn)圓形圖片,適用于用戶頭像,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-12-12
ionic監(jiān)聽(tīng)android返回鍵實(shí)現(xiàn)“再按一次退出”功能
本篇文章主要介紹了ionic監(jiān)聽(tīng)android返回鍵實(shí)現(xiàn)“再按一次退出”功能,非常具有實(shí)用價(jià)值,需要的朋友可以參考下2018-02-02
AndroidStudio代碼達(dá)到指定字符長(zhǎng)度時(shí)自動(dòng)換行實(shí)例
這篇文章主要介紹了AndroidStudio代碼達(dá)到指定字符長(zhǎng)度時(shí)自動(dòng)換行實(shí)例,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2020-03-03
Android入門(mén)之AlertDialog用法實(shí)例分析
這篇文章主要介紹了Android入門(mén)之AlertDialog用法,對(duì)Android初學(xué)者有很多的借鑒學(xué)習(xí)之處,需要的朋友可以參考下2014-08-08
Android自定義評(píng)分控件的完整實(shí)例
在Android開(kāi)發(fā)中,我們經(jīng)常會(huì)用到對(duì)商家或者商品的評(píng)價(jià),運(yùn)用星星進(jìn)行打分,下面這篇文章主要給大家介紹了關(guān)于Android自定義評(píng)分控件的相關(guān)資料,文中通過(guò)圖文介紹的非常詳細(xì),需要的朋友可以參考下2022-05-05

