ICode9

精准搜索请尝试: 精确搜索
首页 > 其他分享> 文章详细

属性动画

2019-07-08 21:40:00  阅读:187  来源: 互联网

标签:动画 img view scaleX import 属性 id animator


属性动画

JAVA代码实现

代码

package com.example.day02;

import android.animation.Animator;
import android.animation.AnimatorInflater;
import android.animation.ObjectAnimator;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.ImageView;

import org.xutils.view.annotation.ContentView;
import org.xutils.view.annotation.Event;
import org.xutils.view.annotation.ViewInject;
import org.xutils.x;

@ContentView(R.layout.activity_java_)
public class Java_Activity extends AppCompatActivity {

    @ViewInject(R.id.img_java)
    ImageView img;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_java_);
        x.view().inject(this);
        img.setImageResource(R.drawable.backs);
    }

//按钮点击事件
    @Event(value = {R.id.one,R.id.two,R.id.three,R.id.fore},type = View.OnClickListener.class)
    private void click(View view){
    //判断按钮实现动画效果
        switch (view.getId()){
            case R.id.one:
                translate();
                break;
            case R.id.two:
                alpha();
                break;
            case R.id.three:
                rotate();
                break;
            case R.id.fore:
                scale();
                break;
        }
    }

    private void scale() {
        ObjectAnimator scaleX = ObjectAnimator.ofFloat(img, "scaleX", 1.0f, 05.f);
        scaleX.setDuration(2000);
        scaleX.setRepeatCount(2);
        scaleX.setRepeatMode(ObjectAnimator.REVERSE);
        scaleX.start();
    }

    private void rotate() {
        ObjectAnimator scaleX = ObjectAnimator.ofFloat(img, "rotation",0.f,360.f,0.25f,0.25f);
        scaleX.setDuration(2000);
        scaleX.setRepeatCount(2);
        scaleX.setRepeatMode(ObjectAnimator.REVERSE);
        scaleX.start();
    }

    private void alpha() {
        ObjectAnimator scaleX = ObjectAnimator.ofFloat(img, "alpha",1.0f,0.5f);
        scaleX.setDuration(2000);
        scaleX.setRepeatCount(2);
        scaleX.setRepeatMode(ObjectAnimator.REVERSE);
        scaleX.start();
    }

    private void translate() {
        ObjectAnimator scaleX = ObjectAnimator.ofFloat(img, "translationX",0.0f,300.0f);
        //设置持续时间
        scaleX.setDuration(2000);
        //设置执行次数
        scaleX.setRepeatCount(2);
        //设置执行模式
        scaleX.setRepeatMode(ObjectAnimator.REVERSE);
        scaleX.start();
    }
}

布局文件

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    tools:context=".Java_Activity">

    <Button
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:id="@+id/one"
        android:text="平移"
        />
    <Button
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:id="@+id/two"
        android:text="透明"
        />
    <Button
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:id="@+id/three"
        android:text="旋转"
        />
    <Button
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:id="@+id/fore"
        android:text="缩放"
        />
    <ImageView
        android:layout_width="150dp"
        android:layout_height="150dp"
        android:id="@+id/img_java"
        />

</LinearLayout>

Xml实现

注:”Xml实现需要在res文件下创建animator文件夹

代码

package com.example.day02;

import android.animation.Animator;
import android.animation.AnimatorInflater;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.ImageView;

import org.xutils.view.annotation.ContentView;
import org.xutils.view.annotation.Event;
import org.xutils.view.annotation.ViewInject;
import org.xutils.x;

@ContentView(R.layout.activity_xml_)
public class Xml_Activity extends AppCompatActivity {

    @ViewInject(R.id.img_xml)
    ImageView img;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_xml_);
        x.view().inject(this);
        img.setImageResource(R.drawable.backs);
    }

    @Event(value = {R.id.one,R.id.two,R.id.three,R.id.fore},type = View.OnClickListener.class)
    private void click(View view){
        switch (view.getId()){
            case R.id.one:
                translate();
                break;
            case R.id.two:
                alpha();
                break;
            case R.id.three:
                rotate();
                break;
            case R.id.fore:
                scale();
                break;
        }
    }

    private void scale() {
        Animator animator = AnimatorInflater.loadAnimator(this,R.animator.scale);
        animator.setTarget(img);
        animator.start();
    }

    private void rotate() {
        Animator animator = AnimatorInflater.loadAnimator(this,R.animator.rotate);
        animator.setTarget(img);
        animator.start();
    }

    private void alpha() {
        Animator animator = AnimatorInflater.loadAnimator(this,R.animator.alpha);
        animator.setTarget(img);
        animator.start();
    }

    private void translate() {
        Animator animator = AnimatorInflater.loadAnimator(this,R.animator.translate);
        animator.setDuration(2000);
        animator.setTarget(img);
        animator.start();
    }
}

布局文件

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:orientation="vertical"
    android:layout_height="match_parent"
    tools:context=".Xml_Activity">

    <Button
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:id="@+id/one"
        android:text="平移"
        />
    <Button
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:id="@+id/two"
        android:text="透明"
        />
    <Button
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:id="@+id/three"
        android:text="旋转"
        />
    <Button
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:id="@+id/fore"
        android:text="缩放"
        />
    <ImageView
        android:layout_width="150dp"
        android:layout_height="150dp"
        android:id="@+id/img_xml"
        />

</LinearLayout>

透明动画效果布局

<?xml version="1.0" encoding="utf-8"?>
<objectAnimator xmlns:android="http://schemas.android.com/apk/res/android"
    android:propertyName="Alpha"
    android:valueFrom="0.0"
    android:valueTo="1.0"
    android:repeatCount="2"
    android:repeatMode="reverse"
    android:duration="5000"
    >

</objectAnimator>

旋转动画效果布局

<?xml version="1.0" encoding="utf-8"?>
<objectAnimator xmlns:android="http://schemas.android.com/apk/res/android"
    android:propertyName="rotation"
    android:valueFrom="0"
    android:valueTo="360"
    android:duration="2000"
    android:repeatMode="reverse"
    android:repeatCount="2"
    >

</objectAnimator>

平移动画效果布局

<?xml version="1.0" encoding="utf-8"?>
<objectAnimator xmlns:android="http://schemas.android.com/apk/res/android"
    android:propertyName="translationX"
    android:valueFrom="0"
    android:valueTo="300"
    android:repeatCount="2"
    >

</objectAnimator>

缩放动画效果布局

<?xml version="1.0" encoding="utf-8"?>
<objectAnimator xmlns:android="http://schemas.android.com/apk/res/android"
    android:propertyName="scaleX"
    android:valueFrom="1.0"
    android:valueTo="0.5"
    android:repeatCount="2"
    android:repeatMode="reverse"
    android:duration="2000"
    >

</objectAnimator>

标签:动画,img,view,scaleX,import,属性,id,animator
来源: https://blog.csdn.net/wangwei_weibo/article/details/95100809

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有