android自定义动画平移旋转

本文展示了如何在Android中创建一个自定义动画,包括ImageView的平移、旋转、缩放和渐变效果。通过使用ObjectAnimator和AnimatorSet,动画在3秒内完成,结束后跳转到新的页面。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

//主方法

public class SplashActivity extends AppCompatActivity {
    private ProgressBarView pbv;
    private int progress = 120;
    private int time = 3;
    private Handler handler = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
            //动画运行时间为3秒钟,动画结束后跳转到商品详情页面。
            time--;
            if (time == 0) {
                startActivity(new Intent(SplashActivity.this, MainActivity.class));
                finish();
            } else {
                //设置动画播放进程
                progress += 120;
                pbv.setProgress(progress);
                handler.sendEmptyMessageDelayed(0, 1000);
            }
        }
    };

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_splash);

        //查找控件
        ImageView imageView = (ImageView) findViewById(R.id.logo_img);
        pbv = (ProgressBarView) findViewById(R.id.my_progess);
        setAnimation(imageView);
        handler.sendEmptyMessage(0);
        pbv.setProgress(progress);
    }

    //执行动画的方法
    private void setAnimation(ImageView imageView) {
        //应用图标从屏幕最上方平移到屏幕中间
        ObjectAnimator trans = ObjectAnimator.ofFloat(imageView, "translationY", 0f, 500f).setDuration(1000);
        //缩放由2倍到1倍
        ObjectAnimator scalX = ObjectAnimator.ofFloat(imageView, "scaleX",  2f, 1f).setDuration(1000);
        ObjectAnimator scalY = ObjectAnimator.ofFloat(imageView, "scaleY",  2f, 1f).setDuration(1000);
        //渐变从完全透明到完全不透明
        ObjectAnimator alpha = ObjectAnimator.ofFloat(imageView, "alpha", 0.0f, 1f).setDuration(1000);
        // 旋转为旋转一圈
        ObjectAnimator rotate = ObjectAnimator.ofFloat(imageView, "rotation", 0f, 360f).setDuration(1000);

        //动画组合开始执行
        AnimatorSet setAnimatior = new AnimatorSet();
        setAnimatior.play(trans).before(scalX).before(scalY).before(alpha).before(rotate);
        setAnimatior.start();
    }
}

  //自定义VIEW
public class ProgressBarView extends View {
    private Paint paint;
    private int currentX = 100;
    private int  currentY = 100;
    private int count;
    private PointF pointF = new PointF(currentX,currentY);
    private  int mProgress;


    public ProgressBarView(Context context) {
        super(context);
        initpaint(context);
    }

    private void initpaint(Context context) {
        paint = new Paint();
        paint.setAntiAlias(true);
        paint.setStyle(Paint.Style.STROKE);
    }

    public ProgressBarView(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
        initpaint(context);
    }

    public ProgressBarView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        initpaint(context);
    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        paint.setStrokeWidth(0);
        paint.setColor(Color.BLACK);
        canvas.drawCircle(pointF.x,pointF.y,20,paint);
        canvas.drawCircle(pointF.x,pointF.y,30,paint);

        paint.setStrokeWidth(10);
        paint.setColor(Color.RED);
        RectF recyF = new RectF(75,75,125,125);
        canvas.drawArc(recyF,-90,mProgress,false,paint);

        paint.setStrokeWidth(1);
        paint.setColor(Color.BLUE);
        canvas.drawText(count+"",98,102,paint);
    }

    public void setProgress(int progress){
        this.mProgress = progress;
        if (mProgress == 120){
            count = 2;
        }
        if (mProgress == 240){
            count = 1;
        }
        if (mProgress == 360){
            count = 0;
        }
        invalidate();
    }
}


//布局

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent" >

    <ImageView
        android:id="@+id/logo_img"
        android:layout_width="100dp"
        android:layout_height="100dp"
        android:src="@mipmap/ic_launcher"
        android:layout_centerHorizontal="true" />

    <com.bawei.chenkai.day14rikao.ProgressBarView
        android:visibility="gone"
        android:id="@+id/my_progess"
        android:layout_centerVertical="true"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />

</RelativeLayout>



                
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值