2012-02-20 61 views
11

我有四個具有不同背景的EditText。 它看起來像這樣: enter image description here使用某些動畫在運行時更改EditText高度和寬度

我要當與EditText上選擇了的EditText覆蓋整個屏幕。爲此,我需要在運行時使用一些動畫來更改EditText的寬度和高度。

當選擇它應該是這樣的: enter image description here

我怎樣才能改變大小的EditText上運行動畫?

+0

所以,你需要使用動畫使EditText上佔據佈局的填充父請求時,它集中。對? – 2012-03-01 10:56:45

+0

@Raman yes.I've你的答案,但事實並非如此。 – asish 2012-03-01 11:29:42

回答

4

我不確定是否可以用Animations完成。在android視圖在動畫完成之前佔用所有空間,因此您會看到其他editTexts消失並且選擇了一個緩慢增加。下面是粗糙的例子,如何做纔不至於非標準的動畫,但在單獨的線程改變權重:

佈局:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
android:layout_width="fill_parent" 
android:layout_height="fill_parent" 
android:orientation="vertical" > 

<LinearLayout 
    android:id="@+id/ll0" 
    android:layout_width="fill_parent" 
    android:layout_height="0dp" 
    android:layout_weight="1" 
    android:orientation="horizontal" > 

    <EditText 
     android:id="@+id/et00" 
     android:layout_width="0dp" 
     android:layout_height="fill_parent" 
     android:layout_weight="1" 
     android:gravity="top|left" 
     android:text="00" /> 

    <EditText 
     android:id="@+id/et01" 
     android:layout_width="0dp" 
     android:layout_height="fill_parent" 
     android:layout_weight="1" 
     android:gravity="top|left" 
     android:text="01" /> 
</LinearLayout> 

<LinearLayout 
    android:id="@+id/ll1" 
    android:layout_width="fill_parent" 
    android:layout_height="0dp" 
    android:layout_weight="1" 
    android:orientation="horizontal" > 

    <EditText 
     android:id="@+id/et10" 
     android:layout_width="0dp" 
     android:layout_height="fill_parent" 
     android:layout_weight="1" 
     android:gravity="top|left" 
     android:text="10" /> 

    <EditText 
     android:id="@+id/et11" 
     android:layout_width="0dp" 
     android:layout_height="fill_parent" 
     android:layout_weight="1" 
     android:gravity="top|left" 
     android:text="11" /> 
</LinearLayout> 
</LinearLayout> 

,並在代碼中添加關於改變重點行動:

@Override 
public void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.main); 

    OnFocusChangeListener focusListener = new OnFocusChangeListener() { 
     @Override 
     public void onFocusChange(View v, boolean hasFocus) { 
      if (hasFocus) { 
       LinearLayout parent = (LinearLayout) v.getParent(); 
       EditText forDecresing = null; 
       for (int i = 0; i < parent.getChildCount(); i++) { 
        if (parent.getChildAt(i) != v) { 
         forDecresing = (EditText) parent.getChildAt(i); 
         break; 
        } 
       } 
       LinearLayout pp = (LinearLayout) parent.getParent(); 
       LinearLayout layoutForDecreasing = null; 
       for (int i = 0; i < pp.getChildCount(); i++) { 
        if (pp.getChildAt(i) != parent && pp.getChildAt(i) instanceof LinearLayout) { 
         layoutForDecreasing = (LinearLayout) pp.getChildAt(i); 
         break; 
        } 
       } 
       startAnimation((EditText) v, forDecresing, layoutForDecreasing, parent); 
      } else { 
      } 
     } 
    }; 

    ((EditText) findViewById(R.id.et00)).setOnFocusChangeListener(focusListener); 
    ((EditText) findViewById(R.id.et01)).setOnFocusChangeListener(focusListener); 
    ((EditText) findViewById(R.id.et11)).setOnFocusChangeListener(focusListener); 
    ((EditText) findViewById(R.id.et10)).setOnFocusChangeListener(focusListener); 
} 

public void onBackPressed() { 
    setWeight(findViewById(R.id.et00), 1); 
    setWeight(findViewById(R.id.et01), 1); 
    setWeight(findViewById(R.id.et11), 1); 
    setWeight(findViewById(R.id.et10), 1); 
    setWeight(findViewById(R.id.ll1), 1); 
    setWeight(findViewById(R.id.ll0), 1); 
} 

Thread animationThread; 

private void startAnimation(final EditText forIncreasing, final EditText forDecresing, final LinearLayout layoutForDecreasing, 
     final LinearLayout layoutForIncreasing) { 
    if (animationThread != null) 
     animationThread.interrupt(); 
    animationThread = new Thread(new Runnable() { 
     @Override 
     public void run() { 
      int iterations = 0; 
      int maxIterations = 30; 
      setWeight(forIncreasing, maxIterations - 1); 
      setWeight(layoutForIncreasing, maxIterations - 1); 
      setWeight(forDecresing, maxIterations - 1); 
      setWeight(layoutForDecreasing, maxIterations - 1); 
      while (iterations < maxIterations) { 
       iterations++; 
       setWeight(forIncreasing, maxIterations - 1 + iterations); 
       setWeight(layoutForIncreasing, maxIterations - 1 + iterations); 
       setWeight(forDecresing, maxIterations - 1 - iterations); 
       setWeight(layoutForDecreasing, maxIterations - 1 - iterations); 
       try { 
        Thread.sleep(10); 
       } catch (InterruptedException e) { 
        return; 
       } 
      } 
      animationThread = null; 
     } 
    }); 
    animationThread.start(); 
} 

private void setWeight(final View view, final float weight) { 
    runOnUiThread(new Runnable() { 
     @Override 
     public void run() { 
      LayoutParams params = (LayoutParams) view.getLayoutParams(); 
      params.weight = weight; 
      view.setLayoutParams(params); 
     } 
    }); 
} 

我做不知道這對你是否可行,但在這個例子中,你可以添加更多的動作很容易。

我做不是推薦使用它的生產,如果只有你有其他一些選擇。

+0

看到我編輯的答案 – Jin35 2012-03-01 19:19:37

0

我試圖解釋我的邏輯你的問題...我希望因此它會爲你工作..

EditText editText = new EditText(this); 
editText.setOnClickListener(new OnClickListener() { 

    public void onClick(){  
     // Before getSize was introduced (in API level 13), you could use 
     // the getWidth and getHeight methods that are now deprecated: 

     Display display = getWindowManager().getDefaultDisplay(); 
     int width = display.getWidth(); 
     int height = display.getHeight(); 

     editText.setWidth(width); 
     editText.setHeight(height); 
    } 
} 
+1

這就是我認識的親愛的朋友......我很抱歉,我可以幫你改進....但仍然....你嘗試用這個動畫的概念......這是正確的方式在運行時更改大小,也會有助於更改動畫的大小... – 2012-03-01 09:17:33

6

答案更新:

使用此代碼繪製/animation_sample.xml

<?xml version="1.0" encoding="utf-8"?> 
<set xmlns:android="http://schemas.android.com/apk/res/android" android:shareInterpolator="false"> 
<translate android:fromXDelta="0%" android:toXDelta="100%" android:fromYDelta="0%" android:toYDelta="0%" android:duration="500" /> 

並採用設置動畫編輯文本:

Animation anim=AnimationUtils.loadAnimation(this, R.anim.righttoleft); 
editText = (EditText)findViewById(R.id.editText1); 
Display display = getWindowManager().getDefaultDisplay(); 
int width=display.getWidth(); 
int height = display.getHeight(); 
editText.setWidth(width); 
editText.setHeight(height); 

editText.startAnimation(anim);