动画使用LayoutParams进行翻译

时间:2013-12-26 07:14:45

标签: android translate-animation

基本上我想创建翻译动画但由于某种原因我无法使用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(),否则动画将无法工作,对象将只移动到其最终位置。我已经尝试了invalidate,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
          );
        }
      }
    );
  }
}

2 个答案:

答案 0 :(得分:0)

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


    }


}

答案 1 :(得分:-1)

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