2011-11-09 54 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