MainActivity
import android.animation.ValueAnimator;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.support.v7.app.AppCompatActivity;
import com.example.montchcode.MyView.MyViews;
import com.example.montchcode.activity.HomeActivity;
public class MainActivity extends AppCompatActivity {
private MyViews mv;
int item=2;
Handler handler=new Handler();
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mv = (MyViews) findViewById(R.id.mv);
initData();
}
private void initData() {
// 通过静态方法构建一个ValueAnimator对象
// 设置数值集合
ValueAnimator animator = ValueAnimator.ofFloat(0f, 200.0f);
// 设置作用对象
animator.setTarget(mv);
// 设置执行时间(1000ms)
animator.setDuration(2000);
// 添加动画更新监听
animator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
@Override
public void onAnimationUpdate(ValueAnimator animation) {
// 获取当前值
Float mValue = (Float) animation.getAnimatedValue();
// 设置横向偏移量
mv.setTranslationX(mValue);
// 设置纵向偏移量
mv.setTranslationY(mValue);
}
});
// 开始动画
animator.start();
//倒计时2秒跳转
final Handler handler=new Handler();
handler.postDelayed(new Runnable() {
@Override
public void run() {
item--;
if (item==1){
Intent intent=new Intent(MainActivity.this, HomeActivity.class);
startActivity(intent);
}
handler.postDelayed(this,2000);
}
},2000);
}
}
activity_main布局
<?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"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context="com.example.montchcode.MainActivity">
<com.example.montchcode.MyView.MyViews
android:id="@+id/mv"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</RelativeLayout>
自定义View
import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.util.AttributeSet;
import android.view.View;
public class MyViews extends View {
private Paint paint;
public MyViews(Context context) {
super(context);
init();
}
public MyViews(Context context, AttributeSet attrs) {
super(context, attrs);
init();
}
public MyViews(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
init();
}
private void init() {
paint = new Paint();
paint.setAntiAlias(true);
//圆,空心圆
paint.setColor(Color.BLUE);
}
@Override
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);
canvas.drawCircle(50,50,50,paint);
}
}