使用布局参数进行动画翻译



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

主.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>

主要活动.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(),否则动画将无法工作,对象将移动到其最终位置。我已经尝试了无效,请求布局,请求可绘制状态,但没有一个有效。有什么建议吗?

编辑:

主.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>

主要活动.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
          );
        }
      }
    );
  }
}
<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);

    }

}
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);

最新更新