- Forum-Beiträge: 29
04.03.2014, 21:47:46 via Website
04.03.2014 21:47:46 via Website
ich habe ein kleines Anfänger Problem meine App läuft immer ziemlich langsam, wenn ich einen Button drücke, der ein FrameLayout Visible setzt.
Warum wird die App dann so langsam?
Hier mal mein Code^^
2
3 private Button shop;
4 private Button back;
5 private TextView tv1;
6 private TextView tv2;
7 private TextView tv3;
8 private FrameLayout Frame;
9 private ImageView click;
10 private int level1 = 1;
11 private int level2 = 1;
12 private int level3 = 1;
13 private double count;
14 private double countps;
15 private double zaehler1;
16 private double zaehler2;
17 private double zaehler3;
18
19
20 @Override
21 protected void onCreate(Bundle savedInstanceState) {
22 super.onCreate(savedInstanceState);
23 setContentView(R.layout.activity_main);
24 click = (ImageView) findViewById(R.id.imageView1);
25 click.setOnClickListener(this);
26 Framesho = (FrameLayout) findViewById(R.id.Frame);
27 shop = (Button) findViewById(R.id.shop);
28 shop.setOnClickListener(this);
29 back = (Button) findViewById(R.id.back);
30 back.setOnClickListener(this);
31
32 tv1 = (TextView) findViewById(R.id.textView1);
33 tv2 = (TextView) findViewById(R.id.textView2);
34 tv3 = (TextView) findViewById(R.id.textView3);
35
36 LoadPreferences();
37 aktualisieren();
38 starteCount();
39 }
40
41
42
43 @Override
44 public void onClick(View v) {
45
46 if(v.getId() == R.id.tele) {
47
48 Frame.setVisibility(View.VISIBLE);
49
50 }
51
52 if(v.getId() == R.id.back) {
53
54
55 Frame.setVisibility(View.GONE);
56
57
58 }
59
60 if(v.getId() == R.id.imageView1) {
61
62 count = count + 1.0;
63 aktualisieren();
64
65 }
66
67 if(v.getId() == R.id.imageView2) {
68 if(count >= (int) Math.pow(1.2, level1)*2) {
69 count = count - (Math.pow(1.2, level1)*2);
70 zaehler1 = zaehler1 + Math.round(0.1*10)/10d;
71
72 level1 = level1 + 1;
73 aktualisieren();
74 }
75 }
76
77 if(v.getId() == R.id.imageView3) {
78 if(count >= Math.pow(1.2, level2)*10) {
79 count = count - Math.pow(1.2, level2)*10;
80 zaehler2 = zaehler2 + 0.5;
81 level2 = level2 + 1;
82 aktualisieren();
83 }
84 }
85
86 if(v.getId() == R.id.imageView4) {
87 if(count >= Math.pow(1.2, level3)*50) {
88 count = count - Math.pow(1.2, level3)*50;
89
90 zaehler3 = zaehler3 + 5.0;
91 level3 = level3 + 1;
92 aktualisieren();
93
94 }
95 }
96 round();
97 countps = zaehler1 + zaehler2 + zaehler3;
98 countps = Math.round(countps*10)/10d;
99 aktualisieren();
100 round();
101 }
102
103private void starteCount()
104{
105 new Thread(new Runnable() {
106 public void run(){
107
108 Count();
109
110
111 }
112}).start();
113
114}
115private void Count() {
116 Timer timer = new Timer();
117 TimerTask task = new TimerTask() {
118 public void run(){
119 MainActivity.this.runOnUiThread(new Runnable() {
120 public void run() {
121
122
123 count = count + countps;
124 round();
125 aktualisieren();
126
127
128
129 }
130 });
131 }
132 };
133
134 timer.schedule(task, 0, 1000);
135
136 }
137public void round(){
138 count = Math.round(count * 10)/10d;
139}
140
141
142 public void aktualisieren(){
143 TextView countps = (TextView) findViewById(R.id.countps);
144 countps.setText(Double.toString(countps) + " countps");
145 TextView Count = (TextView) findViewById(R.id.Count);
146 Count.setText(Double.toString(count) + " Count");
147 writeSavedPreferences();
148
149 tv1.setText(Integer.toString( (int) ( Math.pow(1.2, level1)*2)));
150 tv2.setText(Integer.toString( (int) ( Math.pow(1.2, level2)*10)));
151 tv3.setText(Integer.toString( (int) ( Math.pow(1.2, level3)*50)));
152
153 }
154
155 private void writeSavedPreferences() {
156 SharedPreferences settings = getSharedPreferences("GAME", 0);
157 SharedPreferences.Editor editor = settings.edit();
158
159 editor.putInt("Level1", level1);
160 editor.putInt("Level2", level2);
161 editor.putInt("Level3", level3);
162 editor.putString("countps", countps + "");
163 editor.putString("count", count + "");
164 editor.putString("zaehler1", zaehler1 + "");
165 editor.putString("zaehler2", zaehler2 + "");
166 editor.putString("zaehler3", zaehler3 + "");
167 editor.commit();
168
169
170 }
171
172 private void LoadPreferences() {
173 SharedPreferences pref = getSharedPreferences("GAME", 0);
174
175
176 level1 = pref.getInt("Level1", level1);
177 level2 = pref.getInt("Level2", level2);
178 level3 = pref.getInt("Level3", level3);
179 countps = Double.parseDouble(pref.getString("countps", countps + "")) ;
180 count = Double.parseDouble(pref.getString("count", count + ""));
181 zaehler1 = Double.parseDouble(pref.getString("zaehler1", zaehler1 + ""));
182 zaehler2 = Double.parseDouble(pref.getString("zaehler2", zaehler2 + ""));
183 zaehler3 = Double.parseDouble(pref.getString("zaehler3", zaehler3 + ""));
184 }
185 }
— geändert am 05.03.2014, 16:26:54