畫球屬性動畫平移跳轉頁面
阿新 • • 發佈:2018-11-09
一:首先自定義類:
public class ClerView extends View { private Paint paint; public ClerView(Context context) { super(context); init(); } public ClerView(Context context, @Nullable AttributeSet attrs) { super(context, attrs); init(); } public ClerView(Context context, @Nullable 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); } }
二:定義佈局
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" tools:context=".activity.WelcomeActivity"> <com.example.moni.widget.ClerView android:layout_width="wrap_content" android:layout_height="wrap_content" android:id="@+id/dd"/> </LinearLayout>
三:主頁面
public class WelcomeActivity extends AppCompatActivity { private ClerView cv; private int item = 2; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_welcome); cv = findViewById(R.id.dd); initData(); } private void initData() { final ValueAnimator animator = ValueAnimator.ofFloat(0f,500.15f); animator.setTarget(cv); animator.setDuration(2000); animator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() { @Override public void onAnimationUpdate(ValueAnimator valueAnimator) { Float mValue = (Float) animator.getAnimatedValue(); cv.setTranslationX(mValue); cv.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(WelcomeActivity.this,MainActivity.class); startActivity(intent); } handler.postDelayed(this,2000); } },2000); } }