亚洲香蕉成人av网站在线观看_欧美精品成人91久久久久久久_久久久久久久久久久亚洲_热久久视久久精品18亚洲精品_国产精自产拍久久久久久_亚洲色图国产精品_91精品国产网站_中文字幕欧美日韩精品_国产精品久久久久久亚洲调教_国产精品久久一区_性夜试看影院91社区_97在线观看视频国产_68精品久久久久久欧美_欧美精品在线观看_国产精品一区二区久久精品_欧美老女人bb

首頁 > 系統 > Android > 正文

android Tween Animation屬性設置方法實例

2020-04-11 12:00:44
字體:
來源:轉載
供稿:網友

在Android開發中,Animation是用來給控件制作效果的。大多數的控件都可以用這個類,這個類包含了4種基本動作,分別為移動,旋轉,淡入淡出,縮放。在使用Animation時,可以在.java文件中用java代碼對其進行設置,這樣的優點是可以方便調試程序效果;另外一種方法就是在xml中對控件的屬性做設置,好處是代碼的重用性比較高,缺點是不方便調試。

一、在java代碼中使用Animation
在java代碼中使用Animation主要分為下面4個步驟。
創建一個AnimationSet類,AnimationSet類是一個Animation集合,里面可以許多Animation,且在AnimationSet中設置的屬性適用于里面的所有Animation。
根據我們需要的動態效果創建一個Animation類,主要有4個這樣的類,分別為AlphaAnimation,ScaleAnimation,RotateAnimation,TranslateAnimation,分別對應著一種動畫效果。
將上面建立好的Animation設置相應的動態屬性,然后加入到AnimationSet中。
最后在需要適用這個動態的效果的控件中加載這個AnimationSet。

這里,做了一個簡單的實驗,分別試了下上面的動態效果。實驗室對一個image圖標進行動態演示,下面有4個按鈕,每個按鈕對應一個動態演示的效果。這4中效果分別是:image圖標由完全透明到完全不透明變化,持續時間為1s;image圖標由原來大小尺寸沿自身尺寸中心逐漸縮放到0,持續時間為1s;image圖標以自身中心為圓心旋轉360度,持續時間為1s;image圖標從自身位置開始同時向右和向下移動了imageView控件的寬和高長度。
界面如下所示(動態效果就不一張一張截圖演示了):


在設置Animation屬性的時候,有一點需要注意的是,在進行尺度縮放,平移,旋轉會涉及到中心點以及移動的距離那些參數,這些參數的值的設置是需要依據它值屬性來的,如果值屬性為Animation.RELATIVE_TO_SELF,那么就是參考控件自身的坐標,如果是Animation.RELATIVE_TO_PARENT,那么就是參考程序界面的坐標。

Java文件代碼如下:

復制代碼 代碼如下:

package com.example.anim_1;

import android.app.Activity;
import android.view.View;
import android.view.View.OnClickListener;
import android.os.Bundle;
import android.view.Menu;
import android.view.animation.AlphaAnimation;
import android.view.animation.Animation;
import android.view.animation.AnimationSet;
import android.view.animation.RotateAnimation;
import android.view.animation.ScaleAnimation;
import android.view.animation.TranslateAnimation;
import android.widget.Button;
import android.widget.ImageView;

public class MainActivity extends Activity {

    private ImageView image  = null;
    private Button alpha = null;
    private Button scale = null;
    private Button rotate = null;
    private Button translate = null;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        image = (ImageView)findViewById(R.id.image);
        alpha = (Button)findViewById(R.id.alpha);
        scale = (Button)findViewById(R.id.scale);
        rotate = (Button)findViewById(R.id.rotate);
        translate = (Button)findViewById(R.id.translate);

        alpha.setOnClickListener(new AlphaButtonListener());
        scale.setOnClickListener(new ScaleButtonListener());
        rotate.setOnClickListener(new RotateButtonListener());
        translate.setOnClickListener(new TranslateButtonListener());

    }

    private class AlphaButtonListener implements OnClickListener{

        public void onClick(View v) {
            // TODO Auto-generated method stub
            //這里設置ture參數表示共享interpolator
            AnimationSet alpha_animation_set = new AnimationSet(true);
            //從完全不透明到完全透明
            //這里的數字后面用f難道表示浮點型?
            AlphaAnimation alpha_animation = new AlphaAnimation(1.0f, 0.0f);   
            alpha_animation_set.addAnimation(alpha_animation);   
            alpha_animation.setDuration(1000);//1s鐘   
            image.startAnimation(alpha_animation_set);
        }

    }

    private class ScaleButtonListener implements OnClickListener{

        public void onClick(View v) {
            // TODO Auto-generated method stub
            AnimationSet scale_animation_set = new AnimationSet(true);
            //以自身為尺度縮放中心,從原大小尺寸逐漸縮放到0尺寸
            ScaleAnimation scale_animation = new ScaleAnimation(1.0f, 0.0f, 1.0f, 0.0f,
                                            Animation.RELATIVE_TO_SELF, 0.5f,
                                            Animation.RELATIVE_TO_SELF, 0.5f);   
            scale_animation_set.addAnimation(scale_animation);   
            scale_animation.setDuration(1000);//1s鐘   
            image.startAnimation(scale_animation_set);
        }

    }

    private class RotateButtonListener implements OnClickListener{

        public void onClick(View v) {
            // TODO Auto-generated method stub
            AnimationSet rotate_animation_set = new AnimationSet(true);
            //以自身中心為圓心,旋轉360度
            RotateAnimation rotate_animation = new RotateAnimation(0, 360,
                                            Animation.RELATIVE_TO_SELF, 0.5f,
                                            Animation.RELATIVE_TO_SELF, 0.5f);   
            rotate_animation_set.addAnimation(rotate_animation);   
            rotate_animation.setDuration(1000);//1s鐘   
            image.startAnimation(rotate_animation_set);
        }

    }

    private class TranslateButtonListener implements OnClickListener{

        public void onClick(View v) {
            // TODO Auto-generated method stub
            AnimationSet translate_animation_set = new AnimationSet(true);
            //以自身為坐標系和長度單位,從(0,0)移動到(1,1)
            TranslateAnimation translate_animation = new TranslateAnimation(Animation.RELATIVE_TO_SELF,
                                                        0.0f, Animation.RELATIVE_TO_SELF, 1.0f,
                                                        Animation.RELATIVE_TO_SELF, 0.0f,
                                                        Animation.RELATIVE_TO_SELF, 1.0f);   
            translate_animation_set.addAnimation(translate_animation);   
            translate_animation.setDuration(1000);//1s鐘   
            image.startAnimation(translate_animation_set);
        }

    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.activity_main, menu);
        return true;
    }
}


   


本次試驗的xml布局文件代碼如下:

復制代碼 代碼如下:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent" >

    <ImageView
        android:id="@+id/image"
        android:layout_centerHorizontal="true"
        android:paddingTop="80px"
        android:paddingBottom="80px"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:src="@drawable/girl"
        /> 

    <LinearLayout
        android:id="@+id/button_group"
        android:orientation="horizontal"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:gravity="center_horizontal"
        android:layout_below="@id/image"
        >
        <Button
            android:id="@+id/alpha"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_margin="5px"
            android:text="@string/alpha"
            android:ems="1"
            />
        <Button
            android:id="@+id/scale"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_margin="5px"
            android:text="@string/scale"
            android:ems="1"
            />
        <Button
            android:id="@+id/rotate"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_margin="5px"
            android:text="@string/rotate"
            android:ems="1"
            />
        <Button
            android:id="@+id/translate"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_margin="5px"
            android:text="@string/translate"
            android:ems="1"
            />
    </LinearLayout>

</RelativeLayout>



二、在xml文件中使用animation
在xml中使用animation,只需對xml文件中的動畫屬性進行設置,這樣寫好的一個xml文件可以被多次利用。
完成該功能步驟大概如下:
首先在res目錄下新建anim目錄,在anim目錄里面建立xml文件,每個xml文件對應里面可以設置各種動畫,可以將上面講到的4種動畫都放里面。這些xml文件的屬性設置應該都在set標簽下面。
在java語句中,只需要對需要動畫顯示的控件加載一個animation對象,該animation對象采用AnimationUtils.loadAnimation()方法來獲得,該方法里面就有一個參數為anim下的一個xml,因此這個控件也就得到了相應xml里面設置的動畫效果了。
這次試驗的效果和第一種情況的一樣,只是圖片換了一張。
效果演示界面如下:


在用xml設置動態屬性的時候,有些屬性比如旋轉中心,平移尺寸的設置,有如下規則:
如果android:pivotX=”N”,則表示絕對坐標比例,即屏幕的坐標比例。
如果android:pivotX=”N%”,則表示相對自身的坐標比例。
如果android:pivotX=”N%p”,則表示相對于父控件的坐標比例。

實驗代碼如下(附錄有工程code下載鏈接):

MainActivity.java:

復制代碼 代碼如下:

package com.example.anim_2;

import android.app.Activity;
import android.view.View;
import android.view.View.OnClickListener;
import android.os.Bundle;
import android.view.Menu;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.widget.Button;
import android.widget.ImageView;

public class MainActivity extends Activity {

    private ImageView image  = null;
    private Button alpha = null;
    private Button scale = null;
    private Button rotate = null;
    private Button translate = null;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        image = (ImageView)findViewById(R.id.image);
        alpha = (Button)findViewById(R.id.alpha);
        scale = (Button)findViewById(R.id.scale);
        rotate = (Button)findViewById(R.id.rotate);
        translate = (Button)findViewById(R.id.translate);

        alpha.setOnClickListener(new AlphaButtonListener());
        scale.setOnClickListener(new ScaleButtonListener());
        rotate.setOnClickListener(new RotateButtonListener());
        translate.setOnClickListener(new TranslateButtonListener());

    }

    private class AlphaButtonListener implements OnClickListener{

        public void onClick(View v) {
            // TODO Auto-generated method stub
            Animation animation = AnimationUtils.loadAnimation(MainActivity.this, R.anim.alpha);
            image.startAnimation(animation);
        }

    }

    private class ScaleButtonListener implements OnClickListener{

        public void onClick(View v) {
            Animation animation = AnimationUtils.loadAnimation(MainActivity.this, R.anim.scale);
            image.startAnimation(animation);
        }

    }

    private class RotateButtonListener implements OnClickListener{

        public void onClick(View v) {
            Animation animation = AnimationUtils.loadAnimation(MainActivity.this, R.anim.rotate);
            image.startAnimation(animation);
        }

    }

    private class TranslateButtonListener implements OnClickListener{

        public void onClick(View v) {
            Animation animation = AnimationUtils.loadAnimation(MainActivity.this, R.anim.translate);
            image.startAnimation(animation);
        }

    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.activity_main, menu);
        return true;
    }
}



activity_main.xml:
復制代碼 代碼如下:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    >

    <LinearLayout
        android:id="@+id/image_layout"
        android:layout_width="fill_parent"
        android:layout_height="250dip"
        android:gravity="center"
        >
        <ImageView
            android:id="@+id/image"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:src="@drawable/olympic"
            />          
    </LinearLayout>

    <LinearLayout
        android:id="@+id/button_group"
        android:orientation="horizontal"
        android:gravity="center_horizontal"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:layout_below="@id/image_layout"
        >
        <Button
            android:id="@+id/alpha"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_margin="5dip"
            android:text="@string/alpha"
            android:ems="1"
            />
        <Button
            android:id="@+id/scale"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_margin="5dip"
            android:text="@string/scale"
            android:ems="1"
            />
        <Button
            android:id="@+id/rotate"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_margin="5dip"
            android:text="@string/rotate"
            android:ems="1"
            />
        <Button
            android:id="@+id/translate"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_margin="5dip"
            android:text="@string/translate"
            android:ems="1"
            />
    </LinearLayout>

</LinearLayout>



anim/alpha.xml:
復制代碼 代碼如下:

set
    android:interpolator="@android:anim/accelerate_interpolator"
    xmlns:android="http://schemas.android.com/apk/res/android">
    <alpha
        android:fromAlpha="1.0"
        android:toAlpha="0.0"
        android:duration="1000"
        />  
</set>


anim/scale.xml:
復制代碼 代碼如下:

<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android"
    android:interpolator="@android:anim/accelerate_interpolator">
    <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>



anim/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="1000"
        />

</set>



anim/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="0%"
        android:toXDelta="100%"
        android:fromYDelta="0%"
        android:toYDelta="100%"
        android:duration="1000"
        />
</set>


 
總結:本次實驗主要講android中animation這個類對控件的動畫效果設置,對用java代碼和用xml代碼2種方式都做了個簡單的實驗。懂得其使用方法的大體流程。

作者:tornadomeet

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
亚洲香蕉成人av网站在线观看_欧美精品成人91久久久久久久_久久久久久久久久久亚洲_热久久视久久精品18亚洲精品_国产精自产拍久久久久久_亚洲色图国产精品_91精品国产网站_中文字幕欧美日韩精品_国产精品久久久久久亚洲调教_国产精品久久一区_性夜试看影院91社区_97在线观看视频国产_68精品久久久久久欧美_欧美精品在线观看_国产精品一区二区久久精品_欧美老女人bb
456亚洲影院| 国产91精品青草社区| 欧美激情一二三| 亚洲最新在线视频| 日韩成人在线视频| 欧美国产日韩精品| 欧美性jizz18性欧美| 全亚洲最色的网站在线观看| 欧美精品www| 亚洲精品综合精品自拍| 亚洲精品资源美女情侣酒店| 亚洲精品国产拍免费91在线| 久久精品视频在线播放| 亚洲成av人片在线观看香蕉| 久久久久久国产精品久久| 欧美激情videoshd| 欧美丝袜美女中出在线| 97热精品视频官网| 久久91精品国产91久久跳| 成人美女av在线直播| 亚洲www永久成人夜色| 亚洲午夜未满十八勿入免费观看全集| 亚洲精品成a人在线观看| 成人免费观看49www在线观看| 久久在线视频在线| 日韩av片免费在线观看| 精品偷拍各种wc美女嘘嘘| 亚洲一区二区三区在线视频| 日韩网站免费观看| 亚洲韩国青草视频| 综合久久五月天| 久久精品免费电影| 欧美性视频在线| 日韩一区二区av| 国产视频精品在线| 成人做爰www免费看视频网站| 国产亚洲在线播放| 亚洲激情国产精品| 国产精品直播网红| 欧美性极品xxxx做受| 久热精品在线视频| 色婷婷av一区二区三区在线观看| 九九热这里只有精品免费看| 欧美激情综合色| 亚洲欧美国产高清va在线播| 97在线观看免费高清| 久久高清视频免费| 中文字幕国产日韩| 国产精品视频地址| 国产一区二区三区在线视频| 在线精品视频视频中文字幕| 国产精品99久久久久久www| 欧美在线视频免费| 久久99国产综合精品女同| 欧美色欧美亚洲高清在线视频| 久久久这里只有精品视频| 日韩精品电影网| 国产在线精品自拍| 91精品国产高清自在线| 亚洲人高潮女人毛茸茸| 欧美日本啪啪无遮挡网站| 亚洲va欧美va国产综合久久| 久久人人爽人人爽人人片av高请| 国产精品久在线观看| 美女999久久久精品视频| 久久久久久久久久久成人| 热99在线视频| 亚洲性av网站| 亚洲香蕉伊综合在人在线视看| 亚洲国模精品私拍| 狠狠躁夜夜躁久久躁别揉| 亚洲综合日韩在线| 中文字幕日本欧美| 北条麻妃在线一区二区| 中文字幕免费精品一区| 久久夜精品va视频免费观看| 国产精品丝袜白浆摸在线| 国产亚洲欧美日韩美女| 97涩涩爰在线观看亚洲| 日韩av影视在线| 亚洲第一在线视频| 国产精品一区二区在线| 欧美精品在线观看| 91丝袜美腿美女视频网站| 91精品免费久久久久久久久| 亚洲成人性视频| 97香蕉久久夜色精品国产| 久久久久久久久久久av| 日韩av免费看网站| 亚洲性视频网址| 国内精品小视频| 国产91在线播放九色快色| 国产精品户外野外| 成人国产精品日本在线| 亚洲精品99久久久久| 国产一区二区三区在线播放免费观看| 日韩中文在线视频| 91久久精品在线| 国产精品视频精品视频| 91美女福利视频高清| 亚洲精品国产精品国自产观看浪潮| 97精品国产97久久久久久春色| 海角国产乱辈乱精品视频| 美日韩在线视频| 最近中文字幕mv在线一区二区三区四区| 亚洲精品资源美女情侣酒店| 久久久久久久香蕉网| 国语自产精品视频在线看一大j8| 亚洲第一精品电影| 国产精品一区二区性色av| 久久久久久噜噜噜久久久精品| 97成人超碰免| 国产精品电影观看| 欧美一级免费视频| 91免费在线视频网站| 欧美日韩在线视频一区| 欧美限制级电影在线观看| 亚洲成在人线av| 亚洲永久免费观看| 国产精品色视频| 国产成人精品视| 欧美中文在线观看| 日韩欧美在线国产| 亚洲精品电影网在线观看| 国产精品久久久久久久久粉嫩av| 伊人激情综合网| 色偷偷91综合久久噜噜| 国产午夜精品全部视频播放| 97人人爽人人喊人人模波多| 久久久久久久一区二区三区| 57pao精品| 国产亚洲欧洲高清| 亚洲qvod图片区电影| 成人春色激情网| 中文国产亚洲喷潮| 日韩一区二区久久久| 日韩欧美亚洲综合| 亚洲成年人在线| 国产九九精品视频| 日韩中文娱乐网| 日韩精品欧美国产精品忘忧草| 欧美亚洲另类制服自拍| 国产日韩换脸av一区在线观看| 欧美亚洲日本黄色| 国产精品视频精品视频| 91久久国产精品91久久性色| 一区二区三区四区在线观看视频| 欧美精品18videosex性欧美| 欧美多人爱爱视频网站| 激情成人中文字幕| 中日韩美女免费视频网站在线观看| 久久精品亚洲热| 亚州国产精品久久久| 亚洲一区二区国产| 欧美日韩日本国产| 日韩av网址在线| 国产精品久久激情| 性色av一区二区三区| 5566成人精品视频免费| 国产亚洲激情在线| 高清日韩电视剧大全免费播放在线观看| 亚洲白虎美女被爆操| 色噜噜亚洲精品中文字幕| 国产精品电影久久久久电影网|