1

我想让一个TextView像动画一样一点一点的出现。问题是,动画不流畅。它有时会卡住一段时间,然后恢复。有时甚至更糟,它会回去......我的意思是,TextView变得越来越大,但突然变小又变大。有人可以帮我吗?

private class UnfoldTask extends AsyncTask<Integer, Integer, Integer> {

    View view;

    public UnfoldTask(View v) {
        this.view = v;
        ViewGroup.LayoutParams pa = view.getLayoutParams();
        pa.height = 0;
        view.setLayoutParams(pa);
    }

    @Override
    protected Integer doInBackground(Integer... maxHeight) {
        ViewGroup.LayoutParams pa = view.getLayoutParams();
        while (pa.height < maxHeight[0]) {
                pa.height += (int) (24 * getResources().getDisplayMetrics().density + 0.5f);
                sleep(100);
                publishProgress(pa.height);
        }
        return maxHeight[0];
    }

    private void sleep(int i) {
        try {
            Thread.sleep(i);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }

    @Override
    protected void onProgressUpdate(Integer... values) {
        ViewGroup.LayoutParams pa = view.getLayoutParams();
        pa.height = values[0];
        view.setLayoutParams(pa);
    }

    @Override
    protected void onPostExecute(Integer result) {
        ViewGroup.LayoutParams pa = view.getLayoutParams();
        pa.height = result;
        view.setLayoutParams(pa);
    }
}
4

2 回答 2

0

您应该为此使用比例动画。这是一个例子:

ScaleAnimation animation = new ScaleAnimation(1, 2, 1, 2, centerX, centerY); // Scales from normal size (1) to double size (2). centerX/Y is the center of your text view. Change this to set the pivot point of your animation.
animation.setDuration(1000);
myTextView.startAnimation(animation);
于 2013-11-12T02:48:50.520 回答
0

您可以使用droidQuery来简化这一点:

//this will set the height of myView to 0px.
$.with(myView).height(0);

//when you are ready to animate to height (in pixels):
$.with(myView).animate("{height:" + height + "}", new AnimationOptions());

如果您想花哨,请查看文档 - 例如添加持续时间和事件回调。如果您仍然注意到动画不流畅,请考虑将application 属性添加到您的AndroidManifest

android:hardwareAccelerated="true"
于 2013-11-12T04:42:43.960 回答