我的布局中有 3 Textview 和 1 Imageview 。通过此代码,我可以更改Imageview
的位置,将其移动到用户点击的每个Textview
之上。是否有任何方法可以将animation
从其位置移动到点击了Textview
?
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
DisplayMetrics screenSize = new DisplayMetrics();
getWindowManager().getDefaultDisplay().getMetrics(screenSize);
screenWidth = screenSize.widthPixels;
final ImageView greenBar = findViewById(R.id.green_bar);
greenBar.getLayoutParams().width = screenWidth / 3;
greenBar.setX(screenWidth * 2 /3);
TextView leftText = findViewById(R.id.text_left);
TextView rightText = findViewById(R.id.text_right);
TextView middleText = findViewById(R.id.text_middle);
leftText.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
greenBar.setX(0);
}
});
rightText.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
greenBar.setX(screenWidth * 2 /3);
}
});
middleText.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
greenBar.setX(screenWidth /3);
}
});
}
更新:我知道我应该使用这些构造函数,但我不知道如何设置这些值!
TranslateAnimation(float fromXDelta, float toXDelta, float fromYDelta, float toYDelta)
TranslateAnimation(int fromXType, float fromXValue, int toXType, float toXValue, int fromYType, float fromYValue, int toYType, float toYValue)