java如何在安卓中将图像视图从其位置设置为单击位置?
我的版面中有3个文本视图和1个图像视图。通过这段代码,我可以更改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)
共 (0) 个答案