Habe es nochmal getestet, mich aber dann doch für das Google Training Tutorial entschieden, scheint auch i,wie abzuspeichern aber wenn ich sie laden will zeigt er mir keine Images mehr an.
Hier mal mein Code:
1package de.uulm.quartett;
2
3import android.app.ActivityOptions;
4import android.content.Intent;
5import android.content.res.Resources;
6import android.graphics.Bitmap;
7import android.graphics.BitmapFactory;
8import android.os.AsyncTask;
9import android.os.Bundle;
10import android.support.v4.app.FragmentActivity;
11import android.support.v4.util.LruCache;
12import android.support.v4.view.ViewPager;
13import android.util.Log;
14import android.view.View;
15import android.view.View.OnClickListener;
16import android.view.animation.Animation;
17import android.view.animation.AnimationUtils;
18import android.widget.ImageButton;
19import android.widget.ImageView;
20import android.widget.Toast;
21
22public class MainActivity extends FragmentActivity {
23 Animation animTranslate;
24 private GameSelectPagerAdapter mAdapter;
25 private ViewPager modes;
26 private LruCache<String, Bitmap> mMemoryCache;
27
28 ImageButton btn_startGame, btn_highScore, btn_decks, btn_profile, btn_info;
29
30 @Override
31 protected void onCreate(Bundle savedInstanceState) {
32 super.onCreate(savedInstanceState);
33 setContentView(R.layout.activity_main);
34
35 final int maxMemory = (int) (Runtime.getRuntime().maxMemory() / 1024);
36 Log.e("MAXMEMORY ", "" + maxMemory);
37
38 final int cacheSize = maxMemory / 8;
39
40 mMemoryCache = new LruCache<String, Bitmap>(cacheSize) {
41 @Override
42 protected int sizeOf(String key, Bitmap bitmap) {
43 // The cache size will be measured in kilobytes rather than
44 // number of items.
45 return bitmap.getByteCount() / 1024;
46 }
47 };
48
49 btn_startGame = (ImageButton) findViewById(R.id.imgBtnStartGame);
50 btn_highScore = (ImageButton) findViewById(R.id.imgBtnHighScore);
51 btn_decks = (ImageButton) findViewById(R.id.imgBtnDecks);
52 btn_profile = (ImageButton) findViewById(R.id.imgBtnProfile);
53 btn_info = (ImageButton) findViewById(R.id.imgBtn_info);
54
55 loadBitmap(R.drawable.slice_btn_new_game, btn_startGame);
56 loadBitmap(R.drawable.slice_btn_highscore, btn_highScore);
57 loadBitmap(R.drawable.slice_btn_decks, btn_decks);
58 loadBitmap(R.drawable.slice_btn_profil, btn_profile);
59
60 animTranslate = AnimationUtils.loadAnimation(this,
61 R.anim.anim_translate);
62 btn_startGame.setAnimation(animTranslate);
63 btn_startGame.setOnClickListener(new OnClickListener() {
64 @Override
65 public void onClick(View v) {
66 setContentView(R.layout.fragmentlayout);
67 mAdapter = new GameSelectPagerAdapter(
68 getSupportFragmentManager());
69 modes = (ViewPager) findViewById(R.id.pager);
70 modes.setAdapter(mAdapter);
71 }
72 });
73
74 btn_highScore.setAnimation(animTranslate);
75 btn_highScore.setOnClickListener(new OnClickListener() {
76 @Override
77 public void onClick(View v) {
78 // TODO Auto-generated method stub
79 Intent intent = new Intent(getApplicationContext(),
80 HighscoreActivity.class);
81 Bundle animation = ActivityOptions.makeCustomAnimation(
82 getApplicationContext(), R.anim.right_in,
83 R.anim.left_out).toBundle();
84 startActivity(intent, animation);
85 }
86 });
87
88 btn_decks.setAnimation(animTranslate);
89 btn_decks.setOnClickListener(new OnClickListener() {
90
91 @Override
92 public void onClick(View v) {
93 // TODO Auto-generated method stub
94 Intent intent = new Intent(getApplicationContext(),
95 DeckDetailActivity.class);
96 Bundle animation = ActivityOptions.makeCustomAnimation(
97 getApplicationContext(), R.anim.right_in,
98 R.anim.left_out).toBundle();
99 startActivity(intent, animation);
100 }
101 });
102
103 btn_profile.setAnimation(animTranslate);
104 btn_profile.setOnClickListener(new OnClickListener() {
105
106 @Override
107 public void onClick(View v) {
108 // TODO Auto-generated method stub
109 Intent intent = new Intent(getApplicationContext(),
110 ProfileActivity.class);
111 Bundle animation = ActivityOptions.makeCustomAnimation(
112 getApplicationContext(), R.anim.right_in,
113 R.anim.left_out).toBundle();
114 startActivity(intent, animation);
115 }
116 });
117
118 btn_info.setOnClickListener(new OnClickListener() {
119
120 @Override
121 public void onClick(View v) {
122 // TODO Auto-generated method stub
123 Toast.makeText(getApplicationContext(), "Infos folgen!",
124 Toast.LENGTH_LONG).show();
125 }
126 });
127 }
128
129 public void addBitmapToMemoryCache(String key, Bitmap bitmap) {
130 if (getBitmapFromMemCache(key) == null) {
131 mMemoryCache.put(key, bitmap);
132 }
133 }
134
135 public Bitmap getBitmapFromMemCache(String key) {
136 return mMemoryCache.get(key);
137 }
138
139 public void loadBitmap(int resId, ImageView imageView) {
140 Log.e("STRING RES ID", "" + String.valueOf(resId));
141 final String imageKey = String.valueOf(resId);
142
143 final Bitmap bitmap = getBitmapFromMemCache(imageKey);
144 if (bitmap != null) {
145 imageView.setImageBitmap(bitmap);
146 } else {
147 imageView.setImageResource(R.drawable.slice_info);
148 BitmapWorkerTask task = new BitmapWorkerTask();
149 task.execute(resId);
150 }
151 }
152
153 class BitmapWorkerTask extends AsyncTask<Integer, Void, Bitmap> {
154 @Override
155 protected Bitmap doInBackground(Integer... params) {
156 final Bitmap bitmap = BitmapFactory.decodeResource(getResources(),
157 params[0]);
158 addBitmapToMemoryCache(String.valueOf(params[0]), bitmap);
159 return bitmap;
160 }
161 }
162}
Vllt. fällt ja jemanden etwas auf wäre dankbar bin glaub vor lauter rumprobieren schon voll neben der Spur.
Empfohlener redaktioneller Inhalt
Mit Deiner Zustimmung wird hier ein externer Inhalt geladen.
Mit Klick auf den oben stehenden Button erklärst Du Dich damit einverstanden, dass Dir externe Inhalte angezeigt werden dürfen. Dabei können personenbezogene Daten an Drittanbieter übermittelt werden. Mehr Infos dazu findest Du in unserer Datenschutzerklärung.