使用一些动画在运行时更改EditText的高度和宽度

时间:2012-02-20 11:35:13

标签: android listadapter

我有四个不同背景的EditText。 它看起来像这样: enter image description here

我想在使用That EditText选择EditText时覆盖全屏。为此,我需要在运行时使用一些动画更改EditText的宽度和高度。

选择时它应如下所示: enter image description here

如何在运行时使用动画更改EditText大小?

3 个答案:

答案 0 :(得分:6)

  

更新答案:

在drawable / 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);

答案 1 :(得分: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);
        }
    });
}

我不知道你是否可以这样做,但在这个例子中你可以很容易地添加一些动作。

建议使用它制作,只要你有其他选择。

答案 2 :(得分: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);
    }  
}