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

效果:

 

转载于:https://www.cnblogs.com/loaderman/p/10196724.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
你可以通过使用属性动画来实现点击 ImageView 图片转圈的效果。下面是一个简单示例代码: ```java import android.animation.ObjectAnimator; import android.support.v7.app.AppCompatActivity; import android.os.Bundle; import android.view.View; import android.view.animation.LinearInterpolator; import android.widget.ImageView; public class MainActivity extends AppCompatActivity { private ImageView imageView; private ObjectAnimator rotationAnimator; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); imageView = findViewById(R.id.imageView); // 设置属性动画 rotationAnimator = ObjectAnimator.ofFloat(imageView, "rotation", 0f, 360f); rotationAnimator.setDuration(1000); // 设置动画时长为1秒 rotationAnimator.setInterpolator(new LinearInterpolator()); // 设置动画插值器为线性插值器 rotationAnimator.setRepeatCount(ObjectAnimator.INFINITE); // 设置动画重复次数为无限次 rotationAnimator.setRepeatMode(ObjectAnimator.RESTART); // 设置动画重复模式为重新开始 imageView.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { if (rotationAnimator.isRunning()) { rotationAnimator.cancel(); // 如果动画正在进行,则取消动画 } else { rotationAnimator.start(); // 否则开始动画 } } }); } } ``` 这段代码首先获取到一个 ImageView 实例,然后创建了一个属性动画 `rotationAnimator` 来实现图片的旋转效果。在点击 ImageView 的时候,通过判断动画是否正在进行来决定是开始动画还是取消动画。注意需要在布局文件中添加一个 ImageView,并将其 id 设置为 `imageView`。 希望这可以帮到你!如果还有其他问题,请随时提问。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值