У меня есть ImageButton с изображением. Я хочу повернуть ЕДИНСТВЕННОЕ изображение.
Не так: вращающаяся кнопка
Вот что у меня есть:
<ImageButton
android:id="@+id/imageButton"
android:layout_width="100dp"
android:layout_height="100dp"
android:layout_alignBottom="@+id/button"
android:src="@drawable/up"
/>
<set xmlns:android="http://schemas.android.com/apk/res/android">
<rotate
android:fromDegrees="0"
android:toDegrees="360"
android:pivotX="50%"
android:pivotY="50%"
android:repeatCount="0"
android:duration="1000" />
</set>
public void clockwise(View view){
ImageButton image = (ImageButton)findViewById(R.id.imageButton);
Animation animation = AnimationUtils.loadAnimation(getApplicationContext(), R.anim.myanimation);<p>
image.startAnimation(animation);
}
Должен ли я использовать AnimationDrawable или Matrix или любые другие подходы?
2 ответа
Вот ответ, как повернуть единственное изображение. Спасибо @Fondesa, я принял ваше предложение.
1. файл аминирования в res / amin / ic_play_sound_animation.xml:
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android"
android:shareInterpolator="false">
<rotate
android:duration="1000"
android:fromDegrees="0"
android:interpolator="@android:anim/accelerate_decelerate_interpolator"
android:pivotX="50%"
android:pivotY="50%"
android:repeatCount="infinite"
android:repeatMode="restart"
android:toDegrees="359"/>
</set>
2. Кусок макета страницы с двумя видами (кнопка и изображение выше):
<FrameLayout
android:id="@+id/frame"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_column="4"
android:layout_row="3">
<Button
android:id="@+id/id_btn_play_sound"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
<ImageView
android:id="@+id/id_iv_play_sound_icon"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_gravity="center"
android:scaleType="fitCenter"
android:src="@drawable/ic_play_sound_1"/>
</FrameLayout>
< Сильный > 3 . Java-код в Activity:
private ImageView playSoundIcon = (ImageView)view. findViewById (R.id.id_iv_play_sound_icon);
private void runBtnAnimation() {
playSoundIcon.setImageResource(R.drawable.ic_playing_sound_2);
Animation rotation = AnimationUtils
.loadAnimation(getActivity().getApplicationContext(),
R.anim.ic_play_sound_animation);
playSoundIcon.startAnimation(rotation);
}
private void stopBtnAnimation() {
playSoundIcon.clearAnimation();
playSoundIcon.setImageResource(R.drawable.ic_play_sound_1);
}
4. вы можете посмотреть анимацию GIF, как это выглядит: окончательная анимация для кнопки
У вас есть два варианта:
Отделите ваш
Button
от вашего изображения с помощьюFrameLayout
. В этом случае вы можете установить прослушиватель щелчка наButton
и применить вращение только кImageView
. Это легче и быстрее выполнить, но это обходной путь, и производительность лучше при втором выборе, потому что у вас нет вложенных макетов:<FrameLayout android:layout_width="100dp" android:layout_height="100dp" android:layout_alignBottom="@+id/button"> <Button android:id="@+id/imageButton" android:layout_width="match_parent" android:layout_height="match_parent" /> <ImageView android:id="@+id/imageButtonContent" android:layout_width="match_parent" android:layout_height="match_parent" /> </FrameLayout>
Используйте
AnimationDrawable
. Вы можете предоставить список рисования во время анимации. Я серьезно рекомендую это.
Новые вопросы
android
Android - это мобильная операционная система Google, используемая для программирования или разработки цифровых устройств (смартфоны, планшеты, автомобили, телевизоры, одежда, стекло, IoT). Для тем, связанных с Android, используйте специальные теги Android, такие как android-intent, android-activity, android-адаптер и т. Д. Для вопросов, не связанных с разработкой или программированием, но связанных с платформой Android, используйте эту ссылку: https: // android.stackexchange.com .