Android 通過Animate寫入程式碼實現簡單的平移、旋轉、縮放及透明度動畫過程

來源:互聯網
上載者:User

以下只列出主要代碼:

[java]
private ImageView scanLight; 
     
    @Override 
    public void onCreate(Bundle savedInstanceState) { 
        super.onCreate(savedInstanceState); 
        setContentView(R.layout.activity_main); 
         
        scanLight = (ImageView)findViewById(R.id.img); 
         
        Button translateStart = (Button)findViewById(R.id.translateStart); 
        Button translateEnd = (Button)findViewById(R.id.translateEnd); 
        Button scaleStart = (Button)findViewById(R.id.scaleStart); 
        Button scaleEnd = (Button)findViewById(R.id.scaleEnd); 
        Button alphaStart = (Button)findViewById(R.id.alphaStart); 
        Button alphaEnd = (Button)findViewById(R.id.alphaEnd); 
        Button rotateStart = (Button)findViewById(R.id.rotateStart); 
        Button rotateEnd = (Button)findViewById(R.id.rotateEnd); 
         
        translateStart.setOnClickListener(listener); 
        translateEnd.setOnClickListener(listener); 
        scaleStart.setOnClickListener(listener); 
        scaleEnd.setOnClickListener(listener); 
        alphaStart.setOnClickListener(listener); 
        alphaEnd.setOnClickListener(listener); 
        rotateStart.setOnClickListener(listener); 
        rotateEnd.setOnClickListener(listener); 
    } 
     
    private OnClickListener listener = new OnClickListener() 
    { 
         
        @Override 
        public void onClick(View v) 
        { 
            switch (v.getId()) 
            { 
            //平移  setFillAfter(true) 控制執行動畫後定在目前狀態  
            case R.id.translateStart: 
                Animation translateIn = new TranslateAnimation(0, 100, 0, 0); 
                translateIn.setDuration(500); 
                translateIn.setFillAfter(true); 
                scanLight.startAnimation(translateIn); 
                break; 
            case R.id.translateEnd: 
                Animation translateOut = new TranslateAnimation(100, 0, 0, 0); 
                translateOut.setDuration(500); 
                translateOut.setFillAfter(true); 
                scanLight.startAnimation(translateOut); 
                break; 
            //縮放  後四個參數控制沿自身中心點縮放   
            case R.id.scaleStart: 
                Animation sIn = new ScaleAnimation(1f, 2f, 1f, 2f, Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF, 0.5f); 
                sIn.setDuration(500); 
                sIn.setFillAfter(true); 
                scanLight.startAnimation(sIn); 
                break; 
            case R.id.scaleEnd: 
                Animation sOut = new ScaleAnimation(2f, 1f, 2f, 1f, Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF, 0.5f); 
                sOut.setDuration(500); 
                sOut.setFillAfter(true); 
                scanLight.startAnimation(sOut); 
                break; 
            //透明度  
            case R.id.alphaStart: 
                Animation aIn = new AlphaAnimation(1f, 0f); 
                aIn.setDuration(500); 
                aIn.setFillAfter(true); 
                scanLight.startAnimation(aIn); 
                break; 
            case R.id.alphaEnd: 
                Animation aOut = new AlphaAnimation(0f, 1f); 
                aOut.setDuration(500); 
                aOut.setFillAfter(true); 
                scanLight.startAnimation(aOut); 
                break; 
            //旋轉  後四個參數控制沿自身中心點旋轉  
            case R.id.rotateStart: 
                Animation rIn = new RotateAnimation(0f, +360f, Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF, 0.5f); 
                rIn.setDuration(500); 
                rIn.setFillAfter(true);  
                scanLight.startAnimation(rIn); 
                break; 
            case R.id.rotateEnd: 
                Animation rOut = new RotateAnimation(+360f, 0f, Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF, 0.5f); 
                rOut.setDuration(500); 
                rOut.setFillAfter(true); 
                scanLight.startAnimation(rOut); 
                break; 
 
            default: 
                break; 
            } 
             
        } 
    }; 

private ImageView scanLight;
 
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
       
        scanLight = (ImageView)findViewById(R.id.img);
       
        Button translateStart = (Button)findViewById(R.id.translateStart);
        Button translateEnd = (Button)findViewById(R.id.translateEnd);
        Button scaleStart = (Button)findViewById(R.id.scaleStart);
        Button scaleEnd = (Button)findViewById(R.id.scaleEnd);
        Button alphaStart = (Button)findViewById(R.id.alphaStart);
        Button alphaEnd = (Button)findViewById(R.id.alphaEnd);
        Button rotateStart = (Button)findViewById(R.id.rotateStart);
        Button rotateEnd = (Button)findViewById(R.id.rotateEnd);
       
        translateStart.setOnClickListener(listener);
        translateEnd.setOnClickListener(listener);
        scaleStart.setOnClickListener(listener);
        scaleEnd.setOnClickListener(listener);
        alphaStart.setOnClickListener(listener);
        alphaEnd.setOnClickListener(listener);
        rotateStart.setOnClickListener(listener);
        rotateEnd.setOnClickListener(listener);
    }
   
    private OnClickListener listener = new OnClickListener()
 {
  
  @Override
  public void onClick(View v)
  {
   switch (v.getId())
   {
   //平移  setFillAfter(true) 控制執行動畫後定在目前狀態
   case R.id.translateStart:
    Animation translateIn = new TranslateAnimation(0, 100, 0, 0);
    translateIn.setDuration(500);
    translateIn.setFillAfter(true);
    scanLight.startAnimation(translateIn);
    break;
   case R.id.translateEnd:
    Animation translateOut = new TranslateAnimation(100, 0, 0, 0);
    translateOut.setDuration(500);
    translateOut.setFillAfter(true);
    scanLight.startAnimation(translateOut);
    break;
   //縮放  後四個參數控制沿自身中心點縮放
   case R.id.scaleStart:
    Animation sIn = new ScaleAnimation(1f, 2f, 1f, 2f, Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF, 0.5f);
    sIn.setDuration(500);
    sIn.setFillAfter(true);
    scanLight.startAnimation(sIn);
    break;
   case R.id.scaleEnd:
    Animation sOut = new ScaleAnimation(2f, 1f, 2f, 1f, Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF, 0.5f);
    sOut.setDuration(500);
    sOut.setFillAfter(true);
    scanLight.startAnimation(sOut);
    break;
   //透明度
   case R.id.alphaStart:
    Animation aIn = new AlphaAnimation(1f, 0f);
    aIn.setDuration(500);
    aIn.setFillAfter(true);
    scanLight.startAnimation(aIn);
    break;
   case R.id.alphaEnd:
    Animation aOut = new AlphaAnimation(0f, 1f);
    aOut.setDuration(500);
    aOut.setFillAfter(true);
    scanLight.startAnimation(aOut);
    break;
   //旋轉  後四個參數控制沿自身中心點旋轉
   case R.id.rotateStart:
    Animation rIn = new RotateAnimation(0f, +360f, Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF, 0.5f);
    rIn.setDuration(500);
    rIn.setFillAfter(true);
    scanLight.startAnimation(rIn);
    break;
   case R.id.rotateEnd:
    Animation rOut = new RotateAnimation(+360f, 0f, Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF, 0.5f);
    rOut.setDuration(500);
    rOut.setFillAfter(true);
    scanLight.startAnimation(rOut);
    break;

   default:
    break;
   }
   
  }
 };


 


相關文章

聯繫我們

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