【问题标题】:Animation Translate using LayoutParams使用 LayoutParams 进行动画翻译
【发布时间】:2026-02-22 19:50:01
【问题描述】:

基本上我想创建翻译动画,但由于某种原因我不能使用 TranslateAnimation 类。无论如何,这是我的代码

main.xml

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"      
  android:id="@+id/layout"   
  android:orientation="vertical"     
  android:layout_width="fill_parent"   
  android:layout_height="fill_parent">   
  <ImageView
    android:id="@+id/image"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:src="@drawable/ic_launcher"/> 
</RelativeLayout>

MainActivity.java

package com.test2;

import android.app.Activity; 
import android.os.Bundle; 
import android.view.View; 
import android.view.View.OnClickListener;
import android.widget.RelativeLayout; 
import android.widget.RelativeLayout.LayoutParams; 
import android.widget.ImageView;

public class MainActivity extends Activity 
{   

  private RelativeLayout layout;   
  private ImageView image;   
  private LayoutParams imageParams;

  @Override   
  public void onCreate(Bundle savedInstanceState)   
  {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);
    layout = (RelativeLayout) findViewById(R.id.layout);
    image = (ImageView) findViewById(R.id.image);
    imageParams = new LayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT);
    image.setLayoutParams(imageParams);
    layout.setOnClickListener(new ClickHandler());   
  }

  class ClickHandler implements OnClickListener   
  {
    public void onClick(View view)
    {
      // This is the animation part
      for (int i = 0; i < 100; i++)
      {
        imageParams.leftMargin = i;
        image.setLayoutParams(imageParams);
        // repaint() in java
        try
        {
          Thread.sleep(100);
        }
        catch (Exception e)
        {

        }
      }
    }   
  }
}

我的问题是我需要在使用 Thread.sleep 之前执行 repaint() 否则动画将无法正常工作,并且对象只会移动到其最终位置。我尝试过无效、requestLayout、requestDrawableState,但都没有。有什么建议吗?

编辑:

main.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
  android:id="@+id/layout"
  android:orientation="vertical"
  android:layout_width="fill_parent"
  android:layout_height="fill_parent">
</RelativeLayout>

MainActivity.java

public class MainActivity extends Activity
{
  private RelativeLayout layout;
  private ImageView image;
  private LayoutParams imageParams;
  private Timer timer;
  private int i;

  @Override
  public void onCreate(Bundle savedInstanceState)
  {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);
    layout = (RelativeLayout) findViewById(R.id.layout);
    image = new ImageView(this);
    image.setImageResource(R.drawable.ic_launcher);
    imageParams = new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
    image.setLayoutParams(imageParams);
    layout.addView(image);
    i = 0;
    timer = new Timer();
    runOnUiThread
    (
      new Runnable()
      {
        public void run()
        {
          timer.scheduleAtFixedRate
          (
            new TimerTask()
            {
              @Override
              public void run()
              {
                imageParams.leftMargin = i;
                image.setLayoutParams(imageParams);
                i++;
                if (i == 15)
                {
                  timer.cancel();
                  timer.purge();
                }
              }
            }, 0, 100
          );
        }
      }
    );
  }
}

【问题讨论】:

  • 你在代码中哪里使用了动画?
  • 我想通过添加 leftMargin 和循环来创建动画,使图像看起来像在移动

标签: android translate-animation


【解决方案1】:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/main"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity" >
</RelativeLayout>


package com.example.example;

import java.util.Timer;
import java.util.TimerTask;

import android.os.Bundle;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.app.Activity;

public class MainActivity extends Activity {


    ImageView imageView;
    RelativeLayout.LayoutParams params;
    int i=0;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);


        imageView=new ImageView(this);
        RelativeLayout main=(RelativeLayout)findViewById(R.id.main);

        params=new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT,RelativeLayout.LayoutParams.WRAP_CONTENT);
        imageView.setBackgroundResource(R.drawable.ic_launcher);
        imageView.setLayoutParams(params);
        main.addView(imageView);
        final Timer timer=new Timer();
        timer.scheduleAtFixedRate(new TimerTask() {

            @Override
            public void run() {
                runOnUiThread(new Runnable() {
                    public void run() {
                        params.leftMargin=i;
                        imageView.setLayoutParams(params);
                        i++;
                        if(i>=100){
                            timer.cancel();
                            timer.purge();
                        }
                    }
                });

            }
        }, 0, 100);


    }


}

【讨论】:

    【解决方案2】:
    you can use timer task instead of sleep
    Like:-
    int i=0;
    Timer timer=new Timer();
            timer.scheduleAtFixedRate(new TimerTask() {
    
                @Override
                public void run() {
                    // TODO Auto-generated method stub
     imageParams.leftMargin = i;
            image.setLayoutParams(imageParams);
                    i++;
                    if(i==100){
                        timer.cancel();
                        timer.purge();
                    }
                }
            }, 0, 1000);
    

    【讨论】:

    • 感谢您的回复。但我猜 run() 方法中的 image.setLayoutParams(imageParams) 会以某种方式导致运行时错误
    • 你可以使用 runOnUiThread( new Runnable() { public void run() { } });避免错误
    • 尝试以编程方式添加图像视图而不是通过 xml 这将起作用
    • 我以编程方式添加了 imageview,但仍然出现错误。也许我应该更新我的帖子,这样你就可以看到我的代码是否错误..
    • schemas.android.com/apk/res/android" xmlns:tools="schemas.android.com/tools" android:id="@+id/main" android:layout_width="match_parent" android:layout_height= “match_parent”工具:context=".MainActivity" >