سؤال

لقد قمت بإنشاء تخطيط مع عرض صورة وجهة نظر ويب. تم تعيين عرض الويب للحصول على رؤية افتراضية للذهاب. عندما ينطلق النشاط أنه يعرض عرض الصورة أولا وعند الانتهاء من طريقة عرض الويب من تحميل عنوان URL الخاص به، فإنه يمثل نفسه مرئيا ويتم تمييز ImageView كما هو مخفي.

عند إظهار imageView، أود ذلك للتدوير مرارا وتكرارا فقط للحصول على بيزاز مضافة قليلا.

لم أفعل أبدا الرسوم المتحركة من قبل في Android وجميع المشاركات التي وجدتها عندما طلبت من الإنترنت غير مفيدة؛ وبالتالي، عدت إلى ذلك للحصول على المساعدة.

لذلك إذا بدأت مع هذا ...

    final ImageView splash = (ImageView)findViewById(R.id.splash);

كيف يمكنني إنشاء رسوم متحركة تدوير متكررة وتطبيقه على ImageView؟

شكرا لك مرة أخرى!

هل كانت مفيدة؟

المحلول

إستخدم RotateAnimation, ، وضع نقطة المحورية إلى مركز صورتك.

RotateAnimation anim = new RotateAnimation(0f, 350f, 15f, 15f);
anim.setInterpolator(new LinearInterpolator());
anim.setRepeatCount(Animation.INFINITE);
anim.setDuration(700);

// Start animating the image
final ImageView splash = (ImageView) findViewById(R.id.splash);
splash.startAnimation(anim);

// Later.. stop the animation
splash.setAnimation(null);

نصائح أخرى

كيفية تدوير صورة حول مركزها:

ImageView view = ... //Initialize ImageView via FindViewById or programatically

RotateAnimation anim = new RotateAnimation(0.0f, 360.0f, Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF, 0.5f);

//Setup anim with desired properties
anim.setInterpolator(new LinearInterpolator());
anim.setRepeatCount(Animation.INFINITE); //Repeat animation indefinitely
anim.setDuration(700); //Put desired duration per anim cycle here, in milliseconds

//Start animation
view.startAnimation(anim); 
//Later on, use view.setAnimation(null) to stop it.

سيؤدي ذلك إلى تدوير الصورة حول مركزها (0.5 أو 50٪ من عرضه / ارتفاعه). أقوم بنشر هذا للقراء في المستقبل الذين ينتقلون إلى هنا من جوجل، كما أرغبوا في تدوير الصورة حول مركزها دون تحديد مركز المذكور في بكسل مطلق.

يمكنك أيضا استخدام ميزة تدوير الرسوم المتحركة. الذي يدير الرسوم المتحركة المحددة، للحصول على كمية محددة مسبقا من الوقت، على imageView.

Animation rotate = AnimationUtils.loadAnimation([context], R.anim.rotate_picture);
splash.startAnimation(rotate);

ثم قم بإنشاء ملف XML الرسوم المتحركة في RES / Anim يسمى Rotate_Picture مع المحتوى:

<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android" 
    android:shareInterpolator="false">

    <rotate 
    android:fromDegrees="0"
    android:toDegrees="360"
    android:duration="5000"
    android:pivotX="50%"
    android:pivotY="50%">
</rotate>
</set>

الآن لسوء الحظ، هذا لن يتم تشغيله مرة واحدة فقط. ستحتاج إلى حلقة في مكان ما لجعله كرر الرسوم المتحركة أثناء انتظاره. لقد جربت قليلا وحصلت على برنامجي عالق في حلقات لا حصر لها، لذلك لست متأكدا من أفضل طريقة لذلك. تحرير: يوفر إجابة Christopher معلومات حول كيفية جعلها حلقة بشكل صحيح، لذلك إزالة اقتراحي السيئ عن المواضيع المنفصلة!

طريقة واحدة - انقسمتك صورة إلى ن تدويرها قليلا في كل مرة. أود أن أقول 5 يكفي. ثم قم بإنشاء شيء من هذا القبيل

<animation-list   android:id="@+id/handimation" android:oneshot="false" 
    xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:drawable="@drawable/progress1" android:duration="150" />
    <item android:drawable="@drawable/progress2" android:duration="150" />
    <item android:drawable="@drawable/progress3" android:duration="150" />
 </animation-list> 

بدء الرمز

progress.setVisibility(View.VISIBLE);
AnimationDrawable frameAnimation = (AnimationDrawable)progress.getDrawable();
frameAnimation.setCallback(progress);
frameAnimation.setVisible(true, true);

توقف الكود

AnimationDrawable frameAnimation = (AnimationDrawable)progress.getDrawable();
frameAnimation.stop();
frameAnimation.setCallback(null);
frameAnimation = null;
progress.setVisibility(View.GONE);

أكثر هنا

imgDics = (ImageView) v.findViewById(R.id.img_player_tab2_dics);
    imgDics.setOnClickListener(onPlayer2Click);
    anim = new RotateAnimation(0f, 360f,
            Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF,
                            0.5f);
    anim.setInterpolator(new LinearInterpolator());
    anim.setRepeatCount(Animation.INFINITE);
    anim.setDuration(4000);

    // Start animating the image
    imgDics.startAnimation(anim);

داخل العنصر وضعت:

android:repeatCount="infinite"

لقد اكتشفت ذلك، إذا كنت تستخدم .getwidth / 2 وما إلى ذلك ... أنه لن يعمل، فلا يحتاجك إلى الحصول على عدد بكسل الصور وتقسيمها بمقدار 2 نفسك، ثم اكتب فقط في الرقم ل آخر 2 حجج.

لذلك أقول إن صورتك كانت 120 بكسل ميدان 120 بكسل، وأنت x و y يساوي 60 بكسل. لذلك في التعليمات البرمجية الخاصة بك، سوف تكون على حق:

RotateAnimation anim = new RotateAnimation(0f, 350f, 60f, 60f);
anim.setInterpolator(new LinearInterpolator());
anim.setRepeatCount(Animation.INFINITE);
anim.setDuration(700);

والآن صورتك ستؤدي إلى جولة مركزها.

رمز التحقق:

imageView.setImageResource(R.drawable.ic_arrow_up);

boolean up = true;

if (!up) { 
    up = true; 
    imageView.startAnimation(animate(up)); 
} else { 
    up = false; 
    imageView.startAnimation(animate(up)); 
}

private Animation animate(boolean up) {
    Animation anim = AnimationUtils.loadAnimation(this, up ? R.anim.rotate_up : R.anim.rotate_down);
    anim.setInterpolator(new LinearInterpolator()); // for smooth animation
    return anim;
}

جميلة / ic_arrow_up.xml.

<vector xmlns:android="http://schemas.android.com/apk/res/android"
        android:width="24dp"
        android:height="24dp"
        android:viewportWidth="24.0"
        android:viewportHeight="24.0">
    <path
        android:fillColor="#3d3d3d"
        android:pathData="M7.41,15.41L12,10.83l4.59,4.58L18,14l-6,-6 -6,6z"/>
</vector>

anim / rotate_up.xml.

<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android"
    android:fillAfter="true"
    android:fillEnabled="true">
    <rotate
        android:duration="200"
        android:fromDegrees="-180"
        android:pivotX="50%"
        android:pivotY="50%"
        android:toDegrees="0" />
</set>

anim / rotate_down.xml.

<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android"
    android:fillAfter="true"
    android:fillEnabled="true">
    <rotate
        android:duration="200"
        android:fromDegrees="0"
        android:pivotX="50%"
        android:pivotY="50%"
        android:toDegrees="180" />
</set>

لا حدود الصورة الصعبة. مجرد استخدام:

RotateAnimation anim = new RotateAnimation( fromAngle, toAngle, imageView.getDrawable().getBounds().width()/2, imageView.getDrawable().getBounds().height()/2);
مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top