I'm fluent in java for some years now but never tried making apps. So 2 days ago I started and it was just a 20 minute work from getting android studio over setting it up in netbeans to show a hello world app on my phone. But now i'm stuck with this approach for swiping through an array of jpg's. The app gets build and started, but not displaying any of my pics. Please have a look.
Java: MainActivity.class
package vf.base;
import android.app.Activity;
import android.content.res.TypedArray;
import android.os.Bundle;
import android.view.MotionEvent;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.ViewFlipper;
public class MainActivity extends Activity {
private ViewFlipper viewFlipper;
private TypedArray pics;
private OnSwipeTouchListener onSwipeTouchListener;
/**
* Called when the activity is first created.
*/
@Override
protected void onCreate(Bundle savedInstanceState) {
pics = getResources().obtainTypedArray(R.array.pics);
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
viewFlipper = (ViewFlipper) findViewById(R.id.viewFlipper);
for (int i = 0; i < pics.length(); i++) {
AddViewFlipper(i);
}
onSwipeTouchListener = new OnSwipeTouchListener(this) {
@Override
public void onSwipeRight() {
viewFlipper.showNext();
}
}
@Override
public void onSwipeLeft() {
viewFlipper.showPrevious();
}
};
viewFlipper.setOnTouchListener(onSwipeTouchListener);
}
private void AddViewFlipper(int position) {
ImageView imageView = new ImageView(MainActivity.this);
imageView.setImageResource(position);
ViewGroup.LayoutParams imageParams = new ViewGroup.LayoutParams(
ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT);
imageView.setLayoutParams(imageParams);
viewFlipper.addView(imageView);
}
@Override
public boolean dispatchTouchEvent(MotionEvent ev) {
onSwipeTouchListener.gestureDetector.onTouchEvent(ev);
return super.dispatchTouchEvent(ev);
}
}
Alles anzeigen
Java: Listener
package vf.base;
import android.content.Context;
import android.view.GestureDetector;
import android.view.MotionEvent;
import android.view.View;
public class OnSwipeTouchListener implements View.OnTouchListener {
public GestureDetector gestureDetector;
public OnSwipeTouchListener(Context context) {
gestureDetector = new GestureDetector(context, new GestureListener());
}
public void onSwipeLeft() {
}
public void onSwipeRight() {
}
public boolean onTouch(View v, MotionEvent event) {
return gestureDetector.onTouchEvent(event);
}
private final class GestureListener extends GestureDetector.SimpleOnGestureListener {
private static final int SWIPE_DISTANCE_THRESHOLD = 100;
private static final int SWIPE_VELOCITY_THRESHOLD = 100;
@Override
public boolean onDown(MotionEvent e) {
return true;
}
@Override
public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX, float velocityY) {
float distanceX = e2.getX() - e1.getX();
float distanceY = e2.getY() - e1.getY();
if (Math.abs(distanceX) > Math.abs(distanceY) && Math.abs(distanceX) > SWIPE_DISTANCE_THRESHOLD
&& Math.abs(velocityX) > SWIPE_VELOCITY_THRESHOLD) {
if (distanceX > 0)
onSwipeRight();
else
onSwipeLeft();
return true;
}
return false;
}
}
}
Alles anzeigen
XML: array.xml
<?xml version="1.0" encoding="UTF-8"?>
<resources>
<integer-array name="pics">
<item>@drawable/tp_001</item>
<item>@drawable/tp_002</item>
<item>@drawable/tp_003</item>
</integer-array>
</resources>
XML: layout.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical">
<ViewFlipper
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:id="@+id/viewFlipper"
android:layout_alignParentTop="true"
android:layout_alignParentStart="true" />
</LinearLayout>
Alles anzeigen
Thank you!
ps ich kann auch etwas antworten in deutsch verstehen.