无标题文档
人多不足以依赖,要生存只有靠自己。
      深窥自己的心,而后发觉一切的奇迹在你自己。
          凡事皆有终结,因此,耐心是赢得成功的一种手段。

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);//开始播放   
    }  

}

 

posted @ 2013-09-12 21:58  酷玩时刻  阅读(3011)  评论(0编辑  收藏  举报
友情链接:快递查询 快递查询