Guía de desarrollo: uso de animación CSS para implementar efectos dinámicos de HarmonyOS (2)

Nota: El contenido de este artículo se comparte y reproduce de la documentación del sitio web oficial de HarmonyOS Developer.

Haga clic para ver la "Guía de desarrollo: uso de animación CSS para realizar efectos de movimiento de HarmonyOS (1)"

3. Animación de estilo de posición de fondo

Mueva la posición de la imagen de fondo cambiando la propiedad de posición de fondo (el primer valor es la posición del eje X y el segundo valor es la posición del eje Y). Si la posición de la imagen de fondo excede el componente, el exceso de imagen de fondo no se mostrará.

<!-- xxx.hml --><div class="container">  <div class="content"></div>  <div class="content1"></div></div>

/* xxx.css */.container {
   
     height: 100%;  background-color:#F1F3F5;  display: flex;  flex-direction: column;  justify-content: center;  align-items: center;  width: 100%;}.content{
   
     width: 400px;  height: 400px;  /* 不建议图片长宽比为1:1 */  background-image: url('common/images/bg-tv.jpg');  background-size: 100%;  background-repeat: no-repeat;  animation: change 3s infinite;  border: 1px solid black;}.content1{
   
     margin-top:50px;  width: 400px;  height: 400px;  background-image: url('common/images/bg-tv.jpg');  background-size: 50%;  background-repeat: no-repeat;  animation: change1 5s infinite;  border: 1px solid black;}/* 背景图片移动出组件 */@keyframes change{
   
     0%{
   
       background-position:0px top;  }  25%{
   
       background-position:400px top;  }  50%{
   
       background-position:0px top;  }  75%{
   
       background-position:0px bottom;  }  100%{
   
       background-position:0px top;  }}/* 背景图片在组件内移动 */@keyframes change1{
   
     0%{
   
       background-position:left top;  }  25%{
   
       background-position:50% 50%;  }  50%{
   
       background-position:right bottom;  }  100%{
   
       background-position:left top;;  }}

ilustrar

La posición de fondo solo admite el movimiento de imágenes de fondo, pero no el color de fondo.

4. animación svg

Agregue efectos de animación a componentes svg.

Animación de estilo de propiedad

En el subcomponente animado de Svg , establezca los atributos que deben animarse a través de atributoName, establezca el valor inicial desde y establezca el valor final hasta.

<!-- xxx.hml --><div class="container">  <svg>    <text x="300" y="300" fill="blue">      Hello      <animate attributeName="font-size" from="30" to="60" dur="3s" repeatCount="indefinite">      </animate>      <animate attributeName="fill" from="red" to="blue" dur="3s" repeatCount="indefinite">      </animate>      <animate attributeName="opacity" from="1" to="0.3" dur="3s" repeatCount="indefinite">      </animate>    </text>    <text x="300" y="600" fill="blue">      World      <animate attributeName="font-size" from="30" to="60" values="30;80" dur="3s" repeatCount="indefinite">      </animate>      <animate attributeName="fill" from="red" to="blue"  dur="3s" repeatCount="indefinite">      </animate>      <animate attributeName="opacity" from="0.3" to="1" dur="3s" repeatCount="indefinite">      </animate>    </text>  </svg></div>

ilustrar

Al configurar el valor de cambio de animación, si se ha establecido el atributo de valores, tanto desde como hasta no serán válidos.

animación de ruta

En el subcomponente animateMotion de Svg , establezca la ruta del cambio de animación a través de la ruta.

<!-- xxx.hml --><div class="container">  <svg fill="white" width="800" height="900">    <path d="M300,200 h-150 a150 150 0 1 0 150 -150 z" fill="white" stroke="blue" stroke-width="5" >    </path>    <path fill="red" d="M-5,-5 L10,0 L-5,5 L0,0 Z"  >      <animateMotion dur="2000" repeatCount="indefinite" rotate="auto-reverse"path="M300,200 h-150 a150 150 0 1 0 150 -150 z">      </animateMotion>    </path>  </svg></div>

animarTransformar animación

En el subcomponente animateTransform de Svg , vincule el atributo de transformación a través de atributoName, establezca el tipo de animación para tipo, establezca el valor inicial para desde y establezca el valor final para hasta.

<!-- xxx.hml --><div class="container" style="">    <svg>        <line x1="90" y1="300" x2="90" y2="730" stroke-width="10" stroke="black" stroke-linecap="round">            <animateTransform attributeName="transform" attributeType="XML" type="translate"  dur="3s" values="0;30;10;30;20;30;25;30" keyTimes="0;0.3;0.5;0.7;0.8;0.9;1.0;1.1"                              fill="freeze">            </animateTransform>        </line>        <circle cx="500" cy="500" r="50" stroke-width="15" fill="red" stroke="#e70d0d">            <animateTransform attributeName="transform" attributeType="XML" type="rotate"  dur="3s" values="0;30;10;30;20;30;25;30" keyTimes="0;0.3;0.5;0.7;0.8;0.9;1.0;1.1" fill="freeze">            </animateTransform>            <animateTransform attributeName="transform" attributeType="XML" type="scale"  dur="6s" values="1;1;1.3" keyTimes="0;0.5;1" fill="freeze"></animateTransform>            <animateTransform attributeName="transform" attributeType="XML" type="translate"  dur="9s" values="0;0;300 7" keyTimes="0;0.6;0.9" fill="freeze"></animateTransform>        </circle>        <line x1="650" y1="300" x2="650" y2="600" stroke-width="20" stroke="blue" stroke-linecap="round">            <animateTransform attributeName="transform" attributeType="XML" type="translate"  dur="9s" values="0;0;0 800" keyTimes="0;0.6;1" fill="freeze"></animateTransform>        </line>    </svg></div>

/* xxx.css */.container {
   
     flex-direction: column;  align-items: center;  width: 100%;  height: 100%;  background-color: #F1F3F5;}

Supongo que te gusta

Origin blog.csdn.net/HarmonyOSDev/article/details/132670565
Recomendado
Clasificación