【问题标题】:Animating a TextView height increasing动画 TextView 高度增加
【发布时间】:2015-03-13 02:50:55
【问题描述】:

有人可以发布一个简单的示例,说明如何将高度从 0dp 增长到动态值的 TextView 设置动画?

我想增加文本视图的高度,但允许用户像动画一样看到它。唯一的条件是我知道我的最大高度是可变的,并且在运行时不知道。我想允许的最大值是 100dp。

我尝试过执行一个简单的 for 循环,例如:

runOnUiThread(new Runnable() {
    public void run() {
        LayoutParams params = myTextView.getLayoutParams();
        int myMaxHeightValue = 100;
        for (int x=0; x<myMaxHeightValue; x++) {
            params.height = getDensityPixels(x);
            myTextView.setLayoutParams(params);
            Thread.Sleep(300);
        }
    }
});

public void getDensityPixels(int value) {
    float density = getResources().getDisplayMetrics.display;
    return (int) (value * density + 0.5f);
}

谁能提供一个简单的例子,我可以提供 MAX 高度并观察 textview 从 0dp 高度增长到 MAX dp 高度?当我使用上面的代码时,它确实增加到了 100dp 的最大高度,但是屏幕在调整大小时锁定,而不是在它发生时向我显示增加。

非常感谢任何帮助!

【问题讨论】:

  • 向我解释为什么投反对票?现在不赞成提出问题?
  • 试试这个:stackoverflow.com/a/6027569/4224337,否则你可能需要创建一个自定义的 TextView 类并使用 onMeasure() 方法
  • 您在工作时阻塞了 UI 线程,因此屏幕冻结。尝试在 AsyncTask 中工作并从 onProgressUpdate 方法更新 UI。

标签: android android-layout android-animation textview


【解决方案1】:

永远不要使用线程来执行 UI/View 动画。 Android 提供了很多动画选项,比如ObjectAnimator, ValueAnimator, View Animator

一个例子,根据您的要求。

    private void animateHeight() {
        TextView myTextView=null;
        int maxInDp = 100;
        int maxInPx = (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP,
                maxInDp, mLauncher.getResources().getDisplayMetrics());
        ObjectAnimator.ofInt(this, "btnHeight", maxInPx).setDuration(300).start(); // for better animation change duration , and set interpolator.
    }

    public int btnHeight = 0;

    public int getBtnHeight() {
        return btnHeight;
    }

    public void setBtnHeight(int height) {
        btnHeight = height;
        LayoutParams params = myTextView.getLayoutParams();
        params.height = btnHeight;
        myTextView.setLayoutParams(params);
    }

【讨论】:

  • 您能否详细说明我将如何称呼它?我更新了代码以使用我的 TextView,并将 setDuration 更新为 (1000),这允许更慢的动画。但是,当您说更好的动画更改持续时间和设置插值器时,您是什么意思?
  • 我的意思是,根据您的要求更改持续时间和插值。要启动动画,请致电animateHeight()
  • 那么你什么时候打电话给setBtnHeight()
【解决方案2】:

您通过在主线程上运行工作线程来锁定主线程。要在后台工作时更新 UI,请使用 AsyncTask。 This tutorial 解释了如何做得很好。

基本上,您的 AsyncTask 看起来像这样(上面教程中的示例)

public class DoSomethingTask extends AsyncTask<Void, String, Void> {

  private static final String TAG = "DoSomethingTask";
  private static final int DELAY = 5000; // 5 seconds
  private static final int RANDOM_MULTIPLIER = 10;

  @Override
  protected void onPreExecute() {
   Log.v(TAG, "starting the Random Number Task");
   super.onPreExecute();
  }

  @Override
  protected void onProgressUpdate(String... values) {

  //HERE is where you will be doing your UI updates

   Log.v(TAG, "reporting back from the Random Number Task");
   updateResults(values[0].toString());
   super.onProgressUpdate(values);
  }

  @Override
  protected Void doInBackground(Void... params) {

  //HERE is where you will be doing your work (your loop)

   Log.v(TAG, "doing work in Random Number Task");
   String text="";
   while (true) {
    if (isCancelled()) {
     break;
    }
    int randNum = (int) (Math.random() * RANDOM_MULTIPLIER);
    text = String.format(getString(R.string.service_msg), randNum);

    //This is how you tell your thread to update the UI
    publishProgress(text);

    try {
     Thread.sleep(DELAY);
    } catch (InterruptedException e) {
     Log.v(TAG, "Interrupting the Random Number Task");
    }
   }
   return null;
  }
}

【讨论】:

  • 也只是想说谢谢您的回复。您是绝对正确的,这就是屏幕锁定直到循环完成的原因。也谢谢你的帮助!!
猜你喜欢
  • 2015-10-08
  • 2016-12-06
  • 2021-08-16
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-12-26
  • 1970-01-01
相关资源
最近更新 更多