Android Tween動畫(網上總結)

1.用代碼實現動畫 

透明度動畫、旋轉動畫、尺寸伸縮動畫、移動動畫



public class MainActivity extends Activity {
    private ImageView image;
    private Button but1;
    private Button but2;
    Bitmap bm;
    
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        init();
        but1.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                image.setImageBitmap(rotateToFit(bm, -90f));
                bm = rotateToFit(bm, -90f);
            }
        });
        but2.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                image.setImageBitmap(rotateToFit(bm, 90f));
                bm = rotateToFit(bm, 90f);
            }
        });

        image.setOnClickListener(new View.OnClickListener() {

            @TargetApi(Build.VERSION_CODES.HONEYCOMB)
            @Override
            public void onClick(View v) {
                System.out.println("你點擊了圖片");
                initAnimation();
            }
        });
    }

    @SuppressLint("NewApi")
    private void init() {
        image = (ImageView) findViewById(R.id.image);
        but1 = (Button) findViewById(R.id.but1);
        but2 = (Button) findViewById(R.id.but2);
        but1.setText("向左轉");
        but2.setText("向右轉");
        bm = BitmapFactory.decodeResource(getResources(), R.drawable.eee);
        image.setImageBitmap(bm);
        image.setAlpha(alpha);
    }

    public static Bitmap rotateToFit(Bitmap bm, float degrees) {
        int wight = bm.getWidth();
        int height = bm.getHeight();

        Matrix matrix = new Matrix();
        matrix.postRotate(degrees);
        Bitmap resBitmap = Bitmap.createBitmap(bm, 0, 0, wight, height, matrix,
                true);
        return resBitmap;

    }
    private Animation animation_alpha,animation_scale,animation_translate,animation_rotate;  
    private AnimationSet animationSet;
    private void initAnimation() {  
        //透明度控制動畫效果 alpha   
        animation_alpha=new AlphaAnimation(0.1f,1.0f);  
        //第一個參數fromAlpha爲 動畫開始時候透明度   
        //第二個參數toAlpha爲 動畫結束時候透明度   
        animation_alpha.setRepeatCount(0);//設置循環   
        animation_alpha.setDuration(5000);//設置時間持續時間爲 5000毫秒   
          
        // 旋轉效果rotate   
        animation_rotate = new RotateAnimation(0, -720,  
                RotateAnimation.RELATIVE_TO_SELF, 0.5f,  
                RotateAnimation.RELATIVE_TO_SELF, 0.5f);  
          //第一個參數fromDegrees爲動畫起始時的旋轉角度 
          //第二個參數toDegrees爲動畫旋轉到的角度   
          //第三個參數pivotXType爲動畫在X軸相對於物件位置類型
          //第四個參數pivotXValue爲動畫相對於物件的X座標的開始位置   
          //第五個參數pivotXType爲動畫在Y軸相對於物件位置類型 
          //第六個參數pivotYValue爲動畫相對於物件的Y座標的開始位置   
        animation_rotate.setRepeatCount(0);  
        animation_rotate.setDuration(5000);//設置時間持續時間爲 5000毫秒   
          
        //尺寸伸縮動畫效果 scale   
        animation_scale=new ScaleAnimation(0.1f,3.0f,0.1f,3.0f,Animation.RELATIVE_TO_SELF,0.5f,Animation.RELATIVE_TO_SELF,0.5f);  
        //第一個參數fromX爲動畫起始時 X座標上的伸縮尺寸       
        //第二個參數toX爲動畫結束時 X座標上的伸縮尺寸        
        //第三個參數fromY爲動畫起始時Y座標上的伸縮尺寸       
        //第四個參數toY爲動畫結束時Y座標上的伸縮尺寸     
        /*說明: 
                            以上四種屬性值     
              0.0表示收縮到沒有  
              1.0表示正常無伸縮      
                           值小於1.0表示收縮   
                           值大於1.0表示放大 
        */  
        //第五個參數pivotXType爲動畫在X軸相對於物件位置類型     
        //第六個參數pivotXValue爲動畫相對於物件的X座標的開始位置   
        //第七個參數pivotXType爲動畫在Y軸相對於物件位置類型      
        //第八個參數pivotYValue爲動畫相對於物件的Y座標的開始位置   
        animation_scale.setRepeatCount(0);  
        animation_scale.setDuration(5000);//設置時間持續時間爲 5000毫秒   
          
        //移動動畫效果translate   
        animation_translate=new TranslateAnimation(-20f,300f,-20f,300f);  
        //第一個參數fromXDelta爲動畫起始時 X座標上的移動位置       
        //第二個參數toXDelta爲動畫結束時 X座標上的移動位置         
        //第三個參數fromYDelta爲動畫起始時Y座標上的移動位置    
        //第三個參數toYDelta爲動畫結束時Y座標上的移動位置    
        animation_translate.setRepeatCount(0);//設置動畫執行多少次,如果是-1的話就是一直重複   
        animation_translate.setDuration(5000);//設置時間持續時間爲 5000毫秒   
          
        animationSet=new AnimationSet(true);  
          
        animationSet.addAnimation(animation_alpha);//透明度   
        animationSet.addAnimation(animation_rotate);//旋轉   
        animationSet.addAnimation(animation_scale);//尺寸伸縮   
        animationSet.addAnimation(animation_translate);//移動   
        image.startAnimation(animationSet);//開始播放   
    }  

}


- setFillAfter方法的作用是設置一個動畫效果執行完畢後,View對象是否保留在終止的位置
- 若我們需要將真實組件最終移動到動畫結束的位置,則需要在動畫結束的回調函數內,調用組件的 clearAnimation接口,來清除動畫過程中對某些屬性的修改
- 在調用 clearAnimation接口後,利用layout接口可改變組件的真實位置
- 若調用layout之前不調用 clearAnimation接口: setFillAfter爲flase時,會出現跳閃的問題, setFillAfter爲true時,會出現組件偏離正確位置的問題

例子:

//上下箭頭動畫
                RotateAnimation animation_rotate = new RotateAnimation(0, 180,
                        RotateAnimation.RELATIVE_TO_SELF, 0.5f,
                        RotateAnimation.RELATIVE_TO_SELF, 0.5f);
                animation_rotate.setFillAfter(true);
                animation_rotate.setDuration(300);
                animation_rotate.setAnimationListener(new Animation.AnimationListener() {
                    @Override
                    public void onAnimationStart(Animation animation) {

                    }

                    @Override
                    public void onAnimationEnd(Animation animation) {
//                        imgPriceDeatil.clearAnimation();
//                        imgPriceDeatil.setBackgroundResource(R.mipmap.ic_estimate_expand);
                    }

                    @Override
                    public void onAnimationRepeat(Animation animation) {

                    }
                });
                imgPriceDeatil.startAnimation(animation_rotate);



2.用XML文件實現動畫 

例子:

進入動畫(集合)

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

    <alpha
        android:fromAlpha="0.0"
        android:toAlpha="1.0"
        android:duration="300"
        />
</set>

出去的動畫(集合)

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

    <alpha
        android:fromAlpha="1.0"
        android:toAlpha="0.0"
        android:duration="300"
        />
</set>

代碼綁定該xml動畫

Animation outAnim = AnimationUtils.loadAnimation(mContext, R.anim.slide_out_top_left);
                outAnim.setAnimationListener(new Animation.AnimationListener() {
                    @Override
                    public void onAnimationStart(Animation animation) {
                    }

                    @Override
                    public void onAnimationEnd(Animation animation) {
                        llGetCashTip.setVisibility(View.GONE);
                    }

                    @Override
                    public void onAnimationRepeat(Animation animation) {
                    }
                });
                llGetCashTip.startAnimation(outAnim);


XML資源文件的使用可以見:Animation Resources

XML文件放在項目的res/anim/目錄下。文件必須有一個唯一的根節點。

這個根節點可以是:<alpha>, <scale>, <translate>, <rotate>, interpolator element, 或者是<set>。

默認情況下,所有的動畫都是並行進行的,要想使得它們順尋發生,你必須指定startOffset屬性。

 

有一些值,可以指定是相對於View本身還是相對於父類容器的。

比如pivotX,要表示相對於自身的50%,要用50%;要表示相對於父類容器的50%,則直接寫50


例子:

<set xmlns:android="http://schemas.android.com/apk/res/android"
    android:shareInterpolator="false">
    <scale
        android:interpolator="@android:anim/accelerate_decelerate_interpolator"
        android:fromXScale="1.0"
        android:toXScale="1.4"
        android:fromYScale="1.0"
        android:toYScale="0.6"
        android:pivotX="50%"
        android:pivotY="50%"
        android:fillAfter="false"
        android:duration="700" />
    <set
        android:interpolator="@android:anim/accelerate_interpolator"
        android:startOffset="700">
        <scale
            android:fromXScale="1.4"
            android:toXScale="0.0"
            android:fromYScale="0.6"
            android:toYScale="0.0"
            android:pivotX="50%"
            android:pivotY="50%"
            android:duration="400" />
        <rotate
            android:fromDegrees="0"
            android:toDegrees="-45"
            android:toYScale="0.0"
            android:pivotX="50%"
            android:pivotY="50%"
            android:duration="400" />
    </set>
</set>






發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章