我嘗試使用ObjectAnimator爲LinearLayout內的按鈕設置動畫效果,當我點擊佈局時什麼也沒有發生。如何使用ObjectAnimator爲LinearLayout內的按鈕設置動畫效果
但是,如果我更改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();
}
});
寄託都工作正常,但是當我通過按鈕來更改的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。
我該如何解決這個問題?
謝謝。
這將是更有益OP和未來的訪問者來描述你所做的改變,以及*他們爲什麼工作,而不是僅僅提供代碼。 – codeMagic
嗨。如果你點擊按鈕,它的工作正常,但如果你點擊容器,它不適用於內部的按鈕,但它有效,如果你有ImageVies而不是按鈕。最終的LinearLayout圖像=(LinearLayout)findViewById(R.id.images); images.setOnClickListener(new View.OnClickListener(){。我想單擊容器以啓動按鈕的動畫。謝謝 –
@codeMagic感謝您的評論我將更詳細地解釋此問題 –