添加TextView时,Android应用程序速度变慢/变得无响应

时间:2017-06-01 00:39:22

标签: android android-studio

我一直在开发这款Android游戏,我想知道为什么每当我添加这个TextView时,我的应用就会开始变得反应迟钝。比如,我的一些按钮不会按,而其他按钮不会按,依此类推。然后,当我从线性布局中删除TextView时,一切都恢复正常。请帮忙!

MainActivity

public class MainActivity extends AppCompatActivity {

boolean started = false;
boolean place;
boolean[] tiles = new boolean[60];
int prevId = 0;
int times = 0;
ArrayList<Integer> IDs = new ArrayList<>();

Typeface face;
TextView num;

@Override
protected void onCreate(Bundle savedInstanceState)
{
    super.onCreate(savedInstanceState);

    requestWindowFeature(Window.FEATURE_NO_TITLE);
    getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
            WindowManager.LayoutParams.FLAG_FULLSCREEN);

    setContentView(R.layout.activity_main);

    setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);

    num = (TextView) findViewById(R.id.count);

    face = Typeface.createFromAsset(getAssets(),"fonts/ZonaPro-Bold.otf");

    num.setTypeface(face);
}

public void press(View view)
{
    place = false;
    int id = view.getId() % 2131427400;
    String test = "" + id;

    ImageView img = (ImageView) view;

    Log.d("TEST", test);

    *****HERE, I HAVE LOTS OF LOGIC, AND IF-STATEMENTS INCLUDING SOME UNIMPORTANT VARIABLES*****

    int abs = Math.abs(Math.abs(id - prevId));

    if(place && (abs == 1 || abs == 8) && tiles[id] == false && !(img.getTag().toString().equals("bound")))
    {
        img.setImageResource(R.drawable.blue_sphere);
        tiles[id] = true;
        prevId = id;
        times++;
        IDs.add(id);
        num.setText("" + times);
    }
}

public void start(View view)
{
    ImageView v = (ImageView) view;

    if(!started)
    {
        started = true;
        v.setImageResource(R.drawable.blue_sphere);
        tiles[22] = true;
        prevId = 22;
        IDs.add(22);
        num = (TextView) findViewById(R.id.count);
        num.setText("0");
    }
}

public void undo(View view)
{

    for(int ID : IDs)
    {
        ImageView re = (ImageView) findViewById(2131427400 + ID);
        Log.d("TAG", re.getTag().toString());

        if(re.getTag().toString().equals("tile"))
            re.setImageResource(R.drawable.gray_block);
        else if(re.getTag().toString().equals("start"))
        {
            re.setImageResource(R.drawable.start_tile);
            started = false;
        }
        else if(re.getTag().toString().equals("bound"))
        {
            re.setImageResource(R.drawable.newer_x);
        }
        else if(re.getTag().toString().equals("goal"))
        {
            re.setImageResource(R.drawable.new_goal);
        }
    }

    Arrays.fill(tiles, false);
    times = 0;
}
}

Activity_Main.xml(所有这些布局都在一个更大的水平线性布局中)

<!-- 1 -->
<LinearLayout
    android:layout_width="0dp"
    android:layout_height="fill_parent"
    android:layout_weight="1"
    android:orientation="vertical"
    android:weightSum="9">

    <ImageView
        android:layout_width="fill_parent"
        android:layout_height="0dp"
        android:id="@+id/_11"
        app:srcCompat="@drawable/gray_block"
        android:layout_weight="1"
        android:onClick="press"
        android:tag="tile" />

    <ImageView
        android:layout_width="fill_parent"
        android:layout_height="0dp"
        android:id="@+id/_12"
        app:srcCompat="@drawable/gray_block"
        android:layout_weight="1"
        android:onClick="press"
        android:tag="tile"/>

    <ImageView
        android:layout_width="fill_parent"
        android:layout_height="0dp"
        android:id="@+id/_13"
        app:srcCompat="@drawable/gray_block"
        android:layout_weight="1"
        android:onClick="press"
        android:tag="tile"/>

    <ImageView
        android:layout_width="fill_parent"
        android:layout_height="0dp"
        android:id="@+id/_14"
        app:srcCompat="@drawable/newer_x"
        android:layout_weight="1"
        android:onClick="press"
        android:tag="bound"/>

    <ImageView
        android:layout_width="fill_parent"
        android:layout_height="0dp"
        android:id="@+id/_15"
        app:srcCompat="@drawable/gray_block"
        android:layout_weight="1"
        android:onClick="press"
        android:tag="tile"/>

    <ImageView
        android:layout_width="fill_parent"
        android:layout_height="0dp"
        android:id="@+id/_16"
        app:srcCompat="@drawable/gray_block"
        android:layout_weight="1"
        android:onClick="press"
        android:tag="tile"/>

    <ImageView
        android:layout_width="fill_parent"
        android:layout_height="0dp"
        android:id="@+id/_17"
        app:srcCompat="@drawable/gray_block"
        android:layout_weight="1"
        android:onClick="press"
        android:tag="tile"/>

    <ImageView
        android:layout_width="fill_parent"
        android:layout_height="0dp"
        android:id="@+id/_18"
        app:srcCompat="@drawable/start_tile"
        android:layout_weight="1"
        android:onClick="start"
        android:tag="start" />

****this is the text view I'm having problems with****

    <TextView
        android:layout_width="fill_parent"
        android:layout_height="0dp"
        android:id="@+id/count"
        android:layout_weight="1"
        android:textColor="@android:color/black"
        android:gravity="center"
        android:textSize="30sp" />

******************************************************

</LinearLayout>

<!-- 2 -->
<LinearLayout
    android:layout_width="0dp"
    android:layout_height="fill_parent"
    android:layout_weight="1"
    android:orientation="vertical"
    android:weightSum="9">

    <ImageView
        android:layout_width="fill_parent"
        android:layout_height="0dp"
        android:id="@+id/_21"
        app:srcCompat="@drawable/gray_block"
        android:layout_weight="1"
        android:onClick="press"
        android:tag="tile" />


    <ImageView
        android:layout_width="fill_parent"
        android:layout_height="0dp"
        android:id="@+id/_22"
        app:srcCompat="@drawable/gray_block"
        android:layout_weight="1"
        android:onClick="press"
        android:tag="tile"/>

    <ImageView
        android:layout_width="fill_parent"
        android:layout_height="0dp"
        android:id="@+id/_23"
        app:srcCompat="@drawable/gray_block"
        android:layout_weight="1"
        android:onClick="press"
        android:tag="tile"/>

    <ImageView
        android:layout_width="fill_parent"
        android:layout_height="0dp"
        android:id="@+id/_24"
        app:srcCompat="@drawable/gray_block"
        android:layout_weight="1"
        android:onClick="press"
        android:tag="tile"/>

    <ImageView
        android:layout_width="fill_parent"
        android:layout_height="0dp"
        android:id="@+id/_25"
        app:srcCompat="@drawable/gray_block"
        android:layout_weight="1"
        android:onClick="press"
        android:tag="tile"/>

    <ImageView
        android:layout_width="fill_parent"
        android:layout_height="0dp"
        android:id="@+id/_26"
        app:srcCompat="@drawable/gray_block"
        android:layout_weight="1"
        android:onClick="press"
        android:tag="tile"/>

    <ImageView
        android:layout_width="fill_parent"
        android:layout_height="0dp"
        android:id="@+id/_27"
        app:srcCompat="@drawable/gray_block"
        android:layout_weight="1"
        android:onClick="press"
        android:tag="tile"/>

    <ImageView
        android:layout_width="fill_parent"
        android:layout_height="0dp"
        android:id="@+id/_28"
        app:srcCompat="@drawable/gray_block"
        android:layout_weight="1"
        android:onClick="press"
        android:tag="tile"/>

</LinearLayout>

<!-- 3 -->
<LinearLayout
    android:layout_width="0dp"
    android:layout_height="fill_parent"
    android:layout_weight="1"
    android:orientation="vertical"
    android:weightSum="9">


    <ImageView
        android:layout_width="fill_parent"
        android:layout_height="0dp"
        android:id="@+id/_31"
        app:srcCompat="@drawable/newer_x"
        android:layout_weight="1"
        android:onClick="press"
        android:tag="bound" />

    <ImageView
        android:layout_width="fill_parent"
        android:layout_height="0dp"
        android:id="@+id/_32"
        app:srcCompat="@drawable/newer_x"
        android:layout_weight="1"
        android:onClick="press"
        android:tag="bound" />

    <ImageView
        android:layout_width="fill_parent"
        android:layout_height="0dp"
        android:id="@+id/_33"
        app:srcCompat="@drawable/newer_x"
        android:layout_weight="1"
        android:onClick="press"
        android:tag="bound" />

    <ImageView
        android:layout_width="fill_parent"
        android:layout_height="0dp"
        android:id="@+id/_34"
        app:srcCompat="@drawable/gray_block"
        android:layout_weight="1"
        android:onClick="press"
        android:tag="tile"/>

    <ImageView
        android:layout_width="fill_parent"
        android:layout_height="0dp"
        android:id="@+id/_35"
        app:srcCompat="@drawable/gray_block"
        android:layout_weight="1"
        android:onClick="press"
        android:tag="tile"/>

    <ImageView
        android:layout_width="fill_parent"
        android:layout_height="0dp"
        android:id="@+id/_36"
        app:srcCompat="@drawable/gray_block"
        android:layout_weight="1"
        android:onClick="press"
        android:tag="tile"/>

    <ImageView
        android:layout_width="fill_parent"
        android:layout_height="0dp"
        android:id="@+id/_37"
        app:srcCompat="@drawable/newer_x"
        android:layout_weight="1"
        android:onClick="press"
        android:tag="bound"/>

    <ImageView
        android:layout_width="fill_parent"
        android:layout_height="0dp"
        android:id="@+id/_38"
        app:srcCompat="@drawable/gray_block"
        android:layout_weight="1"
        android:onClick="press"
        android:tag="tile"/>

</LinearLayout>

<!-- 4 -->
<LinearLayout
    android:layout_width="0dp"
    android:layout_height="fill_parent"
    android:layout_weight="1"
    android:orientation="vertical"
    android:weightSum="9">


    <ImageView
        android:layout_width="fill_parent"
        android:layout_height="0dp"
        android:id="@+id/_41"
        app:srcCompat="@drawable/gray_block"
        android:layout_weight="1"
        android:onClick="press"
        android:tag="tile" />

    <ImageView
        android:layout_width="fill_parent"
        android:layout_height="0dp"
        android:id="@+id/_42"
        app:srcCompat="@drawable/gray_block"
        android:layout_weight="1"
        android:onClick="press"
        android:tag="tile"/>

    <ImageView
        android:layout_width="fill_parent"
        android:layout_height="0dp"
        android:id="@+id/_43"
        app:srcCompat="@drawable/gray_block"
        android:layout_weight="1"
        android:onClick="press"
        android:tag="tile"/>

    <ImageView
        android:layout_width="fill_parent"
        android:layout_height="0dp"
        android:id="@+id/_44"
        app:srcCompat="@drawable/gray_block"
        android:layout_weight="1"
        android:onClick="press"
        android:tag="tile"/>

    <ImageView
        android:layout_width="fill_parent"
        android:layout_height="0dp"
        android:id="@+id/_45"
        app:srcCompat="@drawable/gray_block"
        android:layout_weight="1"
        android:onClick="press"
        android:tag="tile"/>

    <ImageView
        android:layout_width="fill_parent"
        android:layout_height="0dp"
        android:id="@+id/_46"
        app:srcCompat="@drawable/newer_x"
        android:layout_weight="1"
        android:onClick="press"
        android:tag="bound"/>

    <ImageView
        android:layout_width="fill_parent"
        android:layout_height="0dp"
        android:id="@+id/_47"
        app:srcCompat="@drawable/gray_block"
        android:layout_weight="1"
        android:onClick="press"
        android:tag="tile"/>

    <ImageView
        android:layout_width="fill_parent"
        android:layout_height="0dp"
        android:id="@+id/_48"
        app:srcCompat="@drawable/gray_block"
        android:layout_weight="1"
        android:onClick="press"
        android:tag="tile"/>

</LinearLayout>

<!-- 5 -->
<LinearLayout
    android:layout_width="0dp"
    android:layout_height="fill_parent"
    android:layout_weight="1"
    android:orientation="vertical"
    android:weightSum="9">


    <ImageView
        android:layout_width="fill_parent"
        android:layout_height="0dp"
        android:id="@+id/_51"
        app:srcCompat="@drawable/new_goal"
        android:layout_weight="1"
        android:onClick="press"
        android:tag="goal" />

    <ImageView
        android:layout_width="fill_parent"
        android:layout_height="0dp"
        android:id="@+id/_52"
        app:srcCompat="@drawable/gray_block"
        android:layout_weight="1"
        android:onClick="press"
        android:tag="tile"/>

    <ImageView
        android:layout_width="fill_parent"
        android:layout_height="0dp"
        android:id="@+id/_53"
        app:srcCompat="@drawable/gray_block"
        android:layout_weight="1"
        android:onClick="press"
        android:tag="tile"/>

    <ImageView
        android:layout_width="fill_parent"
        android:layout_height="0dp"
        android:id="@+id/_54"
        app:srcCompat="@drawable/gray_block"
        android:layout_weight="1"
        android:onClick="press"
        android:tag="tile"/>

    <ImageView
        android:layout_width="fill_parent"
        android:layout_height="0dp"
        android:id="@+id/_55"
        app:srcCompat="@drawable/gray_block"
        android:layout_weight="1"
        android:onClick="press"
        android:tag="tile"/>

    <ImageView
        android:layout_width="fill_parent"
        android:layout_height="0dp"
        android:id="@+id/_56"
        app:srcCompat="@drawable/gray_block"
        android:layout_weight="1"
        android:onClick="press"
        android:tag="tile"/>

    <ImageView
        android:layout_width="fill_parent"
        android:layout_height="0dp"
        android:id="@+id/_57"
        app:srcCompat="@drawable/gray_block"
        android:layout_weight="1"
        android:onClick="press"
        android:tag="tile"/>

    <ImageView
        android:layout_width="fill_parent"
        android:layout_height="0dp"
        android:id="@+id/_58"
        app:srcCompat="@drawable/gray_block"
        android:layout_weight="1"
        android:onClick="press"
        android:tag="tile"/>

    <ImageView
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:id="@+id/undo"
        app:srcCompat="@drawable/newer_undo"
        android:layout_weight="1"
        android:onClick="undo"
        android:tag="undo"/>

</LinearLayout>

我很抱歉发布了这么多代码,但我不知道为什么会发生这种情况,我希望这个问题出现在这些文件中。

2 个答案:

答案 0 :(得分:0)

下线可能有问题

//TextView num;

首先取消注释。

然后不要再声明下面的行(已在onCreate中声明)

 num = (TextView) findViewById(R.id.count);

从onStart()中删除它。

答案 1 :(得分:0)

问题在于您的start(View view) onClick方法,您正在引用已在onCreate(Bundle obj)中实例化的TextView,请尝试将其删除。并将fill_parent替换为match_parent。