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;
    }
}

效果:

 

posted on 2018-12-29 16:30  LoaderMan  阅读(482)  评论(0编辑  收藏  举报

导航