我们怎样才能慢慢地显示图像并在android中从底部到顶部改变图像的位置?

时间:2011-01-20 11:59:47

标签: android

我们怎么能慢慢地显示图像并在android中从下到上改变图像的位置???我知道转型,但请给我示例代码..

2 个答案:

答案 0 :(得分:0)

最简单的方法是在Android中使用Animation类;这很容易,示例代码可以在参考指南中找到:http://developer.android.com/guide/topics/resources/animation-resource.html#translate-element

您可以通过SurfaceView或使用带有GLSurfaceView的OpenGL ES覆盖onDraw调用的其他方法。

答案 1 :(得分:0)

可能对你有所帮助

包Autodrag.test;

import android.app.Activity; import android.os.Bundle; import android.os.Handler; import android.os.Message; import android.view.Menu; import android.view.MenuItem; import android.widget.ImageView; import android.widget.LinearLayout.LayoutParams;

公共类Autodrag扩展了Activity {

private boolean goRight=false,goDown=false,stop=false;
private ImageView img;
private LayoutParams layoutparms;
private Thread thread;
private Runnable run;
private Handler handler;

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);

   handler=new Handler(){
   @Override
   public void handleMessage(Message message) {
        super.handleMessage(message);

        switch (message.what) {

        case 0:
                Myball(5,5);

                break;
        default:
                break;
    }
 }

};        run = new Runnable(){        @覆盖        public void run(){

       while (!stop) {
            Message message=new Message();  
            message.what=0;
            handler.sendMessage(message);
            try{
                    Thread.sleep(100);
            }catch (Exception e) {
                    Thread.currentThread().interrupt(); 
              }
        }    
} };

}
      @覆盖       public boolean onCreateOptionsMenu(Menu menu){          super.onCreateOptionsMenu(菜单);

        menu.add(0, 0, 0, "Start");
        menu.add(0, 1, 0, "Stop");

        return true;
  }

  @Override
  public boolean onOptionsItemSelected(MenuItem item){

    switch (item.getItemId()) {
      case 0:
             thread = new Thread(run);  
             thread.start();
             stop=false;
             return true;
      case 1:
             stop=true;
             return true;
        }
   return true;
  }       

  private void Myball(int goX,int goY) {

        img=(ImageView)findViewById(R.id.img);
        layoutparms = (LayoutParams) img.getLayoutParams(); 

        if (layoutparms.leftMargin < 1){
            goRight = true;
        }
        if (layoutparms.leftMargin > 180){
            goRight = false;
        }
        if (layoutparms.topMargin < 1){
            goDown = true;
        }
        if (layoutparms.topMargin > 300){
            goDown = false;
        }
        if (goRight){
            layoutparms.leftMargin += goX;
        }else{
            layoutparms.leftMargin -= goX;
        }
        if (goDown){
            layoutparms.topMargin += goY;
        }else{
            layoutparms.topMargin -= goY;
        }
            img.setLayoutParams(layoutparms);
 }

}