鍍金池/ 問答/Android  HTML/ Android View屬性動(dòng)畫實(shí)現(xiàn)放大效果出現(xiàn)的問題?。?/span>

Android View屬性動(dòng)畫實(shí)現(xiàn)放大效果出現(xiàn)的問題?。?/h1>

現(xiàn)在遇到個(gè)問題,就是想要放大一個(gè)圖片,且底部得位置不變 ,只方法上面Y軸,X左右也要放大,就是底部和原來保持平行 不變即可, 我試著用屬性動(dòng)畫

ObjectAnimator animator = ObjectAnimator.ofFloat(view, "scaleY", 0f, 1.6f, 1.6f);
ObjectAnimator rotate = ObjectAnimator.ofFloat(view, "scaleX", 0f, 1.6f,1.6f);

但是這樣是先的效果是 最終的效果是位置放生變化了,效果不好, 具體咋實(shí)現(xiàn)呢?求指點(diǎn)!謝謝!

如下圖
圖片描述

回答
編輯回答
傻丟丟

這個(gè)跟變換的中心點(diǎn)有關(guān),但是 ObjectAnimator 貌似沒有可以設(shè)置變換中心點(diǎn)的 api,當(dāng)然你可以可以再疊加一個(gè)位移的 ObjectAnimator 來修正你的錯(cuò)誤。

這里我使用了普通的 Animation 來實(shí)現(xiàn)你說的功能。

  • res/anim 下定義動(dòng)畫文件 simple.anim.xml
<?xml version="1.0" encoding="utf-8"?>
<scale xmlns:android="http://schemas.android.com/apk/res/android"
    android:duration="1000"
    android:fromXScale="1"
    android:fromYScale="1"
    android:pivotX="0"
    android:pivotY="1"
    android:toXScale="1.6"
    android:toYScale="1.6"/>

其中,pivotX/pivotY 屬性是定義變換的中心點(diǎn),這里定在左下角

  • 使用動(dòng)畫
Animation anim = AnimationUtils.loadAnimation(this, R.anim.simple_anim);
anim.setFillAfter(true);
view.startAnimation(anim);

注意要使用 setFillAfter 來讓變換后的大小保持。

但是這種做法有一個(gè)缺點(diǎn):變大后的 view 其有效的點(diǎn)擊面積還是跟變換前一樣,也就是說,有一些區(qū)域是不可點(diǎn)擊的。

2018年9月6日 03:56
編輯回答
女流氓

這個(gè)需要修改View動(dòng)畫變換的基準(zhǔn)點(diǎn)pivotX和pivotY,View默認(rèn)的基準(zhǔn)點(diǎn)是其中心,對(duì)于你的需求,保持底部不變,X和Y方向放大,需要將基準(zhǔn)點(diǎn)設(shè)置為View底邊的中點(diǎn)。

view.setPivotX(view.getWidth()/2);  // X方向中點(diǎn)
view.setPivotY(view.getHeight());   // Y方向底邊
AnimatorSet animatorSet = new AnimatorSet();  //組合動(dòng)畫
ObjectAnimator scaleX = ObjectAnimator.ofFloat(view, "scaleX", 0f, 1.6f);
ObjectAnimator scaleY = ObjectAnimator.ofFloat(view, "scaleY", 0f, 1.6f);
animatorSet.setDuration(2000);  //動(dòng)畫時(shí)間
animatorSet.setInterpolator(new DecelerateInterpolator());  //設(shè)置插值器
animatorSet.play(scaleX).with(scaleY);  //同時(shí)執(zhí)行
animatorSet.start();  //啟動(dòng)動(dòng)畫
2018年1月11日 11:11