android動畫介紹之 自訂Animation動畫實現qq抖一抖效果,androidanimation

來源:互聯網
上載者:User

android動畫介紹之 自訂Animation動畫實現qq抖一抖效果,androidanimation

昨天我們介紹了Animation的基本用法。小夥伴們瞭解的怎麼樣了?如果還沒有瞭解過Animation的小夥伴可以看看這篇部落格

android動畫介紹--Animation 實現loading動畫效果

安卓內建的四種動畫雖然說可以用AnimationSet進行組合操作,但是這些組合往往在特定場合是不夠用的,就像view一樣,我們可以自訂動畫效果。

今天要做成的效果是模仿qq的抖一抖效果,老規矩,先來看今天的:


自訂動畫的實現方法為 繼承Animation類重寫applyTransformation()方法

public class CustomAnimation extends Animation {    @Override    protected void applyTransformation(float interpolatedTime, Transformation t) {        t.getMatrix().setTranslate((float)Math.sin(interpolatedTime*50)*20,0);        super.applyTransformation(interpolatedTime, t);    }}

可以看到 applyTransformation()方法有兩個參數  第一個參數interpolatedTime代表一個0-1逐漸增大的float, t代表view所需要的變化,例如t.setAlpha();  t.setTranslate();顧名思義這兩個方法是發生透明度變化和位移變化。

那麼我們今天的目標是實現抖一抖效果,思路如下: view先向左位移,再向右位移。 座標為  (0-n,0) --->(0+n,n),一正一負的變化,就要用到我們的三角函數了。所以這裡t.setTranslate(Math.sin())。

這樣基本上我們的自訂 動畫就完成了,接下來在MainActivity下擷取當前的布局,給他添加動畫效果

public class MainActivity extends ActionBarActivity {    private Button mButton;    private RelativeLayout mRelativeLayout;    @Override    protected void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        setContentView(R.layout.activity_main);        mButton = (Button) findViewById(R.id.shake);        mRelativeLayout = (RelativeLayout) findViewById(R.id.relativelayout);        final CustomAnimation ca = new CustomAnimation();        ca.setDuration(1000);        mButton.setOnClickListener(new View.OnClickListener() {            @Override            public void onClick(View view) {                mRelativeLayout.startAnimation(ca);            }        });    }}


這樣便大功告成!如果喜歡我的部落格,請點贊哦

原始碼下載

著作權聲明:本文為博主原創文章,歡迎註明出處後轉載。

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.