android通過xml檔案實現Animation動畫

來源:互聯網
上載者:User

標籤:

Rotate的xml檔案編寫方法

    <rotate        android:fromDegrees="0"        android:toDegrees="+350"        android:pivotX="50%"        android:pivotY="50%"        android:duration="1000"/>

*android:toDegrees="+350"正號代表的是旋轉方向,正號為順時針,負號為逆時針

*android:pivotX的值共有三種設定方法

1.android:pivotX="50"這種方法使用絕對位置定位(螢幕位置座標上50這個點)

2.android:pivotX="50%"這種方法使用相對於控制項本身定位

3.android:pivotX="50%p"這種方法相對於控制項的父控制項定位

其他幾種動畫的寫法與此類似

 

MainActivity.java

public class MainActivity extends Activity {    private ImageView imageView;    private Button button_1;    private Button button_2;    private Button button_3;    private Button button_4;    @Override    protected void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        setContentView(R.layout.main);                imageView = (ImageView)findViewById(R.id.imageView);        button_1 = (Button)findViewById(R.id.button_1);        button_2 = (Button)findViewById(R.id.button_2);        button_3 = (Button)findViewById(R.id.button_3);        button_4 = (Button)findViewById(R.id.button_4);                AlphaButtonListener alphaButtonListener = new AlphaButtonListener();        RotateButtonListener rotateButtonListener = new RotateButtonListener();        ScaleButtonListener scaleButtonListener = new ScaleButtonListener();        TranslateButtonListener translateButtonListener = new TranslateButtonListener();                button_1.setOnClickListener(alphaButtonListener);        button_2.setOnClickListener(rotateButtonListener);        button_3.setOnClickListener(scaleButtonListener);        button_4.setOnClickListener(translateButtonListener);    }    //淡入淡出    private class AlphaButtonListener implements OnClickListener {        @Override        public void onClick(View v) {            //使用AnimationUtils裝載動畫檔案            Animation animation = AnimationUtils.loadAnimation(MainActivity.this, R.anim.alpha);            imageView.startAnimation(animation);        }    }    //旋轉    private class RotateButtonListener implements OnClickListener {        @Override        public void onClick(View v) {            Animation animation = AnimationUtils.loadAnimation(MainActivity.this, R.anim.rotate);            imageView.startAnimation(animation);        }    }    //水平     private class TranslateButtonListener implements OnClickListener {        @Override        public void onClick(View v) {            Animation animation = AnimationUtils.loadAnimation(MainActivity.this, R.anim.translate);            imageView.startAnimation(animation);        }    }    //縮放    private class ScaleButtonListener implements OnClickListener {        @Override        public void onClick(View v) {            Animation animation = AnimationUtils.loadAnimation(MainActivity.this, R.anim.scale);            imageView.startAnimation(animation);        }    }}

在res目錄下建立anim檔案夾

alpha.xml

<?xml version="1.0" encoding="utf-8"?><set xmlns:android="http://schemas.android.com/apk/res/android"    android:interpolator="@android:anim/accelerate_interpolator">        <alpha         android:fromAlpha="1.0"        android:toAlpha="0.0"        android:startOffset="500"        android:duration="500"/></set>

rotate.xml

<?xml version="1.0" encoding="utf-8"?><set xmlns:android="http://schemas.android.com/apk/res/android"    android:interpolator="@android:anim/accelerate_interpolator">        <rotate         android:fromDegrees="0"        android:toDegrees="360"        android:pivotX="50%"        android:pivotY="50%"        android:duration="3000"/></set>

scale.xml

<?xml version="1.0" encoding="utf-8"?><set xmlns:android="http://schemas.android.com/apk/res/android">    <scale         android:fromXScale="1.0"        android:toXScale="0.0"        android:fromYScale="1.0"        android:toYScale="0.0"        android:pivotX="50%"        android:pivotY="50%"        android:duration="1000"/></set>

translate.xml

<?xml version="1.0" encoding="utf-8"?><set xmlns:android="http://schemas.android.com/apk/res/android"    android:interpolator="@android:anim/accelerate_interpolator">        <translate         android:fromXDelta="50%"        android:toXDelta="100%"        android:fromYDelta="50%"        android:toYDelta="100%"        android:duration="1000"/></set>

 

android通過xml檔案實現Animation動畫

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.