简体   繁体   中英

how can we show the image slowly-slowly and change the place of image from bottom to top in android?

how can we show the image slowly-slowly and change the place of image from bottom to top in android ??? i knw through Transformation but how please give me sample code..

The easiest way is to use the Animation class in Android; it's very easy, sample code can be found in the reference guide here: http://developer.android.com/guide/topics/resources/animation-resource.html#translate-element

Other ways you can do it are with overriding the onDraw call through a SurfaceView or using OpenGL ES with a GLSurfaceView.

may be it helps to you

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

public class Autodrag extends 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() { @Override 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(); 
              }
        }    
} };

}
@Override public boolean onCreateOptionsMenu(Menu menu) { super.onCreateOptionsMenu(menu);

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

}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM