拡張子:txtvbswsfjsphpjavahtml utf8sjispackage sample.lightbox.viewswitch; import android.app.Activity; import android.os.Bundle; import android.util.Log; import android.view.GestureDetector; import android.view.LayoutInflater; import android.view.MotionEvent; import android.view.View; import android.view.animation.AnimationUtils; import android.widget.LinearLayout; import android.widget.RelativeLayout; import android.widget.ViewSwitcher; public class MainActivity extends Activity { private ViewSwitcher vs = null; private GestureDetector mDetector; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); // 画面インスタンス作成用のオブジェクトをシステムから取得 LayoutInflater inflater = (LayoutInflater) getSystemService(LAYOUT_INFLATER_SERVICE); // メイン画面定義のインスタンスを作成して表示する RelativeLayout root = (RelativeLayout) inflater.inflate(R.layout.activity_main, null); setContentView(root); // 戻るボタンの処理 findViewById(R.id.button_p).setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { vs.showPrevious(); } }); // 進むボタンの処理 findViewById(R.id.button_n).setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { vs.showNext(); } }); // レイアウト用の XML より View のインスタンスを作成 // ( inflate( id, null ) では、ルートのクラスのインスタンスとなる ) LinearLayout image1 = (LinearLayout) inflater.inflate(R.layout.image_1, null); LinearLayout image2 = (LinearLayout) inflater.inflate(R.layout.image_2, null); // ViewSwitcher に 作成した View を追加する vs = (ViewSwitcher) root.findViewById(R.id.viewSwitcher); vs.addView(image1); vs.addView(image2); // アニメーションの設定 vs.setInAnimation(AnimationUtils.loadAnimation(MainActivity.this, android.R.anim.fade_in)); vs.setOutAnimation(AnimationUtils.loadAnimation(MainActivity.this, android.R.anim.fade_out)); mDetector = new GestureDetector(MainActivity.this, new MyGestureListener()); } @Override public boolean onTouchEvent(MotionEvent event) { // この処理で、タッチイベントからジェスチャーが処理できるようになる mDetector.onTouchEvent(event); return super.onTouchEvent(event); } // フリックによる画面切り替えの為の定義 class MyGestureListener extends GestureDetector.SimpleOnGestureListener { private static final String DEBUG_TAG = "Gestures"; @Override public boolean onDown(MotionEvent event) { Log.i(DEBUG_TAG, "onDown: " + event.toString()); return true; } @Override public boolean onFling(MotionEvent event1, MotionEvent event2, float velocityX, float velocityY) { vs.showNext(); // 左から右で 正、右から左で 負 Log.i(DEBUG_TAG, (event2.getX() - event1.getX() )+"" ); return true; } } }
package sample.lightbox.viewswitch; import android.app.Activity; import android.os.Bundle; import android.util.Log; import android.view.GestureDetector; import android.view.LayoutInflater; import android.view.MotionEvent; import android.view.View; import android.view.animation.AnimationUtils; import android.widget.LinearLayout; import android.widget.RelativeLayout; import android.widget.ViewSwitcher; public class MainActivity extends Activity { private ViewSwitcher vs = null; private GestureDetector mDetector; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); // 画面インスタンス作成用のオブジェクトをシステムから取得 LayoutInflater inflater = (LayoutInflater) getSystemService(LAYOUT_INFLATER_SERVICE); // メイン画面定義のインスタンスを作成して表示する RelativeLayout root = (RelativeLayout) inflater.inflate(R.layout.activity_main, null); setContentView(root); // 戻るボタンの処理 findViewById(R.id.button_p).setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { vs.showPrevious(); } }); // 進むボタンの処理 findViewById(R.id.button_n).setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { vs.showNext(); } }); // レイアウト用の XML より View のインスタンスを作成 // ( inflate( id, null ) では、ルートのクラスのインスタンスとなる ) LinearLayout image1 = (LinearLayout) inflater.inflate(R.layout.image_1, null); LinearLayout image2 = (LinearLayout) inflater.inflate(R.layout.image_2, null); // ViewSwitcher に 作成した View を追加する vs = (ViewSwitcher) root.findViewById(R.id.viewSwitcher); vs.addView(image1); vs.addView(image2); // アニメーションの設定 vs.setInAnimation(AnimationUtils.loadAnimation(MainActivity.this, android.R.anim.fade_in)); vs.setOutAnimation(AnimationUtils.loadAnimation(MainActivity.this, android.R.anim.fade_out)); mDetector = new GestureDetector(MainActivity.this, new MyGestureListener()); } @Override public boolean onTouchEvent(MotionEvent event) { // この処理で、タッチイベントからジェスチャーが処理できるようになる mDetector.onTouchEvent(event); return super.onTouchEvent(event); } // フリックによる画面切り替えの為の定義 class MyGestureListener extends GestureDetector.SimpleOnGestureListener { private static final String DEBUG_TAG = "Gestures"; @Override public boolean onDown(MotionEvent event) { Log.i(DEBUG_TAG, "onDown: " + event.toString()); return true; } @Override public boolean onFling(MotionEvent event1, MotionEvent event2, float velocityX, float velocityY) { vs.showNext(); // 左から右で 正、右から左で 負 Log.i(DEBUG_TAG, (event2.getX() - event1.getX() )+"" ); return true; } } }
拡張子:txtvbswsfjsphpjavahtml utf8sjis<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:paddingLeft="@dimen/activity_horizontal_margin" android:paddingRight="@dimen/activity_horizontal_margin" android:paddingTop="@dimen/activity_vertical_margin" android:paddingBottom="@dimen/activity_vertical_margin" tools:context=".MainActivity"> <LinearLayout android:orientation="horizontal" android:layout_width="match_parent" android:layout_height="wrap_content" android:id="@+id/buttons"> <Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="戻る" android:id="@+id/button_p" android:layout_weight="1"/> <Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="進む" android:id="@+id/button_n" android:layout_weight="1"/> </LinearLayout> <ViewSwitcher android:layout_width="match_parent" android:layout_height="match_parent" android:id="@+id/viewSwitcher" android:layout_below="@+id/buttons"/> </RelativeLayout> image_1.xml 拡張子:txtvbswsfjsphpjavahtml utf8sjis<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="match_parent" android:layout_height="match_parent"> <ImageView android:layout_width="match_parent" android:layout_height="wrap_content" android:id="@+id/imageView" android:src="@drawable/kirin"/> </LinearLayout> image_2.xml 拡張子:txtvbswsfjsphpjavahtml utf8sjis<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="match_parent" android:layout_height="match_parent"> <ImageView android:layout_width="match_parent" android:layout_height="wrap_content" android:id="@+id/imageView" android:src="@drawable/niwatori"/> </LinearLayout>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:paddingLeft="@dimen/activity_horizontal_margin" android:paddingRight="@dimen/activity_horizontal_margin" android:paddingTop="@dimen/activity_vertical_margin" android:paddingBottom="@dimen/activity_vertical_margin" tools:context=".MainActivity"> <LinearLayout android:orientation="horizontal" android:layout_width="match_parent" android:layout_height="wrap_content" android:id="@+id/buttons"> <Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="戻る" android:id="@+id/button_p" android:layout_weight="1"/> <Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="進む" android:id="@+id/button_n" android:layout_weight="1"/> </LinearLayout> <ViewSwitcher android:layout_width="match_parent" android:layout_height="match_parent" android:id="@+id/viewSwitcher" android:layout_below="@+id/buttons"/> </RelativeLayout>
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="match_parent" android:layout_height="match_parent"> <ImageView android:layout_width="match_parent" android:layout_height="wrap_content" android:id="@+id/imageView" android:src="@drawable/kirin"/> </LinearLayout>
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="match_parent" android:layout_height="match_parent"> <ImageView android:layout_width="match_parent" android:layout_height="wrap_content" android:id="@+id/imageView" android:src="@drawable/niwatori"/> </LinearLayout>