Android 设置文本视图高度增加的动画
有人可以发布一个简单的例子,说明如何将文本视图的高度从0dp增加到动态值 我想增加textview的高度,但允许用户像看到动画一样看到它。唯一的条件是我知道我的最大高度是可变的,在运行时不知道。我想允许的最大值是100dp 我尝试过执行一个简单的for循环,例如:Android 设置文本视图高度增加的动画,android,android-layout,android-animation,textview,Android,Android Layout,Android Animation,Textview,有人可以发布一个简单的例子,说明如何将文本视图的高度从0dp增加到动态值 我想增加textview的高度,但允许用户像看到动画一样看到它。唯一的条件是我知道我的最大高度是可变的,在运行时不知道。我想允许的最大值是100dp 我尝试过执行一个简单的for循环,例如: runOnUiThread(new Runnable() { public void run() { LayoutParams params = myTextView.getLayoutParams();
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);
}
runOnUiThread(新的Runnable(){
公开募捐{
LayoutParams params=myTextView.getLayoutParams();
int myMaxHeightValue=100;
对于(int x=0;x您通过在主线程上运行工作线程来锁定主线程。若要在后台工作时更新UI,请使用AsyncTask。介绍了如何很好地执行此操作
基本上,您的AsyncTask看起来像这样(上面教程中的示例)
public类DoSomethingTask扩展异步任务{
私有静态最终字符串TAG=“DoSomethingTask”;
专用静态最终整数延迟=5000;//5秒
私有静态最终int随机_乘数=10;
@凌驾
受保护的void onPreExecute(){
Log.v(标记“启动随机数任务”);
super.onPreExecute();
}
@凌驾
受保护的void onProgressUpdate(字符串…值){
//这里是您将要进行UI更新的地方
Log.v(标签,“从随机数任务返回报告”);
updateResults(值[0].toString());
super.onProgressUpdate(值);
}
@凌驾
受保护的Void doInBackground(Void…参数){
//这里是你将要做你的工作(你的循环)的地方
Log.v(标签“在随机数任务中工作”);
字符串文本=”;
while(true){
如果(isCancelled()){
打破
}
int randNum=(int)(Math.random()*随机乘法器);
text=String.format(getString(R.String.service_msg),randNum);
//这就是告诉线程更新UI的方式
出版进度(文本);
试一试{
睡眠(延迟);
}捕捉(中断异常e){
Log.v(标记“中断随机数任务”);
}
}
返回null;
}
}
通过在主线程上运行工作线程来锁定主线程。若要在后台工作时更新UI,请使用AsyncTask。说明如何很好地执行此操作
基本上,您的AsyncTask看起来像这样(上面教程中的示例)
public类DoSomethingTask扩展异步任务{
私有静态最终字符串TAG=“DoSomethingTask”;
专用静态最终整数延迟=5000;//5秒
私有静态最终int随机_乘数=10;
@凌驾
受保护的void onPreExecute(){
Log.v(标记“启动随机数任务”);
super.onPreExecute();
}
@凌驾
受保护的void onProgressUpdate(字符串…值){
//这里是您将要进行UI更新的地方
Log.v(标签,“从随机数任务返回报告”);
updateResults(值[0].toString());
super.onProgressUpdate(值);
}
@凌驾
受保护的Void doInBackground(Void…参数){
//这里是你将要做你的工作(你的循环)的地方
Log.v(标签“在随机数任务中工作”);
字符串文本=”;
while(true){
如果(isCancelled()){
打破
}
int randNum=(int)(Math.random()*随机乘法器);
text=String.format(getString(R.String.service_msg),randNum);
//这就是告诉线程更新UI的方式
出版进度(文本);
试一试{
睡眠(延迟);
}捕捉(中断异常e){
Log.v(标记“中断随机数任务”);
}
}
返回null;
}
}
通过在主线程上运行工作线程来锁定主线程。若要在后台工作时更新UI,请使用AsyncTask。说明如何很好地执行此操作
基本上,您的AsyncTask看起来像这样(上面教程中的示例)
public类DoSomethingTask扩展异步任务{
私有静态最终字符串TAG=“DoSomethingTask”;
专用静态最终整数延迟=5000;//5秒
私有静态最终int随机_乘数=10;
@凌驾
受保护的void onPreExecute(){
Log.v(标记“启动随机数任务”);
super.onPreExecute();
}
@凌驾
受保护的void onProgressUpdate(字符串…值){
//这里是您将要进行UI更新的地方
Log.v(标签,“从随机数任务返回报告”);
updateResults(值[0].toString());
super.onProgressUpdate(值);
}
@凌驾
受保护的Void doInBackground(Void…参数){
//这里是你将要做你的工作(你的循环)的地方
Log.v(标签“在随机数任务中工作”);
字符串文本=”;
while(true){
如果(isCancelled()){
打破
}
int randNum=(int)(Math.random()*随机乘法器);
text=String.format(getString(R.String.service_msg),randNum);
//这就是告诉线程更新UI的方式
出版进度(文本);
试一试{
睡眠(延迟);
}捕捉(中断异常e){
Log.v(标记“中断随机数任务”);
}
}
返回null;
}
}
通过在主线程上运行工作线程来锁定主线程。若要在后台工作时更新UI,请使用AsyncTask。说明如何很好地执行此操作
基本上,您的AsyncTask看起来像这样(上面教程中的示例)
public类DoSomethingTask扩展异步任务{
私有静态最终字符串TAG=“DoSomethingTask”;
专用静态最终整数延迟=5000;//5秒
私有静态最终int随机_乘数=10;
@凌驾
受保护的void onPreExecute(){
Log.v(标记“启动随机数任务”);
super.onPreExecute();
}
@凌驾
受保护的void onProgressUpdate(字符串…值){
//这里是您将要进行UI更新的地方
Log.v(标签,“从随机数任务返回报告”);
updateResults(值[0].toString());
超级进步
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;
}
}
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);
}