Android動畫Animation之Tween用代碼實現動畫


 

 

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

 

 

package com.javen.tween;

import android.annotation.SuppressLint;
import android.annotation.TargetApi;
import android.app.Activity;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Matrix;
import android.os.Build;
import android.os.Bundle;
import android.view.View;
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;
    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);//開始播放   
    }  

}

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM