package org.mazhuang.animatedemo;

import android.annotation.TargetApi;
import android.app.Activity;
import android.os.Bundle;
import android.os.SystemClock;
import android.util.Log;
import android.util.TypedValue;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.RelativeLayout;
import android.widget.TextView;

public class LayoutScaleActivity extends Activity {
    
    private static final String TAG = "LayoutScaleActivity";
    
    private RelativeLayout mTitlePanel;
    private TextView mHelloText;
    private Button mScaleBtn;
    private ScaleRunnable mScaleRunnable;
    
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_layout_scale);
        
        mScaleRunnable = new ScaleRunnable();
        
        mHelloText = (TextView)findViewById(R.id.hello_textview);
        
        mTitlePanel = (RelativeLayout)findViewById(R.id.layout_title_panel);
        
        mScaleBtn = (Button)findViewById(R.id.btn_scale);
        mScaleBtn.setOnClickListener(new OnClickListener() {
            private boolean isScaled = false;
            @Override
            public void onClick(View v) {
                if (!mScaleRunnable.isFinished) {
                    return;
                }
                
                if (!isScaled) {
                    isScaled = true;
                    mScaleRunnable.startAnimation(0.5f, 100L);
                } else {
                    isScaled = false;
                    mScaleRunnable.startAnimation(2.0f, 100L);
                }
            }
        });
    }
    
    
    class ScaleRunnable implements Runnable {
        long mDuration;
        boolean isFinished = true;
        float mFactor;
        long mStartTime;
        float mBaseHeight;
        float mBaseTextSize;
        
        @TargetApi(11)
        public void run() {
            ViewGroup.LayoutParams lp = mTitlePanel.getLayoutParams();
            if ((!this.isFinished)) {
                float f1 = ((float) SystemClock.currentThreadTimeMillis() - (float) this.mStartTime)
                        / (float) this.mDuration;
                float factor = 1.0f - f1 * (1.0f - mFactor);
                lp.height = (int)(mBaseHeight * factor);
                if ((mFactor < 1.0f && factor > mFactor) || (mFactor >= 1.0f && factor < mFactor)) {
                    mTitlePanel.setLayoutParams(lp);
                    Log.d(TAG, "" + mHelloText.getText() + " " + mHelloText.getTextSize() * factor );
                    mHelloText.setTextSize(mBaseTextSize * factor / TypedValue.COMPLEX_UNIT_SP);
                    mTitlePanel.post(this);
                    return;
                }
                this.isFinished = true;
            }
        }
        
        public void startAnimation(float factor, long duration) {
            this.mStartTime = SystemClock.currentThreadTimeMillis();
            this.mDuration = duration;
            this.mFactor = factor;
            this.isFinished = false;
            this.mBaseHeight = mTitlePanel.getLayoutParams().height;
            this.mBaseTextSize = mHelloText.getTextSize();
            mTitlePanel.post(this);
        }
    }
}

@@ -155,33 +155,8 @@
 ck;%0A
-import android.util.Log;%0A
 impo
@@ -444,74 +444,8 @@
    %0A
-    private static final String TAG = %22LayoutScaleActivity%22;%0A    %0A
     
@@ -2257,68 +2257,13 @@
     
-lp.height = (int)(mBaseHeight * factor);%0A                
 if 
+(!
 ((mF
@@ -2337,16 +2337,17 @@
 Factor))
+)
  %7B%0A     
@@ -2361,43 +2361,66 @@
     
-    mTitlePanel.setLayoutParams(lp)
+%09this.isFinished = true;%0A                %09factor = mFactor
 ;%0A  
@@ -2434,84 +2434,75 @@
     
+   %7D%0A
        
-Log.d(TAG, %22%22 + mHelloText.getText() + %22 %22 + mHelloText.getTextSize()
+         %0A                lp.height = (int)(mBaseHeight
  * f
@@ -2506,17 +2506,16 @@
 * factor
- 
 );%0A     
@@ -2508,32 +2508,74 @@
 factor);%0A       
+         mTitlePanel.setLayoutParams(lp);%0A
              mHe
@@ -2563,32 +2563,35 @@
 );%0A             
+   
 mHelloText.setTe
@@ -2659,36 +2659,32 @@
                 
-    
 mTitlePanel.post
@@ -2695,94 +2695,8 @@
 s);%0A
-                    return;%0A                %7D%0A                this.isFinished = true;%0A
     

fix scale bug