zoukankan      html  css  js  c++  java
  • ObjectAnimator简单示例

    <?xml version="1.0" encoding="utf-8"?>
    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:tools="http://schemas.android.com/tools"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal"
        android:layout_gravity="center"
        tools:context="com.loaderman.customviewdemo.MainActivity">
    
        <TableLayout
            android:visibility="gone"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content">
    
            <TableRow>
    
                <Button
                    android:id="@+id/start_alpha"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:text="alpha"/>
    
                <Button
                    android:id="@+id/start_rotation"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:text="rotation"/>
    
                <Button
                    android:id="@+id/start_rotationX"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:text="rotationX"/>
    
    
            </TableRow>
    
    
            <TableRow>
    
                <Button
                    android:id="@+id/start_rotationY"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:text="rotationY"/>
    
                <Button
                    android:id="@+id/start_translationX"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:text="translationX"/>
    
                <Button
                    android:id="@+id/start_translationY"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:text="translationY"/>
            </TableRow>
    
            <TableRow>
    
                <Button
                    android:id="@+id/start_scaleX"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:text="scaleX"/>
    
                <Button
                    android:id="@+id/start_scaleY"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:text="scaleY"/>
    
    
            </TableRow>
        </TableLayout>
    
        <TextView
            android:visibility="gone"
            android:id="@+id/tv"
            android:layout_marginLeft="30dp"
            android:layout_width="50dp"
            android:layout_height="20dp"
            android:gravity="center"
            android:text="启舰"
            android:layout_marginTop="100dp"
            android:layout_gravity="center"
            android:background="#000000"
            android:textColor="#ffffff"/>
    
    
        <RelativeLayout
            android:layout_width="fill_parent"
            android:layout_height="fill_parent">
    
            <Button
                android:id="@+id/demobtn"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="start anim"/>
    
            <com.loaderman.customviewdemo.CustomTextView
                android:id="@+id/customtv"
                android:layout_toRightOf="@id/demobtn"
                android:layout_marginLeft="30dp"
                android:layout_width="50dp"
                android:layout_height="20dp"
                android:gravity="center"
                android:text="Hello"
                android:background="#000000"
                android:textColor="#ffffff"/>
    
    
        </RelativeLayout>
    
    </LinearLayout>
    package com.loaderman.customviewdemo;
    
    import android.animation.ObjectAnimator;
    import android.os.Bundle;
    import android.support.v7.app.AppCompatActivity;
    import android.view.View;
    import android.widget.TextView;
    
    public class MainActivity extends AppCompatActivity {
        private TextView tv;
    
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
    
            final CustomTextView tv2 = (CustomTextView) findViewById(R.id.customtv);
            findViewById(R.id.demobtn).setOnClickListener(new View.OnClickListener() {
                public void onClick(View v) {
                    ObjectAnimator animator = ObjectAnimator.ofFloat(tv2, "ScaleSize", 6);
                    animator.setDuration(2000);
                    animator.start();
                }
            });
    
            tv = (TextView) findViewById(R.id.tv);
    
            findViewById(R.id.start_alpha).setOnClickListener(new View.OnClickListener() {
                public void onClick(View v) {
                    /**
                     * 实现alpha值变化
                     */
                    ObjectAnimator animator = ObjectAnimator.ofFloat(tv, "alpha", 1, 0, 1);
                    animator.setDuration(2000);
                    animator.start();
                }
            });
    
            findViewById(R.id.start_rotation).setOnClickListener(new View.OnClickListener() {
                public void onClick(View v) {
                    /**
                     * Z轴旋转
                     */
                    ObjectAnimator animator = ObjectAnimator.ofFloat(tv, "rotation", 0, 270, 0);
                    animator.setDuration(2000);
                    animator.start();
                }
            });
    
            findViewById(R.id.start_rotationX).setOnClickListener(new View.OnClickListener() {
                public void onClick(View v) {
                    /**
                     * RotationX旋转
                     */
                    ObjectAnimator animator = ObjectAnimator.ofFloat(tv, "rotationX", 0, 270, 0);
                    animator.setDuration(2000);
                    animator.start();
                }
            });
    
            findViewById(R.id.start_rotationY).setOnClickListener(new View.OnClickListener() {
                public void onClick(View v) {
                    /**
                     * RotationY 旋转
                     */
                    ObjectAnimator animator = ObjectAnimator.ofFloat(tv, "rotationY", 0, 180, 0);
                    animator.setDuration(2000);
                    animator.start();
    
                }
            });
    
            findViewById(R.id.start_translationX).setOnClickListener(new View.OnClickListener() {
                public void onClick(View v) {
                    /**
                     * translationX动画
                     */
                    ObjectAnimator animator = ObjectAnimator.ofFloat(tv, "translationX", 0, 200, -200, 0);
                    animator.setDuration(2000);
                    animator.start();
                }
            });
    
            findViewById(R.id.start_translationY).setOnClickListener(new View.OnClickListener() {
                public void onClick(View v) {
                    /**
                     * translationY动画
                     */
                    ObjectAnimator animator = ObjectAnimator.ofFloat(tv, "translationY", 0, 200, -100, 0);
                    animator.setDuration(2000);
                    animator.start();
                }
            });
    
            findViewById(R.id.start_scaleX).setOnClickListener(new View.OnClickListener() {
                public void onClick(View v) {
                    /**
                     * scaleX缩放动画
                     */
                    ObjectAnimator animator = ObjectAnimator.ofFloat(tv, "scaleX", 0, 3, 1);
                    animator.setDuration(2000);
                    animator.start();
                }
            });
    
            findViewById(R.id.start_scaleY).setOnClickListener(new View.OnClickListener() {
                public void onClick(View v) {
    
                    /**
                     * scaleY缩放动画
                     */
                    ObjectAnimator animator = ObjectAnimator.ofFloat(tv, "scaleY", 0, 3, 1);
                    animator.setDuration(2000);
                    animator.start();
                }
            });
        }
    }
    package com.loaderman.customviewdemo;
    
    import android.content.Context;
    import android.util.AttributeSet;
    import android.widget.TextView;
    
    
    public class CustomTextView extends TextView {
        public CustomTextView(Context context) {
            super(context);
        }
    
        public CustomTextView(Context context, AttributeSet attrs) {
            super(context, attrs);
        }
    
        public CustomTextView(Context context, AttributeSet attrs, int defStyle) {
            super(context, attrs, defStyle);
        }
    
        public void setScaleSize(float num){
            setScaleX(num);
        }
    
        public float getScaleSize(){
            return 0.5f;
        }
    }

    效果:

  • 相关阅读:
    js全选,全不选,反选练习
    linux查看系统的一些命令,留着用
    JAVA中native方法
    应该被记住的 8 位 Java 人物
    iframe里面的iframe无法左右对齐的解决方法
    sql语句,查找合并后的结果
    地址路径过深时的处理方式
    惠普中国CEO孙振耀退休感言
    js获得readOnly属性
    直接加载错误页面void com.opensymphony.xwork2.ActionSupport.addActionError(String anErrorMessage)
  • 原文地址:https://www.cnblogs.com/loaderman/p/10196724.html
Copyright © 2011-2022 走看看