【问题标题】:Move an image dynamically in android在android中动态移动图像
【发布时间】:2011-11-09 05:26:40
【问题描述】:

这是我的应用程序的 oncreate。

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

}

控制.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); 
            }
        });

但现在图像会消失两秒钟。我究竟做错了什么。 请任何人帮助。 实际上我想在加速度计变化时移动图像。

【问题讨论】:

    标签: android


    【解决方案1】:

    如果您使用的是动画,那么您的图像在完成动画后会回到起始位置。

    如果你想实现这样,那么只需增加动画持续时间,

    或者如果你想在没有动画的情况下将图像从一个地方缩放到另一个地方,

    删除动画并仅用于按钮的单击方法,

     ImageView.scrollBy(scrollByX, scrollByY);
    

    更多信息请查看Android: Scrolling an Imageview

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-11-03
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多