[英]How to move ImageView to another ImageView Android Animation
有人知道我如何將一個imageview移到另一個imageview嗎?
我正在考慮這種方法,或者也許是我不知道的另一種方法...沒問題,我很高興學習它
<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種具有權重值。 按鈕頂部的頂部是布局,但沒有其他部分那么重。
所以從上方放置紙牌在布局1中,我想要到達的位置在布局2中,從下方放置紙牌在布局3中
我也使用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的幫助下成功了。
有很多工作要做,我沒有拒絕,因為我需要重新設計所有布局,但這沒問題,因為我學到了一些新東西,我感謝我能收到的所有幫助。
如果有人想將視圖(任何類型的視圖,imageview,buttonview,textview等)移動到另一個視圖,則必須使兩個視圖都處於同一布局中,這是最重要的部分。
實現此目的的代碼是:
//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();
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.