مشاهده انیمیشن
با مجموعهها، منظم بمانید
ذخیره و طبقهبندی محتوا براساس اولویتهای شما.
روش Compose را امتحان کنید
Jetpack Compose جعبه ابزار UI توصیه شده برای اندروید است. نحوه استفاده از انیمیشن ها در Compose را بیاموزید.
شما می توانید از سیستم انیمیشن view برای اجرای انیمیشن tweened در Views استفاده کنید. انیمیشن Tween انیمیشن را با اطلاعاتی مانند نقطه شروع، نقطه پایان، اندازه، چرخش و سایر جنبه های رایج یک انیمیشن محاسبه می کند.
یک انیمیشن tween می تواند یک سری تبدیل ساده (موقعیت، اندازه، چرخش و شفافیت) را روی محتویات یک شی View انجام دهد. بنابراین، اگر یک شی TextView
دارید، میتوانید متن را جابهجا کنید، بچرخانید، رشد کنید یا کوچک کنید. اگر تصویر پس زمینه داشته باشد، تصویر پس زمینه به همراه متن تغییر شکل می دهد. animation package
تمام کلاس های مورد استفاده در یک انیمیشن tween را ارائه می دهد.
دنباله ای از دستورالعمل های انیمیشن، انیمیشن tween را تعریف می کند که با کد XML یا Android تعریف شده است. مانند تعریف یک طرح، یک فایل XML توصیه می شود زیرا خوانا، قابل استفاده مجدد و قابل تعویض تر از کدگذاری سخت انیمیشن است. در مثال زیر از XML استفاده می کنیم. (برای کسب اطلاعات بیشتر در مورد تعریف انیمیشن در کد برنامه خود، به جای XML، به کلاس AnimationSet
و سایر زیر کلاس های Animation
مراجعه کنید.)
دستورالعملهای انیمیشن تغییراتی را که میخواهید رخ دهند، زمانی که رخ میدهند و مدت زمانی که باید طول بکشد تا اعمال شوند را تعریف میکند. تبدیلها میتوانند متوالی یا همزمان باشند - برای مثال، میتوانید محتویات TextView را از چپ به راست حرکت دهید و سپس 180 درجه بچرخانید، یا میتوانید متن را حرکت داده و همزمان بچرخانید. هر تبدیل مجموعهای از پارامترهای خاص برای آن تبدیل را میگیرد (اندازه شروع و اندازه پایان برای تغییر اندازه، زاویه شروع و زاویه پایان برای چرخش و غیره)، و همچنین مجموعهای از پارامترهای رایج (به عنوان مثال، زمان شروع و مدت زمان) . برای اینکه چندین تغییر به طور همزمان اتفاق بیفتند، زمان شروع یکسانی را به آنها بدهید. برای متوالی کردن آنها، زمان شروع به اضافه مدت تبدیل قبلی را محاسبه کنید.
فایل XML انیمیشن در دایرکتوری res/anim/
پروژه اندروید شما قرار دارد. فایل باید یک عنصر ریشه واحد داشته باشد: این یک عنصر <alpha>
، <scale>
، <translate>
، <rotate>
، عنصر interpolator یا <set>
خواهد بود که گروههایی از این عناصر را در خود نگه میدارد (که ممکن است شامل دیگری باشد. <set>
). به طور پیش فرض، تمام دستورالعمل های انیمیشن به طور همزمان اعمال می شوند. برای اینکه آنها به صورت متوالی اتفاق بیفتند، باید مشخصه startOffset
را مشخص کنید، همانطور که در مثال زیر نشان داده شده است.
XML زیر از یکی از ApiDemos برای کشش، سپس چرخش و چرخش همزمان یک شی View استفاده می شود.
<set android:shareInterpolator="false">
<scale
android:interpolator="@android:anim/accelerate_decelerate_interpolator"
android:fromXScale="1.0"
android:toXScale="1.4"
android:fromYScale="1.0"
android:toYScale="0.6"
android:pivotX="50%"
android:pivotY="50%"
android:fillAfter="false"
android:duration="700" />
<set android:interpolator="@android:anim/decelerate_interpolator">
<scale
android:fromXScale="1.4"
android:toXScale="0.0"
android:fromYScale="0.6"
android:toYScale="0.0"
android:pivotX="50%"
android:pivotY="50%"
android:startOffset="700"
android:duration="400"
android:fillBefore="false" />
<rotate
android:fromDegrees="0"
android:toDegrees="-45"
android:toYScale="0.0"
android:pivotX="50%"
android:pivotY="50%"
android:startOffset="700"
android:duration="400" />
</set>
</set>
مختصات صفحه (که در این مثال استفاده نمی شود) در گوشه سمت چپ بالا (0,0) هستند و با پایین رفتن و به سمت راست افزایش می یابند.
برخی از مقادیر مانند pivotX را می توان نسبت به خود شی یا نسبت به والد تعیین کرد. مطمئن شوید که از قالب مناسب برای آنچه می خواهید استفاده کنید ("50" برای 50٪ نسبت به والدین، یا "50٪" برای 50٪ نسبت به خودش).
شما می توانید با اختصاص یک Interpolator
تعیین کنید که چگونه یک تبدیل در طول زمان اعمال می شود. Android شامل چندین زیر کلاس Interpolator است که منحنیهای سرعت مختلفی را مشخص میکنند: به عنوان مثال، AccelerateInterpolator
به یک تبدیل میگوید که آهسته شروع شود و سرعت آن افزایش یابد. هر یک دارای یک مقدار مشخصه است که می تواند در XML اعمال شود.
با ذخیره این XML به عنوان hyperspace_jump.xml
در دایرکتوری res/anim/
پروژه، کد زیر به آن ارجاع می دهد و آن را به یک شی ImageView
از طرح اعمال می کند.
کاتلین
AnimationUtils.loadAnimation(this, R.anim.hyperspace_jump).also { hyperspaceJumpAnimation ->
findViewById<ImageView>(R.id.spaceshipImage).startAnimation(hyperspaceJumpAnimation)
}
جاوا
ImageView spaceshipImage = (ImageView) findViewById(R.id.spaceshipImage);
Animation hyperspaceJumpAnimation = AnimationUtils.loadAnimation(this, R.anim.hyperspace_jump);
spaceshipImage.startAnimation(hyperspaceJumpAnimation);
به عنوان جایگزینی برای startAnimation()
، میتوانید با Animation.setStartTime()
زمان شروع انیمیشن را تعیین کنید، سپس انیمیشن را به View با View.setAnimation()
اختصاص دهید.
برای اطلاعات بیشتر در مورد نحو XML، برچسبها و ویژگیهای موجود، به منابع انیمیشن مراجعه کنید.
توجه: صرف نظر از اینکه انیمیشن شما چگونه ممکن است جابجا شود یا اندازه آن تغییر کند، محدوده نماهایی که انیمیشن شما را در خود نگه می دارد به طور خودکار برای مطابقت با آن تنظیم نمی شود. با این حال، انیمیشن همچنان فراتر از محدوده نمای خود کشیده می شود و بریده نمی شود. با این حال، اگر انیمیشن از محدوده نمای والد فراتر رود، بریده شدن اتفاق می افتد .
محتوا و نمونه کدها در این صفحه مشمول پروانههای توصیفشده در پروانه محتوا هستند. جاوا و OpenJDK علامتهای تجاری یا علامتهای تجاری ثبتشده Oracle و/یا وابستههای آن هستند.
تاریخ آخرین بهروزرسانی 2025-07-29 بهوقت ساعت هماهنگ جهانی.
[[["درک آسان","easyToUnderstand","thumb-up"],["مشکلم را برطرف کرد","solvedMyProblem","thumb-up"],["غیره","otherUp","thumb-up"]],[["اطلاعاتی که نیاز دارم وجود ندارد","missingTheInformationINeed","thumb-down"],["بیشازحد پیچیده/ مراحل بسیار زیاد","tooComplicatedTooManySteps","thumb-down"],["قدیمی","outOfDate","thumb-down"],["مشکل ترجمه","translationIssue","thumb-down"],["مشکل کد / نمونهها","samplesCodeIssue","thumb-down"],["غیره","otherDown","thumb-down"]],["تاریخ آخرین بهروزرسانی 2025-07-29 بهوقت ساعت هماهنگ جهانی."],[],[],null,["# View Animation\n\nTry the Compose way \nJetpack Compose is the recommended UI toolkit for Android. Learn how to use Animations in Compose. \n[Animate properties →](/develop/ui/compose/animation/value-based) \n\nYou can use the view animation system to perform tweened animation on Views. Tween animation\ncalculates the animation with information such as the start point, end point, size, rotation, and\nother common aspects of an animation.\n\nA tween animation can perform a series of simple transformations (position, size, rotation,\nand transparency) on the contents of a View object. So, if you have a [TextView](/reference/android/widget/TextView) object, you can move, rotate, grow, or shrink the text. If it has a\nbackground image, the background image will be transformed along with the text. The [animation package](/reference/android/view/animation/package-summary) provides all the classes used in a tween animation.\n\nA sequence of animation instructions defines the tween animation, defined by either XML or\nAndroid code. As with defining a layout, an XML file is recommended because it's more readable,\nreusable, and swappable than hard-coding the animation. In the example below, we use XML. (To\nlearn more about defining an animation in your application code, instead of XML, refer to the\n[AnimationSet](/reference/android/view/animation/AnimationSet) class and other [Animation](/reference/android/view/animation/Animation) subclasses.)\n\nThe animation instructions define the transformations that you want to occur, when they will\noccur, and how long they should take to apply. Transformations can be sequential or simultaneous\n- for example, you can have the contents of a TextView move from left to right, and then rotate\n180 degrees, or you can have the text move and rotate simultaneously. Each transformation takes a\nset of parameters specific for that transformation (starting size and ending size for size\nchange, starting angle and ending angle for rotation, and so on), and also a set of common\nparameters (for instance, start time and duration). To make several transformations happen\nsimultaneously, give them the same start time; to make them sequential, calculate the start time\nplus the duration of the preceding transformation.\n\nThe animation XML file belongs in the `res/anim/` directory of your Android\nproject. The file must have a single root element: this will be either a single\n`\u003calpha\u003e`, `\u003cscale\u003e`, `\u003ctranslate\u003e`,\n`\u003crotate\u003e`, interpolator element, or `\u003cset\u003e` element that holds\ngroups of these elements (which may include another `\u003cset\u003e`). By default, all\nanimation instructions are applied simultaneously. To make them occur sequentially, you must\nspecify the `startOffset` attribute, as shown in the example below.\n\nThe following XML from one of the ApiDemos is used to stretch, then simultaneously spin and\nrotate a View object. \n\n```xml\n\u003cset android:shareInterpolator=\"false\"\u003e\n \u003cscale\n android:interpolator=\"@android:anim/accelerate_decelerate_interpolator\"\n android:fromXScale=\"1.0\"\n android:toXScale=\"1.4\"\n android:fromYScale=\"1.0\"\n android:toYScale=\"0.6\"\n android:pivotX=\"50%\"\n android:pivotY=\"50%\"\n android:fillAfter=\"false\"\n android:duration=\"700\" /\u003e\n \u003cset android:interpolator=\"@android:anim/decelerate_interpolator\"\u003e\n \u003cscale\n android:fromXScale=\"1.4\"\n android:toXScale=\"0.0\"\n android:fromYScale=\"0.6\"\n android:toYScale=\"0.0\"\n android:pivotX=\"50%\"\n android:pivotY=\"50%\"\n android:startOffset=\"700\"\n android:duration=\"400\"\n android:fillBefore=\"false\" /\u003e\n \u003crotate\n android:fromDegrees=\"0\"\n android:toDegrees=\"-45\"\n android:toYScale=\"0.0\"\n android:pivotX=\"50%\"\n android:pivotY=\"50%\"\n android:startOffset=\"700\"\n android:duration=\"400\" /\u003e\n \u003c/set\u003e\n\u003c/set\u003e\n```\n\nScreen coordinates (not used in this example) are (0,0) at the upper left hand corner, and\nincrease as you go down and to the right.\n\nSome values, such as pivotX, can be specified relative to the object itself or relative to the\nparent. Be sure to use the proper format for what you want (\"50\" for 50% relative to the parent,\nor \"50%\" for 50% relative to itself).\n\nYou can determine how a transformation is applied over time by assigning an [Interpolator](/reference/android/view/animation/Interpolator). Android includes several Interpolator subclasses that\nspecify various speed curves: for instance, [AccelerateInterpolator](/reference/android/view/animation/AccelerateInterpolator)\ntells a transformation to start slow and speed up. Each one has an attribute value that can be\napplied in the XML.\n\nWith this XML saved as `hyperspace_jump.xml` in the `res/anim/`\ndirectory of the project, the following code will reference it and apply it to an [ImageView](/reference/android/widget/ImageView) object from the layout. \n\n### Kotlin\n\n```kotlin\nAnimationUtils.loadAnimation(this, R.anim.hyperspace_jump).also { hyperspaceJumpAnimation -\u003e\n findViewById\u003cImageView\u003e(R.id.spaceshipImage).startAnimation(hyperspaceJumpAnimation)\n}\n```\n\n### Java\n\n```java\nImageView spaceshipImage = (ImageView) findViewById(R.id.spaceshipImage);\nAnimation hyperspaceJumpAnimation = AnimationUtils.loadAnimation(this, R.anim.hyperspace_jump);\nspaceshipImage.startAnimation(hyperspaceJumpAnimation);\n```\n\nAs an alternative to `startAnimation()`, you can define a starting time for the\nanimation with [Animation.setStartTime()](/reference/android/view/animation/Animation#setStartTime(long)), then assign the animation to the View with [View.setAnimation()](/reference/android/view/View#setAnimation(android.view.animation.Animation)).\n\nFor more information on the XML syntax, available tags and attributes, see [Animation Resources](/guide/topics/resources/animation-resource).\n\n**Note:** Regardless of how your animation may move or resize, the\nbounds of the View that holds your animation will not automatically adjust to accommodate it.\nEven so, the animation will still be drawn beyond the bounds of its View and will not be clipped.\nHowever, clipping *will occur* if the animation exceeds the bounds of the parent View."]]