search
HomeBackend DevelopmentXML/RSS TutorialDetailed explanation of examples of writing animation in xml

1. Selector

The Selector in Android is mainly used to change the default background of the ListView and Button controls.

1. Create the mylist_view.xml file
First create a new drawable folder in the res directory, and then create a new mylist_view.xml in the newly created drawable folder. , its directory structure is: res/drawable/mylist_view.xml.
2. Edit the mylist_view.xml file according to specific needs
After creating a new mylist_view.xml file, its internal code structure is as follows when no attributes are added:

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

Now you can define the style you want internally according to the project requirements. The main attributes are as follows:

<?xml version="1.0" encoding="utf-8" ?>       
<selector xmlns:android="http://schemas.android.com/apk/res/android">     
<!-- 默认时的背景图片-->      
  <item android:drawable="@drawable/pic1" />        
<!-- 没有焦点时的背景图片 -->      
  <item android:state_window_focused="false"       
        android:drawable="@drawable/pic1" />       
<!-- 非触摸模式下获得焦点并单击时的背景图片 -->      
  <item android:state_focused="true" android:state_pressed="true"   android:drawable= "@drawable/pic2" />     
<!-- 触摸模式下单击时的背景图片-->      
<item android:state_focused="false" android:state_pressed="true"   android:drawable="@drawable/pic3" />      
<!--选中时的图片背景-->      
  <item android:state_selected="true"   android:drawable="@drawable/pic4" />       
<!--获得焦点时的图片背景-->      
  <item android:state_focused="true"   android:drawable="@drawable/pic5" />       
</selector>

3. Quote the mylist_view.xml file
There are three ways to reference the file you just created:
(1) Add the following attribute code to the ListView
android:listSelector="@drawable/mylist_view"
(2) In Add the following attribute code to the item interface of ListView
android:background="@drawable/mylist_view"
(3) Use JAVA code to write directly
Drawable drawable = getResources().getDrawable(R.drawable.mylist_view );
listView.setSelector(drawable);

In order to prevent the list from being blacklisted, you need to add the following attribute code to the ListView
android:cacheColorHint="@android:color/transparent "
Attribute introduction:
android:state_selected selected
android:state_focused gets focus
android:state_pressed click
android:state_enabled sets whether to respond to events, referring to all events


2. Write animation in XML
Animation can also be placed in XML files, which improves the maintainability of the program. The steps to write animation in XML are as follows
1. Create a new folder named anim under the res folder
2. Create an xml file, and first add the set tag, change the tag as follows

<set xmlns:android="http://schemas.android.com/apk/res/android"  
    android:interpolator="@android:anim/accelerate_interpolator">   
</set>

3. Add rotate, alpha, scale or translate tags to the tag
4. Use AnimationUtils to load the xml file in the code and generate the Animation object

Alpha animation

<?xml version="1.0" encoding="utf-8"?>    
<set xmlns:android="http://schemas.android.com/apk/res/android"    
    android:interpolator="@android:anim/accelerate_interpolator">    
    <alpha    
        android:fromAlpha="1.0"    
        android:toAlpha="0.0"    
        android:startOffset="500"    
        android:duration="2000"    
            />      
</set>  
Animation a=AnimationUtils.loadAnimation(this, R.anim.alpha);  
iv.startAnimation(a);

Scale animation

<?xml version="1.0" encoding="utf-8"?>    
<set xmlns:android="http://schemas.android.com/apk/res/android"    
    android:interpolator="@android:anim/accelerate_interpolator">    
    <scale    
        android:fromXScale="1.0"    
        android:toXScale="0.0"    
        android:fromYScale="1.0"    
        android:toYScale="0.0"    
        android:pivotX="50%"    
        android:pivotY="50%"    
        android:duration="2000"    
    />      
</set>

Rotate animation

<?xml version="1.0" encoding="utf-8"?>    
<set xmlns:android="http://schemas.android.com/apk/res/android"    
    android:interpolator="@android:anim/accelerate_interpolator">    
    <rotate    
        android:fromDegrees="0"    
        android:toDegrees="400"    
        android:pivotX="50%"    
        android:pivotY="50%"    
        android:duration="3000"    
    />      
</set>

Translate animation

<?xml version="1.0" encoding="utf-8"?>    
<set xmlns:android="http://schemas.android.com/apk/res/android"    
    android:interpolator="@android:anim/accelerate_interpolator">    
    <translate    
        android:fromXDelta="50%"    
        android:toXDelta="100%"    
        android:fromYDelta="50%"    
        android:toYDelta="100%"    
        android:duration="3000"    
    />      
</set>

Here we focus on android:pivotX and android:pivotY and android:fromXDelta,android:toXDelta
android :pivotX="50" uses absolute coordinates
android:pivotX="50%"relative to self
android:pivotX="50%p"relative to parent control


How are these animations called?
Call in styles.xml:

<?xml version="1.0" encoding="utf-8"?>  
<resources>      
    <style mce_bogus="1" name="ThemeActivity">  
        <item name="android:windowAnimationStyle">@style/AnimationActivity</item>  
         <item name="android:windowNoTitle">true</item>  
    </style>      
    <style name="AnimationActivity" parent="@android:style/Animation.Activity" mce_bogus="1">  
        <item name="android:activityOpenEnterAnimation">@anim/translate</item>  
         <item name="android:activityOpenExitAnimation">@anim/rotate</item>  
          <item name="android:activityCloseEnterAnimation">@anim/close_enter</item>  
           <item name="android:activityCloseExitAnimation">@anim/close_exit</item>  
    </style>      
</resources>

注:在/res 目录下新建 anim 目录,上面的Translate.xml,Scale.xml都是在这个文件夹下新建的。

3> Interpolator -- 定义动画变化的速率
① AccelerateDecelerateInterpolator:
   在动画开始和结束的地方速率改变比较慢,在中间的时候加速;
② AccelaerateInterPolotor:
    在动画开始的地方速率改变比较慢,然后开始加速;
③ CycleInterpolator:
动画循环播放特定的次数,速率沿着正弦曲线
④ DecelerateInterpolator:
在动画结束的地方速率比较慢
⑤ LinearInterpolator:
动画以匀速运动

在xml文件中定义Interpolator
android:interpolator="@android:anim/accelerate_interpolator"
android:shareInterpolator="true"
这样所有的Animation共用一个Interpolator。
在代码中用代码设置如下
anim.setInterpolator(new AccelerateInterpolator());
在new一个AnimationSet中传入true则所有的Animation共用Interpolator。
 

The above is the detailed content of Detailed explanation of examples of writing animation in xml. For more information, please follow other related articles on the PHP Chinese website!

Statement
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Building Feeds with XML: A Hands-On Guide to RSSBuilding Feeds with XML: A Hands-On Guide to RSSApr 14, 2025 am 12:17 AM

The steps to build an RSSfeed using XML are as follows: 1. Create the root element and set the version; 2. Add the channel element and its basic information; 3. Add the entry element, including the title, link and description; 4. Convert the XML structure to a string and output it. With these steps, you can create a valid RSSfeed from scratch and enhance its functionality by adding additional elements such as release date and author information.

Creating RSS Documents: A Step-by-Step TutorialCreating RSS Documents: A Step-by-Step TutorialApr 13, 2025 am 12:10 AM

The steps to create an RSS document are as follows: 1. Write in XML format, with the root element, including the elements. 2. Add, etc. elements to describe channel information. 3. Add elements, each representing a content entry, including,,,,,,,,,,,. 4. Optionally add and elements to enrich the content. 5. Ensure the XML format is correct, use online tools to verify, optimize performance and keep content updated.

XML's Role in RSS: The Foundation of Syndicated ContentXML's Role in RSS: The Foundation of Syndicated ContentApr 12, 2025 am 12:17 AM

The core role of XML in RSS is to provide a standardized and flexible data format. 1. The structure and markup language characteristics of XML make it suitable for data exchange and storage. 2. RSS uses XML to create a standardized format to facilitate content sharing. 3. The application of XML in RSS includes elements that define feed content, such as title and release date. 4. Advantages include standardization and scalability, and challenges include document verbose and strict syntax requirements. 5. Best practices include validating XML validity, keeping it simple, using CDATA, and regularly updating.

From XML to Readable Content: Demystifying RSS FeedsFrom XML to Readable Content: Demystifying RSS FeedsApr 11, 2025 am 12:03 AM

RSSfeedsareXMLdocumentsusedforcontentaggregationanddistribution.Totransformthemintoreadablecontent:1)ParsetheXMLusinglibrarieslikefeedparserinPython.2)HandledifferentRSSversionsandpotentialparsingerrors.3)Transformthedataintouser-friendlyformatsliket

Is There an RSS Alternative Based on JSON?Is There an RSS Alternative Based on JSON?Apr 10, 2025 am 09:31 AM

JSONFeed is a JSON-based RSS alternative that has its advantages simplicity and ease of use. 1) JSONFeed uses JSON format, which is easy to generate and parse. 2) It supports dynamic generation and is suitable for modern web development. 3) Using JSONFeed can improve content management efficiency and user experience.

RSS Document Tools: Building, Validating, and Publishing FeedsRSS Document Tools: Building, Validating, and Publishing FeedsApr 09, 2025 am 12:10 AM

How to build, validate and publish RSSfeeds? 1. Build: Use Python scripts to generate RSSfeed, including title, link, description and release date. 2. Verification: Use FeedValidator.org or Python script to check whether RSSfeed complies with RSS2.0 standards. 3. Publish: Upload RSS files to the server, or use Flask to generate and publish RSSfeed dynamically. Through these steps, you can effectively manage and share content.

Securing Your XML/RSS Feeds: A Comprehensive Security ChecklistSecuring Your XML/RSS Feeds: A Comprehensive Security ChecklistApr 08, 2025 am 12:06 AM

Methods to ensure the security of XML/RSSfeeds include: 1. Data verification, 2. Encrypted transmission, 3. Access control, 4. Logs and monitoring. These measures protect the integrity and confidentiality of data through network security protocols, data encryption algorithms and access control mechanisms.

XML/RSS Interview Questions & Answers: Level Up Your ExpertiseXML/RSS Interview Questions & Answers: Level Up Your ExpertiseApr 07, 2025 am 12:19 AM

XML is a markup language used to store and transfer data, and RSS is an XML-based format used to publish frequently updated content. 1) XML describes data structures through tags and attributes, 2) RSS defines specific tag publishing and subscribed content, 3) XML can be created and parsed using Python's xml.etree.ElementTree module, 4) XML nodes can be queried for XPath expressions, 5) Feedparser library can parse RSSfeed, 6) Common errors include tag mismatch and encoding issues, which can be validated by XMLlint, 7) Processing large XML files with SAX parser can optimize performance.

See all articles

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
3 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
3 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. How to Fix Audio if You Can't Hear Anyone
4 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
WWE 2K25: How To Unlock Everything In MyRise
1 months agoBy尊渡假赌尊渡假赌尊渡假赌

Hot Tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Safe Exam Browser

Safe Exam Browser

Safe Exam Browser is a secure browser environment for taking online exams securely. This software turns any computer into a secure workstation. It controls access to any utility and prevents students from using unauthorized resources.

MantisBT

MantisBT

Mantis is an easy-to-deploy web-based defect tracking tool designed to aid in product defect tracking. It requires PHP, MySQL and a web server. Check out our demo and hosting services.

SecLists

SecLists

SecLists is the ultimate security tester's companion. It is a collection of various types of lists that are frequently used during security assessments, all in one place. SecLists helps make security testing more efficient and productive by conveniently providing all the lists a security tester might need. List types include usernames, passwords, URLs, fuzzing payloads, sensitive data patterns, web shells, and more. The tester can simply pull this repository onto a new test machine and he will have access to every type of list he needs.

ZendStudio 13.5.1 Mac

ZendStudio 13.5.1 Mac

Powerful PHP integrated development environment