gpt4 book ai didi

java - ontouchlistener 不工作

转载 作者:太空宇宙 更新时间:2023-11-04 12:31:01 26 4
gpt4 key购买 nike

我的图像移动得很奇怪,并且在我需要它保持连续的时候随机暂停。它停止然后工作。如果您可以提高效率,请发帖。还要看看是否有办法缩短代码。

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.MotionEvent;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;

public class MainActivity extends AppCompatActivity {
public int x;
public float g =10.0f;

@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
final ImageView iconss = (ImageView) findViewById(R.id.craft);
final Button button1 = (Button) findViewById(R.id.left);
final TextView txt1 = (TextView)findViewById(R.id.fail);
final Button button2 = (Button) findViewById(R.id.Right);

button2.setOnClickListener(new View.OnClickListener(){

public void onClick(View v) {
g = g + 10f;
x = 1;

final CharSequence work;
work = "it is working";
txt1.setText(work);

iconss.setTranslationX(g);
}

});

button2.setOnTouchListener(new View.OnTouchListener() {
@Override
public boolean onTouch(View v,MotionEvent event) {
g = g + 10f;
x = 1;

final CharSequence work;
work = "it is working";
txt1.setText(work);

iconss.setTranslationX(g);

return false;
}
});

button1.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
g = g - 10f;
x = 1;

final CharSequence work;
work = "it is working";
txt1.setText(work);

iconss.setTranslationX(g);
}
});

button1.setOnTouchListener(new View.OnTouchListener() {
@Override
public boolean onTouch(View v,MotionEvent event) {
g = g - 10f;
x = 1;

final CharSequence work;
work = "it is working";
txt1.setText(work);

iconss.setTranslationX(g);

return false;
}
});
}
}

最佳答案

该问题在 ClickEvent 和 TouchEvent 之间不明确。

您应该删除 TouchListener 并用动画替换它们

  1. 计算图像将移动到的目标位置

  2. 设置过渡时间

  3. 单击按钮时启动动画

关于java - ontouchlistener 不工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37846252/

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