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
RSS in XML: Unveiling the Core of Content SyndicationRSS in XML: Unveiling the Core of Content SyndicationApr 22, 2025 am 12:08 AM

The implementation of RSS in XML is to organize content through a structured XML format. 1) RSS uses XML as the data exchange format, including elements such as channel information and project list. 2) When generating RSS files, content must be organized according to specifications and published to the server for subscription. 3) RSS files can be subscribed through a reader or plug-in to automatically update the content.

Beyond the Basics: Advanced RSS Document FeaturesBeyond the Basics: Advanced RSS Document FeaturesApr 21, 2025 am 12:03 AM

Advanced features of RSS include content namespaces, extension modules, and conditional subscriptions. 1) Content namespace extends RSS functionality, 2) Extended modules such as DublinCore or iTunes to add metadata, 3) Conditional subscription filters entries based on specific conditions. These functions are implemented by adding XML elements and attributes to improve information acquisition efficiency.

The XML Backbone: How RSS Feeds are StructuredThe XML Backbone: How RSS Feeds are StructuredApr 20, 2025 am 12:02 AM

RSSfeedsuseXMLtostructurecontentupdates.1)XMLprovidesahierarchicalstructurefordata.2)Theelementdefinesthefeed'sidentityandcontainselements.3)elementsrepresentindividualcontentpieces.4)RSSisextensible,allowingcustomelements.5)Bestpracticesincludeusing

RSS & XML: Understanding the Dynamic Duo of Web ContentRSS & XML: Understanding the Dynamic Duo of Web ContentApr 19, 2025 am 12:03 AM

RSS and XML are tools for web content management. RSS is used to publish and subscribe to content, and XML is used to store and transfer data. They work with content publishing, subscriptions, and update push. Examples of usage include RSS publishing blog posts and XML storing book information.

RSS Documents: The Foundation of Web SyndicationRSS Documents: The Foundation of Web SyndicationApr 18, 2025 am 12:04 AM

RSS documents are XML-based structured files used to publish and subscribe to frequently updated content. Its main functions include: 1) automated content updates, 2) content aggregation, and 3) improving browsing efficiency. Through RSSfeed, users can subscribe and get the latest information from different sources in a timely manner.

Decoding RSS: The XML Structure of Content FeedsDecoding RSS: The XML Structure of Content FeedsApr 17, 2025 am 12:09 AM

The XML structure of RSS includes: 1. XML declaration and RSS version, 2. Channel (Channel), 3. Item. These parts form the basis of RSS files, allowing users to obtain and process content information by parsing XML data.

How to Parse and Utilize XML-Based RSS FeedsHow to Parse and Utilize XML-Based RSS FeedsApr 16, 2025 am 12:05 AM

RSSfeedsuseXMLtosyndicatecontent;parsingtheminvolvesloadingXML,navigatingitsstructure,andextractingdata.Applicationsincludebuildingnewsaggregatorsandtrackingpodcastepisodes.

RSS Documents: How They Deliver Your Favorite ContentRSS Documents: How They Deliver Your Favorite ContentApr 15, 2025 am 12:01 AM

RSS documents work by publishing content updates through XML files, and users subscribe and receive notifications through RSS readers. 1. Content publisher creates and updates RSS documents. 2. The RSS reader regularly accesses and parses XML files. 3. Users browse and read updated content. Example of usage: Subscribe to TechCrunch's RSS feed, just copy the link to the RSS reader.

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

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

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.

mPDF

mPDF

mPDF is a PHP library that can generate PDF files from UTF-8 encoded HTML. The original author, Ian Back, wrote mPDF to output PDF files "on the fly" from his website and handle different languages. It is slower than original scripts like HTML2FPDF and produces larger files when using Unicode fonts, but supports CSS styles etc. and has a lot of enhancements. Supports almost all languages, including RTL (Arabic and Hebrew) and CJK (Chinese, Japanese and Korean). Supports nested block-level elements (such as P, DIV),

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

DVWA

DVWA

Damn Vulnerable Web App (DVWA) is a PHP/MySQL web application that is very vulnerable. Its main goals are to be an aid for security professionals to test their skills and tools in a legal environment, to help web developers better understand the process of securing web applications, and to help teachers/students teach/learn in a classroom environment Web application security. The goal of DVWA is to practice some of the most common web vulnerabilities through a simple and straightforward interface, with varying degrees of difficulty. Please note that this software

ZendStudio 13.5.1 Mac

ZendStudio 13.5.1 Mac

Powerful PHP integrated development environment