Android:让元素在屏幕上随机移动?

时间:2014-08-08 13:57:15

标签: android textview xml-layout

我目前有一个非常简单的Android Activity和相应的XML。 (如下所示)

我想添加每个TextView在屏幕上随机移动的能力,这样用户可以更有趣地查看。

我该怎么办?

活动:

public class thirdActivity extends ActionBarActivity {

    TextView count;
    TextView avgMed;
    TextView avgAtt;

    DatabaseHelper db;

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

        initialiseVars();

        displayAllResults();

    }

    public void initialiseVars() {

        db = new DatabaseHelper(this);
        count = (TextView) findViewById(R.id.tvSummary1);
        avgMed = (TextView) findViewById(R.id.tvSummary2);
        avgAtt = (TextView) findViewById(R.id.tvSummary3);

    }

    // Facade method that then calls all the rest
    public void displayAllResults() {

        displayNumberofGamesPlayed();

        displayAverageOfAllMedValues();

        displayAverageOfAllAttValues();
    }

    public void displayNumberofGamesPlayed() {

        // show how many games have been played
        int totalGamesPlayed = db.getTotalGamesPlayed();
        count.setText("Games" + "\n" + "Played: " + "\n" + totalGamesPlayed);

    }

    public void displayAverageOfAllMedValues() {

        // Setting cursor to return value of the method?
        int total = db.getTotalOfAllAvgMedLevels();

        int gamesPlayed = db.getTotalGamesPlayed();

        int average1 = total / gamesPlayed;

        avgMed.setText("Average" + "\n" + "Mediation" + "\n" + "Level:" + "\n"
                + average1);

    }

    public void displayAverageOfAllAttValues() {

        // Setting cursor to return value of the method?
        int total = db.getTotalOfAllAvgAttLevels();

        int gamesPlayed = db.getTotalGamesPlayed();

        int average2 = total / gamesPlayed;

        avgAtt.setText("Average" + "\n" + "Attention" + "\n" + "Level:" + "\n"
                + average2);

    }

}

对应的XML:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >

    <TextView
        android:id="@+id/tvSummary1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:background="@drawable/circle"
        android:gravity="center"
        android:text=" " 
        android:textSize="25sp"/>

    <TextView
        android:id="@+id/tvSummary2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:background="@drawable/circle"
        android:gravity="center"
        android:text=" "
        android:textSize="25sp" />

    <TextView
        android:id="@+id/tvSummary3"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:background="@drawable/circle"
        android:gravity="center"
        android:text=" " 
        android:textSize="25sp"/>

</LinearLayout>

1 个答案:

答案 0 :(得分:0)

您可以使用具有随机值的ObjectAnimator,请检查以下答案: How to get random number on Android Object Animator