如何将 ImageView 移动到另一个 ImageView Android 动画
How to move ImageView to another ImageView Android Animation
有人知道我如何将一个图像视图移动到另一个图像视图吗?
我正在考虑这种方法,或者可能是另一种我不知道的方法...没问题,我很高兴学习它
<set xmlns:android="http://schemas.android.com/apk/res/android"
android:fillAfter="true"
android:interpolator="@android:anim/linear_interpolator">
<translate
android:duration="800"
android:fromXDelta="0%p"
android:toXDelta="75%p" />
我知道 fromXDelta(是起始位置)和 toXDelta 是应该到达的位置..但是?看看我的图片示例,我怎么知道我的起始位置和到达位置?
添加的详细信息:
这里有4种不同的布局,但只有3种有权重值。
从顶部开始的按钮所在的栏是一个布局,但不像其他栏那样有重量。
所以从上往下放置卡片在 layout1 中
我想要到达的位置在 layout2 中
向下的扑克牌在layout3
我还在 xml 中使用 onClick 方法而不是 onClickListeners。谢谢
您可以像这样以编程方式执行此操作:
@Override protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
final View target = findViewById(R.id.target);
final View viewToMove = findViewById(R.id.viewToMove);
viewToMove.setOnClickListener(new View.OnClickListener() {
@Override public void onClick(View v) {
translate(viewToMove, target);
}
});
}
private void translate(View viewToMove, View target) {
viewToMove.animate()
.x(target.getX())
.y(target.getY())
.setDuration(1000)
.start();
}
这里是 XML :
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout
android:id="@+id/activity_main"
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent">
<ImageView
android:id="@+id/target"
android:layout_width="50dp"
android:layout_height="50dp"
android:layout_gravity="center"
android:background="@color/colorAccent"/>
<ImageView
android:id="@+id/viewToMove"
android:layout_width="50dp"
android:layout_height="50dp"
android:background="@color/colorPrimary"
android:src="@mipmap/ic_launcher"/>
</FrameLayout>
终于在用户 Francois L 的帮助下成功了。
有很多工作要做,我不会说不,因为我需要重新设计所有布局,但这没问题,因为我学到了一些新东西,我很感激能得到的所有帮助。
如果有人想将一个视图(任何类型的视图、图像视图、按钮视图、文本视图等)移动到另一个视图,则两个视图必须处于同一布局中,这是最重要的部分。
实现这个的代码是:
//here is the part of initialization
ImageView poz1Inamic = (ImageView) findViewById(inamic_pozitia1);
ImageView poz1InamicSpateCarte = (ImageView) findViewById(inamic_pozitia1spatecarte);
//other code
poz1InamicSpateCarte.setVisibility(View.INVISIBLE);
//here is the initialization of the arrive position
ImageView cartePusaInamic = (ImageView) findViewById(R.id.cartePusaInamic);
//the code for moving from start position to arrive position
poz1Inamic.animate()
.x(cartePusaInamic.getX())
.y(cartePusaInamic.getY())
.setDuration(333)
.start();
有人知道我如何将一个图像视图移动到另一个图像视图吗?
我正在考虑这种方法,或者可能是另一种我不知道的方法...没问题,我很高兴学习它
<set xmlns:android="http://schemas.android.com/apk/res/android"
android:fillAfter="true"
android:interpolator="@android:anim/linear_interpolator">
<translate
android:duration="800"
android:fromXDelta="0%p"
android:toXDelta="75%p" />
我知道 fromXDelta(是起始位置)和 toXDelta 是应该到达的位置..但是?看看我的图片示例,我怎么知道我的起始位置和到达位置?
添加的详细信息:
这里有4种不同的布局,但只有3种有权重值。 从顶部开始的按钮所在的栏是一个布局,但不像其他栏那样有重量。
所以从上往下放置卡片在 layout1 中 我想要到达的位置在 layout2 中 向下的扑克牌在layout3
我还在 xml 中使用 onClick 方法而不是 onClickListeners。谢谢
您可以像这样以编程方式执行此操作:
@Override protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
final View target = findViewById(R.id.target);
final View viewToMove = findViewById(R.id.viewToMove);
viewToMove.setOnClickListener(new View.OnClickListener() {
@Override public void onClick(View v) {
translate(viewToMove, target);
}
});
}
private void translate(View viewToMove, View target) {
viewToMove.animate()
.x(target.getX())
.y(target.getY())
.setDuration(1000)
.start();
}
这里是 XML :
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout
android:id="@+id/activity_main"
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent">
<ImageView
android:id="@+id/target"
android:layout_width="50dp"
android:layout_height="50dp"
android:layout_gravity="center"
android:background="@color/colorAccent"/>
<ImageView
android:id="@+id/viewToMove"
android:layout_width="50dp"
android:layout_height="50dp"
android:background="@color/colorPrimary"
android:src="@mipmap/ic_launcher"/>
</FrameLayout>
终于在用户 Francois L 的帮助下成功了。
有很多工作要做,我不会说不,因为我需要重新设计所有布局,但这没问题,因为我学到了一些新东西,我很感激能得到的所有帮助。
如果有人想将一个视图(任何类型的视图、图像视图、按钮视图、文本视图等)移动到另一个视图,则两个视图必须处于同一布局中,这是最重要的部分。
实现这个的代码是:
//here is the part of initialization
ImageView poz1Inamic = (ImageView) findViewById(inamic_pozitia1);
ImageView poz1InamicSpateCarte = (ImageView) findViewById(inamic_pozitia1spatecarte);
//other code
poz1InamicSpateCarte.setVisibility(View.INVISIBLE);
//here is the initialization of the arrive position
ImageView cartePusaInamic = (ImageView) findViewById(R.id.cartePusaInamic);
//the code for moving from start position to arrive position
poz1Inamic.animate()
.x(cartePusaInamic.getX())
.y(cartePusaInamic.getY())
.setDuration(333)
.start();