简单的实现一下自定义圆然后平移的小动画


//首先在布局当中加入你所创建的Circle类的控件

//下面的Circle是你自己的类

<demo01.com.xiaoyuandian.Circle
    android:id="@+id/cicle"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content" />


//创建Circle类 继承View

//实现了画圆的功能

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.util.AttributeSet;
import android.view.View;



public class Circle extends View {
    public Circle(Context context) {
        super(context);
    }

    public Circle(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    public Circle(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        Paint p = new Paint();
        p.setColor(getResources().getColor(R.color.colorPrimary));
        p.setStrokeWidth(20);
        canvas.drawCircle(canvas.getWidth() / 2, canvas.getHeight() / 2, 50, p);
    }
}
 
  
 
  
//下面是用Acitivity调用你的类
 
  
import android.animation.AnimatorSet;
import android.animation.ObjectAnimator;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;

public class MainActivity extends AppCompatActivity {

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

        Circle c= (Circle) findViewById(R.id.cicle);

        ObjectAnimator transkationY = ObjectAnimator.ofFloat(c,"translationY", 0, 0f);
        ObjectAnimator transkationX = ObjectAnimator.ofFloat(c,"translationX", 0, 300f);
        AnimatorSet animatorSet = new AnimatorSet();
        animatorSet.play(transkationY).with(transkationX);
        animatorSet.setDuration(3000);
        animatorSet.start();
    }
}
.//到这里就完结了 主要实现的就是自定义画圆 然后实现平移的状态

你可能感兴趣的:(简单的实现一下自定义圆然后平移的小动画)