exam01-3


/res/anim/slide_in_left

<?xml version="1.0" encoding="utf-8"?>

<set xmlns:android="http://schemas.android.com/apk/res/android">
    <translate
        android:duration="1000"
        android:fromXDelta="-100%p"
        android:repeatCount="-1"
        android:repeatMode="restart"
        android:toXDelta="0" />
</set>

/res/anim/slide_in_top

<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android">

    <translate
        android:toYDelta="90%p"
        android:repeatCount="-1"
        android:repeatMode="restart"
        android:fromYDelta="0"
        android:duration="1000"/>
</set>

activity.xml

<?xml version="1.0" encoding="utf-8"?>
<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"
    tools:context=".MainActivity"
    android:orientation="vertical"
    android:layout_margin="10dp">

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="#fff"
        android:orientation="horizontal">
        <TextView
            android:id="@+id/textView_char"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:textColor="#00f"
            android:textSize="20sp"
            android:text="Char Count:0" />

        <TextView
            android:id="@+id/textView_circle"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:textColor="#00f"
            android:textSize="20sp"
            android:text="Circle Count:0" />

    </LinearLayout>

    <FrameLayout
        android:id="@+id/showView"
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="1"
        android:background="#0f0">
    </FrameLayout>

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal">


        <Button
            android:id="@+id/button_add_char"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_margin="10dp"
            android:layout_weight="1"
            android:onClick="addChar"
            android:text="Add Char"
            android:textAllCaps="false" />

        <Button
            android:id="@+id/button_clear"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_margin="10dp"
            android:layout_weight="1"
            android:onClick="clearAll"
            android:text="Clear"
            android:textAllCaps="false" />

        <Button
            android:id="@+id/button_add_circle"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_margin="10dp"
            android:layout_weight="1"
            android:onClick="addCircle"
            android:text="Add Circle"
            android:textAllCaps="false" />

    </LinearLayout>
</LinearLayout>

CircleView.java

package com.example.animator;

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

import java.util.Random;

public class CircleView extends View {
    Random random = new Random();
    public CircleView(Context context) {
        super(context);
    }

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

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        Paint paint = new Paint();
        paint.setColor(Color.BLUE);
        paint.setStrokeWidth(5);
        paint.setStyle(Paint.Style.STROKE);

        int cy = random.nextInt(getHeight());

        // 圆形
        canvas.drawCircle(getWidth() - 30, cy, 30, paint);
    }
}

MainActivity.java

package com.example.animator;

import android.graphics.Color;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.widget.FrameLayout;
import android.widget.TextView;

import java.util.Random;

public class MainActivity extends AppCompatActivity {

    TextView textView_char;
    int charCount = 1;
    TextView textView_circle;
    int circleCount = 1;

    FrameLayout showView;
    Random rand = new Random();
    Random randChar = new Random();
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        initView();
    }

    public void initView(){
        showView = findViewById(R.id.showView);
        textView_char = findViewById(R.id.textView_char);
        textView_circle = findViewById(R.id.textView_circle);
    }

    public void addChar(View view) {
        TextView textView = new TextView(this);
        textView.setText("" + (char) ('A' + rand.nextInt(26)));
        textView.setTextColor(Color.RED);

        //载入动画
        Animation in = AnimationUtils.loadAnimation(this, R.anim.slide_in_top);
        in.setRepeatCount(Animation.INFINITE);
        textView.setAnimation(in);
        textView.setTextSize(30.0f);

        FrameLayout.LayoutParams lp = new FrameLayout.LayoutParams(FrameLayout.LayoutParams.WRAP_CONTENT, FrameLayout.LayoutParams.WRAP_CONTENT);

        lp.leftMargin = rand.nextInt(showView.getWidth());

        showView.addView(textView, lp);
        textView_char.setText("Char Count:" + charCount++);
    }
    public void clearAll(View view) {
        showView.removeAllViews();
        showView.clearDisappearingChildren();
        charCount = 0;
        circleCount = 0;

        textView_char.setText("Char Count:" + charCount++);
        textView_circle.setText("Circle Count:" + circleCount++);
    }

    public void addCircle(View view) {
        CircleView circleView = new CircleView(this);

        Animation in = AnimationUtils.loadAnimation(this, R.anim.slide_in_left);
        in.setRepeatCount(Animation.INFINITE);
        circleView.setAnimation(in);

        showView.addView(circleView);
        textView_circle.setText("Circle Count:" + circleCount++);
    }
}

原文地址:https://www.cnblogs.com/lyszyl/p/10651992.html