Notice
Recent Posts
Recent Comments
Link
관리 메뉴

설.현.아빠

HorizontalScrollView & Gesture를 이용한 이미지 좌우 슬라이딩 효과. 본문

안드로이드/Image

HorizontalScrollView & Gesture를 이용한 이미지 좌우 슬라이딩 효과.

설.현.아빠 2011. 2. 11. 10:34




끙...너무 어려운....Gesture를 모르니 이해를 몬하겠네..

 

 

<?xml version="1.0" encoding="utf-8"?>


<!-- AndroidManifest.xml -->


<manifest xmlns:android="http://schemas.android.com/apk/res/android"
      package="lee.android.Sliding"
      android:versionCode="1"
      android:versionName="1.0">
    <application android:icon="@drawable/icon" android:label="@string/app_name">
        <activity android:name=".Sliding"
                  android:label="@string/app_name">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
        <activity android:name=".MenuSlideView"
                  android:label="@string/app_name">
        </activity>
    </application>
    <uses-sdk android:minSdkVersion="8" />

</manifest>

 

 

<?xml version="1.0" encoding="utf-8"?>


<!-- main.xml -->

<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    >
 <Button
 android:id="@+id/menu_btn"
 android:layout_width="fill_parent"
 android:layout_height="wrap_content"
 android:text="Slide Button"
 android:layout_gravity="center_horizontal" />
 <lee.android.Sliding.MenuSlideView
  android:id="@+id/menu_slide"
  android:layout_width="fill_parent"
  android:layout_height="wrap_content"
  android:layout_gravity="bottom" />
</FrameLayout>

 

<?xml version="1.0" encoding="utf-8"?>

 

<!-- manu_layout.xml -->


<LinearLayout
  xmlns:android="http://schemas.android.com/apk/res/android"
  android:orientation="horizontal"
  android:layout_width="wrap_content"
  android:layout_height="wrap_content">
  
  <ImageView
  android:id="@+id/default_gesture"
  android:layout_width="320dip"
  android:layout_height="fill_parent"
  android:src="@drawable/icon"
  android:background="#C0E0FF"/>
  
  <ImageView
  android:id="@+id/custom_gesture"
  android:layout_width="320dip"
  android:layout_height="fill_parent"
  android:src="@drawable/icon"
  android:background="#F0C0FF"/>  
  
</LinearLayout>

 

// Sliding.java

 

package lee.android.Sliding;

import android.app.Activity;
import android.os.Bundle;

public class Sliding extends Activity {
    private MenuSlideView mSlideView;
    
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        mSlideView = (MenuSlideView)findViewById(R.id.menu_slide);
    }
}

 

 

// MenuSlideView .java

 

package lee.android.Sliding;

import android.content.Context;
import android.util.AttributeSet;
import android.util.Log;
import android.view.Display;
import android.view.GestureDetector;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;
import android.view.WindowManager;
import android.view.GestureDetector.OnGestureListener;
import android.widget.HorizontalScrollView;


public class MenuSlideView extends HorizontalScrollView {
 private Context mContext;
 private int mScreenWidth;
 private GestureDetector mGesture;
 private GestureDetector.OnGestureListener mGesturesListener = new OnGestureListener() {
  @Override
  public boolean onDown (MotionEvent e) {
   Log.d("dodo4989","onDown");
   return false;
  }
  @Override
  public boolean onFling (MotionEvent e1, MotionEvent e2, float velocityX, float velocityY) {
   Log.d("dodo4989","onFling");
   if(velocityX>0){
    smoothScrollTo(0,0);
   }else {
    smoothScrollTo(mScreenWidth,0);
   }
   return false;
  }
  @Override
  public void onLongPress (MotionEvent e) {
   Log.d("dodo4989","onLongPress");
   
  }
  @Override
  public boolean onScroll (MotionEvent e1, MotionEvent e2, float distanceX, float distanceY) {
   Log.d("dodo4989","onScroll");
   smoothScrollBy((int)distanceX,0);
   return false;
  }
  @Override
  public void onShowPress (MotionEvent e) {
   Log.d("dodo4989","onShowPress");
   
  }
  @Override
  public boolean onSingleTapUp (MotionEvent e) {
   Log.d("dodo4989","onSingleTapUp");
   return false;
  }
 };
 private void createSubView() {
  mGesture = new GestureDetector(mContext, mGesturesListener);
  
  //scroll view setting
  setHorizontalScrollBarEnabled(false);
  
  //get screen size
  Display display = ((WindowManager)mContext.getSystemService(Context.WINDOW_SERVICE)).getDefaultDisplay();
  mScreenWidth = display.getWidth();
  
  //set sub layout
  LayoutInflater inflater = (LayoutInflater)mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
  View v = inflater.inflate(R.layout.menu_layout, null);
  ViewGroup.LayoutParams params = new ViewGroup.LayoutParams(LayoutParams.MATCH_PARENT,100);
  addView(v, params);  
 }
 
 public MenuSlideView(Context context) {
  super(context);
  mContext = context;
  createSubView();
 }
 public MenuSlideView(Context context, AttributeSet attrs) {
  super(context, attrs);
  mContext = context;
  createSubView();
 }
 public MenuSlideView(Context context, AttributeSet attrs, int defStyle) {
  super(context, attrs, defStyle);
  mContext = context;
  createSubView();
 }
 @Override
 public boolean onTouchEvent(MotionEvent ev) {
  mGesture.onTouchEvent(ev);
  int action = ev.getAction();
  switch(action) {
  case MotionEvent.ACTION_UP:
   if(getScrollX() < mScreenWidth/2) {
    smoothScrollTo(0,0);
   } else {
    smoothScrollTo(mScreenWidth,0);
   }
   break;
  }
  return true;
 } 
}


Comments