ImageView Mehrere Bitmaps anzeigen

  • Antworten:2
Alex M.
  • Forum-Beiträge: 30

01.01.2014, 12:39:54 via Website

Hallo,

ich will Bilder mit einer ImageView anzeigen lassen und bei Wischbewegungen soll das Bild gewechselt werden.

Hier erstmal mein Code:

1public class SortActivity extends Activity {
2
3 String name;
4 String path;
5
6 List<File> images = new ArrayList<File>();
7 ImageView imageview;
8 int position = 0;
9
10 private static final int SWIPE_MIN_DISTANCE = 120;
11 private static final int SWIPE_MAX_OFF_PATH = 250;
12 private static final int SWIPE_THRESHOLD_VELOCITY = 200;
13 private GestureDetector gestureDetector;
14 View.OnTouchListener gestureListener;
15 Bitmap bm;
16
17 private static int maxSize;
18
19 @SuppressWarnings("unused")
20 private boolean isImage(File file) {
21 BitmapFactory.Options options = new BitmapFactory.Options();
22 options.inJustDecodeBounds = true;
23 Bitmap bitmap = BitmapFactory.decodeFile(file.getAbsolutePath(), options);
24 return (options.outWidth != -1 && options.outHeight != -1);
25 }
26
27 @Override
28 protected void onCreate(Bundle savedInstanceState) {
29 super.onCreate(savedInstanceState);
30 setContentView(R.layout.activity_sort);
31
32 int[] max = new int[1];
33 GLES10.glGetIntegerv(GL10.GL_MAX_TEXTURE_SIZE, max, 0);
34
35 maxSize = max[0] / 2;
36
37 imageview = (ImageView) findViewById(R.id.imageView);
38
39 Bundle bundle = getIntent().getExtras();
40 name = bundle.getString("name");
41 path = bundle.getString("path");
42
43 gestureDetector = new GestureDetector(this, new MyGestureDetector());
44 gestureListener = new View.OnTouchListener() {
45
46 public boolean onTouch(View v, MotionEvent event) {
47 return gestureDetector.onTouchEvent(event);
48 }
49 };
50
51 setTitle("Source: " + name);
52
53 File dir = new File(path);
54
55 for(File file : dir.listFiles()) {
56 if(isImage(file)) {
57 images.add(file);
58 }
59 }
60
61 imageview.setImageBitmap(getBitmap(images.get(position)));
62 }
63
64 public static int calculateInSampleSize(BitmapFactory.Options options, int reqWidth, int reqHeight) {
65
66 final int height = options.outHeight;
67 final int width = options.outWidth;
68 int inSampleSize = 1;
69
70 if (height > reqHeight || width > reqWidth) {
71
72 final int halfHeight = height / 2;
73 final int halfWidth = width / 2;
74
75 while ((halfHeight / inSampleSize) > reqHeight
76 && (halfWidth / inSampleSize) > reqWidth) {
77 inSampleSize *= 2;
78 }
79 }
80
81 return inSampleSize;
82 }
83
84 public static Bitmap getBitmap(File file) {
85
86
87 // First decode with inJustDecodeBounds=true to check dimensions
88 final BitmapFactory.Options options = new BitmapFactory.Options();
89 options.inJustDecodeBounds = true;
90 BitmapFactory.decodeFile(file.getAbsolutePath(), options);
91
92 // Calculate inSampleSize
93 options.inSampleSize = calculateInSampleSize(options, maxSize, maxSize);
94
95 // Decode bitmap with inSampleSize set
96 options.inJustDecodeBounds = false;
97 return BitmapFactory.decodeFile(file.getAbsolutePath(), options);
98 }
99
100 @Override
101 public boolean onTouchEvent(MotionEvent event) {
102 return gestureDetector.onTouchEvent(event);
103 }
104
105 @Override
106 public boolean onCreateOptionsMenu(Menu menu) {
107 // Inflate the menu; this adds items to the action bar if it is present.
108 getMenuInflater().inflate(R.menu.sort, menu);
109 return true;
110 }
111
112 class MyGestureDetector extends GestureDetector.SimpleOnGestureListener {
113 @Override
114 public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX, float velocityY) {
115 try {
116 if (Math.abs(e1.getY() - e2.getY()) > SWIPE_MAX_OFF_PATH) {
117 return false;
118 }
119
120 if (e1.getX() - e2.getX() > SWIPE_MIN_DISTANCE && Math.abs(velocityX) > SWIPE_THRESHOLD_VELOCITY) {
121 if(position != images.size() - 1) {
122 imageview.setImageBitmap(getBitmap(images.get(position)));
123 }
124 } else if (e2.getX() - e1.getX() > SWIPE_MIN_DISTANCE && Math.abs(velocityX) > SWIPE_THRESHOLD_VELOCITY) {
125 if(position != 0) {
126 imageview.setImageBitmap(getBitmap(images.get(position)));
127 }
128 }
129 } catch (Exception e) {
130
131 }
132
133 return false;
134 }
135 }
136
137}

Beim Starten der Activity zeigt er das erste Bild im Ordner an, aber wenn ich wische, kommt kein anderes Bild.
Die Wischfunktion funktioniert, das weiß ich schon.

Mir ist dann aufgefallen, dass in Logcat folgendes steht:


Ich hab mich in Google eingelesen, dass das eine Meldung vom Garbage Collector ist.

Ich habe schon viele Möglichkeiten von StackOverflow, etc. ausprobiert mit recycle() und System.gc(), aber das
brachte keine Wirkung :(

Wenn es ein Control für meine Activity gibt, die das Wischen und Bilder wechseln automatisch kann und man nur
den Pfad zu den Bilder übergeben muss, könnt ihr es hier posten (am besten soll es schon in Android integriert sein).

Hoffe, jemand kann helfen.

Freue mich auf Antworten

MFG

Alex M.

— geändert am 01.01.2014, 13:22:48

Antworten
Alex M.
  • Forum-Beiträge: 30

02.01.2014, 13:23:03 via Website

Hallo,

ich habe mir den UIL mal angeschaut und jede bekomme ich mit folgenden Code garkein Bild angezeigt:

1public class SortActivity extends Activity {
2
3 ImageLoader imageLoader;
4
5 List<String> images = new ArrayList<String>();
6
7 String name;
8 String path;
9 ImageView imageView;
10
11 private static final int SWIPE_MIN_DISTANCE = 120;
12 private static final int SWIPE_MAX_OFF_PATH = 250;
13 private static final int SWIPE_THRESHOLD_VELOCITY = 200;
14 private GestureDetector gestureDetector;
15 View.OnTouchListener gestureListener;
16
17 DisplayImageOptions options = new DisplayImageOptions.Builder()
18 .resetViewBeforeLoading(true)
19 .cacheOnDisc(true)
20 .imageScaleType(ImageScaleType.EXACTLY)
21 .bitmapConfig(Bitmap.Config.RGB_565)
22 .considerExifParams(true)
23 .displayer(new FadeInBitmapDisplayer(300))
24 .build();
25
26 int position = 0;
27
28 @SuppressWarnings("unused")
29 private boolean isImage(File file) {
30 BitmapFactory.Options options = new BitmapFactory.Options();
31 options.inJustDecodeBounds = true;
32 Bitmap bitmap = BitmapFactory.decodeFile(file.getAbsolutePath(), options);
33 return (options.outWidth != -1 && options.outHeight != -1);
34 }
35
36 class MyGestureDetector extends GestureDetector.SimpleOnGestureListener {
37 @Override
38 public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX, float velocityY) {
39 try {
40 if (Math.abs(e1.getY() - e2.getY()) > SWIPE_MAX_OFF_PATH) {
41 return false;
42 }
43
44 if (e1.getX() - e2.getX() > SWIPE_MIN_DISTANCE && Math.abs(velocityX) > SWIPE_THRESHOLD_VELOCITY) {
45 if(position != images.size() - 1) {
46 position += 1;
47 imageLoader.displayImage(images.get(position), imageView, options);
48 System.out.println("...");
49 }
50 } else if (e2.getX() - e1.getX() > SWIPE_MIN_DISTANCE && Math.abs(velocityX) > SWIPE_THRESHOLD_VELOCITY) {
51 if(position != 0) {
52 position -= 1;
53 imageLoader.displayImage(images.get(position), imageView, options);
54 System.out.println("...");
55 }
56 }
57 } catch (Exception e) {
58
59 }
60
61 return false;
62 }
63 }
64
65 @Override
66 protected void onCreate(Bundle savedInstanceState) {
67 super.onCreate(savedInstanceState);
68 setContentView(R.layout.activity_sort);
69
70 ImageLoaderConfiguration config = new ImageLoaderConfiguration.Builder(this)
71 .memoryCacheSize(41943040)
72 .discCacheSize(104857600)
73 .threadPoolSize(10)
74 .build();
75
76 imageLoader = ImageLoader.getInstance();
77 imageLoader.init(config);
78
79 Bundle bundle = getIntent().getExtras();
80 name = bundle.getString("name");
81 path = bundle.getString("path");
82
83 gestureDetector = new GestureDetector(this, new MyGestureDetector());
84 gestureListener = new View.OnTouchListener() {
85
86 @Override
87 public boolean onTouch(View v, MotionEvent event) {
88 return gestureDetector.onTouchEvent(event);
89 }
90
91 };
92
93 setTitle("Source: " + name);
94
95 File dir = new File(path);
96
97 for(File file : dir.listFiles()) {
98 if(isImage(file)) {
99 images.add(file.getAbsolutePath());
100 }
101 }
102
103 imageLoader.displayImage(images.get(position), imageView, options);
104
105 }
106
107}

Ich habe nur das Standardbild von meiner ImageView.

Hoffe, jemand kann mir helfen.

Freue mich auf Antworten.

MFG

Alex M.

Antworten