[英]how to animate a button inside a LinearLayout with ObjectAnimator
我正在嘗試使用ObjectAnimator為LinearLayout中的一個按鈕設置動畫,當我單擊Layout時,什么也沒有發生。
但是,如果我通過LinearLayout內的ImageViews更改按鈕,則動畫開始不會出現問題。
這是xml:
<LinearLayout
android:id="@+id/images"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<ImageView
android:id="@+id/img1"
android:layout_width="match_parent"
android:layout_height="0dip"
android:layout_weight="1.0"
android:src="@drawable/image1"
android:visibility="visible"
/>
<ImageView
android:id="@+id/img2"
android:layout_width="match_parent"
android:layout_height="0dip"
android:layout_weight="1.0"
android:src="@drawable/image2"
android:visibility="gone" />
</LinearLayout>
這是代碼:
final LinearLayout images = (LinearLayout) findViewById(R.id.images);
images.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
final ObjectAnimator goneToVisible = ObjectAnimator.ofFloat(images, "rotationY", -90f, 0f);
goneToVisible.setDuration(1000);
goneToVisible.start();
}
});
一切正常,但是當我通過Button更改ImageView時:
<LinearLayout
android:id="@+id/images"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<Button
android:id="@+id/img1"
android:layout_width="match_parent"
android:layout_height="0dip"
android:layout_weight="1.0"
android:background="@drawable/image1"
android:visibility="visible"
/>
<Button
android:id="@+id/img2"
android:layout_width="match_parent"
android:layout_height="0dip"
android:layout_weight="1.0"
android:background="@drawable/image2"
android:visibility="gone" />
</LinearLayout>
final LinearLayout images = (LinearLayout) findViewById(R.id.images);
images.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
//flip(image1, image2, animLength.getProgress());
final ObjectAnimator goneToVisible = ObjectAnimator.ofFloat(images, "rotationY", -90f, 0f);
goneToVisible.setDuration(1000);
goneToVisible.start();
}
});
動畫不起作用。
如果從LinearLayout中取出按鈕,則可以使用,但不能在內部使用。 我需要使用Buttons而不是ImageView。
我該如何解決這個問題?
謝謝。
ImageView
的第一個代碼對您ImageView
,因為ImageView
默認是不可單擊的,因此當您單擊LinearLayou
,單擊事件將響應LinearLayout
,而布局單擊事件中的動畫將起作用
帶Button
的第二個代碼對您不起作用,因為Button
具有可單擊的位置,因此當您單擊布局時, button
具有的是布局的高度,因此單擊布局時,單擊事件將響應Button
而不是布局,您沒有Button
單擊事件的偵聽器,沒有動畫發生,因此您可以將其放在xml布局中
android:clickable="false"
這將使事件直接響應布局或使Button
上的動畫本身像這樣
final Button btn = (Button) findViewById(R.id.img1);
final LinearLayout images = (LinearLayout) findViewById(R.id.images);
btn.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
//flip(image1, image2, animLength.getProgress());
final ObjectAnimator goneToVisible = ObjectAnimator.ofFloat(btn, "rotationY", -90f, 0f);
goneToVisible.setDuration(1000);
goneToVisible.start();
}
});
喂我回來
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.