gpt4 book ai didi

android - 在android中动态移动图像

转载 作者:行者123 更新时间:2023-11-30 04:28:56 25 4
gpt4 key购买 nike

这是我的应用程序中的 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);
}
});

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

最佳答案

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

如果你想达到这样的效果,只需增加动画持续时间,

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

删除动画,只使用按钮的点击方法,

 ImageView.scrollBy(scrollByX, scrollByY);

有关更多信息,请查看 Android: Scrolling an Imageview .

关于android - 在android中动态移动图像,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8060730/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com