2011-11-09 74 views
1

這是從我的應用程序創建的。在Android中動態地移動圖像

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

    setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE); 

    getWindow().setFormat(PixelFormat.UNKNOWN); 
    surfaceView = (SurfaceView)findViewById(R.id.camerapreview); 
    surfaceHolder = surfaceView.getHolder(); 
    surfaceHolder.addCallback(this); 
    surfaceHolder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS); 

    controlInflater = LayoutInflater.from(getBaseContext()); 
    viewControl = controlInflater.inflate(R.layout.control, null); 
    image =(ImageView) viewControl.findViewById(R.id.img); 
    LayoutParams layoutParamsControl 
     = new LayoutParams(LayoutParams.FILL_PARENT, 
     LayoutParams.FILL_PARENT); 
    this.addContentView(viewControl, layoutParamsControl); 

} 

control.xml

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:orientation="vertical" 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent" 
    android:gravity="center" 
     > 
<ImageView 
    android:id="@+id/img" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:background="@drawable/ic_launcher" 
    /> 

<Button 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:id="@+id/button" 
/> 

</LinearLayout> 

我如何才能將一個按鈕點擊此圖片。 其實我想把這個圖像從當前像素位置轉換到另一個。

我試過這個按鈕點擊。

b =(Button)findViewById(R.id.button); 
     b.setOnClickListener(new OnClickListener() { 
      @Override 
      public void onClick(View v) { 
       System.out.println("Button clicked"); 
       TranslateAnimation anim = new TranslateAnimation(0,0,200,200);    
       anim.setDuration(2000); 
       image.setAnimation(anim); 
      } 
     }); 

但現在圖像將消失兩秒鐘。我究竟做錯了什麼。 請任何一個幫助。 其實我想移動加速度計上的圖像變化。

回答

1

如果您正在使用動畫,則圖像在完成動畫後返回到開始位置。

如果你想達到這樣便只是增加動畫的持續時間,

,或者如果你要縮放圖像的一個地方到另一個地方而不動畫,

刪除動畫,只是在你的按鈕的點擊方法使用,

ImageView.scrollBy(scrollByX, scrollByY); 

欲瞭解更多信息,請登錄Android: Scrolling an Imageview