所以这里是简单转换的代码,从 top_left 到 bottom_right,我不明白为什么我必须返回 false 才能使 onTouch() 正常工作,
1.如果我设置 return true, one a single touch ,它将 counter 从 0 设置为 3 ,或者只是将 2 或 3 添加到 counter 并且大部分时间保持在当前位置。
2.如果我设置return false,程序运行正常,按照写的,即counter++;
public class MainActivity extends AppCompatActivity {
Button b;
TextView tv;
public static int counter_button=0,counter=0;
RelativeLayout.LayoutParams old_rules;
RelativeLayout rl;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
b= (Button) findViewById(R.id.b1);
tv= (TextView) findViewById(R.id.tv1);
rl= (RelativeLayout) findViewById(R.id.rl);
old_rules= (RelativeLayout.LayoutParams) b.getLayoutParams();
rl.setOnTouchListener(new View.OnTouchListener() {
@Override
public boolean onTouch(View v, MotionEvent event) {
my_changes_touch();
return false;
}
});
}
public void my_changes_touch(){
RelativeLayout.LayoutParams rules= new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT,RelativeLayout.LayoutParams.WRAP_CONTENT);
if(counter%2==0) {
counter++;
tv.setText(" "+counter);
rules.addRule(RelativeLayout.ALIGN_PARENT_BOTTOM);
rules.addRule(RelativeLayout.ALIGN_PARENT_RIGHT);
b.setLayoutParams(rules);
rules.height=400;
rules.width=400;
b.setLayoutParams(rules);
getContentTransitionManager().beginDelayedTransition(rl);
} else {
counter++;
tv.setText(" "+counter);
b.setLayoutParams(old_rules);
getContentTransitionManager().beginDelayedTransition(rl);
}
}
}