在屏幕上自动移动TextView

时间:2020-01-15 10:15:52

标签: android animation textview

我正在尝试使用TextView使translateAnimation沿屏幕在所有方向随机移动。我需要像在屏幕保护程序中一样移动文本,例如不断旋转直到被单击。但是有一些问题: 1.文字从上到下移动 2.它不会在屏幕边框中停止显示,而是从屏幕上消失然后再次返回:

public class aktivityStarted extends AppCompatActivity {

 TextView textMovin;
   /* int loc[]=new int[2];
    int x=loc[0];
    int y=loc[1];*/

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



        textMovin=findViewById(R.id.movingText);
        DisplayMetrics displaymetrics = new DisplayMetrics();
        getWindowManager().getDefaultDisplay().getMetrics(displaymetrics);
        final int width = displaymetrics.widthPixels-textMovin.getWidth();
        final int height = displaymetrics.heightPixels-textMovin.getHeight();

       final Random r = new Random();
        final int translationX = r.nextInt(width);
       final int translationY = r.nextInt(height);
        final int randomx=r.nextInt(50)+1;
        final int randomy=r.nextInt(50)+1;

        final TranslateAnimation anim = new TranslateAnimation(-translationX,translationX ,-translationY,translationY ); //Use current view position instead of `currentX` and `currentY`
        anim.setDuration(2500);

        anim.setRepeatCount(-1);
        anim.setAnimationListener(new Animation.AnimationListener() {
            @Override
            public void onAnimationStart(Animation animation) {

            }

            @Override
            public void onAnimationEnd(Animation animation) {
                anim.reset();
                anim.setRepeatMode(Animation.REVERSE);
                anim.setFillAfter(true);
            }

            @Override
            public void onAnimationRepeat(Animation animation) {
textMovin.setY(r.nextInt(height));
textMovin.setX(r.nextInt(width));
anim.start();
            }
        });



        textMovin.startAnimation(anim);

    }
}

2 个答案:

答案 0 :(得分:1)

尝试此代码:

public class MainActivity extends AppCompatActivity {

private View parent;
private TextView textMovin;
private float speedX;
private float speedY;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    textMovin = findViewById(R.id.textV);
    parent = findViewById(R.id.parent);
    final Random r = new Random();
    speedX = r.nextFloat() * 200;
    speedY = r.nextFloat() * 200;
    parent.getViewTreeObserver().addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
        @Override
        public void onGlobalLayout() {
            final int width = parent.getWidth() - textMovin.getWidth();
            final int height = parent.getHeight() - textMovin.getHeight();


            final int period = 50;
            new Timer().scheduleAtFixedRate(new TimerTask() {
                @Override
                public void run() {
                    textMovin.post(new TimerTask() {
                        @Override
                        public void run() {
                            textMovin.setX(speedX * period / 1000.0f + textMovin.getX());
                            textMovin.setY(speedY * period / 1000.0f + textMovin.getY());
                            if (textMovin.getY() <= 0 || textMovin.getY() >= height)
                                speedY *= -1;
                            if (textMovin.getX() <= 0 || textMovin.getX() >= width)
                                speedX *= -1;
                        }
                    });
                }
            }, 50, period);

        }
    });

    findViewById(R.id.random).setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            final Random r = new Random();
            speedX = r.nextFloat() * 200;
            speedY = r.nextFloat() * 200;
        }
    });

}

}

和布局:

<?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:id="@+id/parent"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity">

    <TextView
        android:id="@+id/textV"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerInParent="true"
        android:text="Hello World!" />

    <Button
        android:id="@+id/random"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:layout_centerHorizontal="true"
        android:layout_marginBottom="10dp"
        android:text="Random" />

</RelativeLayout>

答案 1 :(得分:0)

希望它对您有用。

public class OnDragTouchListener implements View.OnTouchListener {

    /**
     * Callback used to indicate when the drag is finished
     */
    public interface OnDragActionListener {
        /**
         * Called when drag event is started
         *
         * @param view The view dragged
         */
        void onDragStart(View view);

        /**
         * Called when drag event is completed
         *
         * @param view The view dragged
         */
        void onDragEnd(View view);
    }

    private View mView;
    private View mParent;
    private boolean isDragging;
    private boolean isInitialized = false;

    private int width;
    private float xWhenAttached;
    private float maxLeft;
    private float maxRight;
    private float dX;

    private int height;
    private float yWhenAttached;
    private float maxTop;
    private float maxBottom;
    private float dY;

    private OnDragActionListener mOnDragActionListener;

    public OnDragTouchListener(View view) {
        this(view, (View) view.getParent(), null);
    }

    public OnDragTouchListener(View view, View parent) {
        this(view, parent, null);
    }

    public OnDragTouchListener(View view, OnDragActionListener onDragActionListener) {
        this(view, (View) view.getParent(), onDragActionListener);
    }

    public OnDragTouchListener(View view, View parent, OnDragActionListener onDragActionListener) {
        initListener(view, parent);
        setOnDragActionListener(onDragActionListener);
    }

    public void setOnDragActionListener(OnDragActionListener onDragActionListener) {
        mOnDragActionListener = onDragActionListener;
    }

    public void initListener(View view, View parent) {
        mView = view;
        mParent = parent;
        isDragging = false;
        isInitialized = false;
    }

    public void updateBounds() {
        updateViewBounds();
        updateParentBounds();
        isInitialized = true;
    }

    public void updateViewBounds() {
        width = mView.getWidth();
        xWhenAttached = mView.getX();
        dX = 0;

        height = mView.getHeight();
        yWhenAttached = mView.getY();
        dY = 0;
    }

    public void updateParentBounds() {
        maxLeft = 0;
        maxRight = maxLeft + mParent.getWidth();

        maxTop = 0;
        maxBottom = maxTop + mParent.getHeight();
    }

    @Override
    public boolean onTouch(View v, MotionEvent event) {
        if (isDragging) {
            float[] bounds = new float[4];
            // LEFT
            bounds[0] = event.getRawX() + dX;
            if (bounds[0] < maxLeft) {
                bounds[0] = maxLeft;
            }
            // RIGHT
            bounds[2] = bounds[0] + width;
            if (bounds[2] > maxRight) {
                bounds[2] = maxRight;
                bounds[0] = bounds[2] - width;
            }
            // TOP
            bounds[1] = event.getRawY() + dY;
            if (bounds[1] < maxTop) {
                bounds[1] = maxTop;
            }
            // BOTTOM
            bounds[3] = bounds[1] + height;
            if (bounds[3] > maxBottom) {
                bounds[3] = maxBottom;
                bounds[1] = bounds[3] - height;
            }

            switch (event.getAction()) {
                case MotionEvent.ACTION_CANCEL:
                case MotionEvent.ACTION_UP:
                    onDragFinish();
                    break;
                case MotionEvent.ACTION_MOVE:
                    mView.animate().x(bounds[0]).y(bounds[1]).setDuration(0).start();
                    break;
            }
            return true;
        } else {
            switch (event.getAction()) {
                case MotionEvent.ACTION_DOWN:
                    isDragging = true;
                    if (!isInitialized) {
                        updateBounds();
                    }
                    dX = v.getX() - event.getRawX();
                    dY = v.getY() - event.getRawY();
                    if (mOnDragActionListener != null) {
                        mOnDragActionListener.onDragStart(mView);
                    }
                    return true;
            }
        }
        return false;
    }

    private void onDragFinish() {
        if (mOnDragActionListener != null) {
            mOnDragActionListener.onDragEnd(mView);
        }

        dX = 0;
        dY = 0;
        isDragging = false;
    }
}

您可以使用以下命令进行设置:

myView.setOnTouchListener(new OnDragTouchListener(myView));

或通过直接在“自定义视图”的init方法中添加它:

setOnTouchListener(new OnDragTouchListener(this));