2012-04-06 10 views
4

나는 내 액티비티 중 하나에서 안드로이드로 어플리케이션을 만들려고합니다. 전체 화면 이미지를 보여주고 그림을 손가락으로 움직여 좌우로 슬라이드시키고 싶습니다.전체 화면 이미지 갤러리 안드로이드 안드로이드 ImageSwitcher

기본 갤러리보기 및 이미지 전환기를 사용해 봤지만 맞춤형 Android 갤러리에서와 같이 축소판없이 슬라이드 이벤트를 처리 할 수 ​​없습니다.

내 간단한 이미지 스위처 xml 및 활동 클래스는 다음과 같습니다. 아무도 내 방식을 보여 주거나 아래 코드를 편집하면 매우 감사하겠습니다. 사전에 감사합니다 ...

레이아웃 XML :

<?xml version="1.0" encoding="utf-8"?> 
    <ImageSwitcher 
    xmlns:android="http://schemas.android.com/apk/res/android" 
    android:id="@+id/imageSwitcher" 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent" 
    android:layout_alignParentBottom="true" 
    android:layout_alignParentLeft="true" 
    android:layout_alignParentTop="true" 
    android:onClick="imageClick" 
    android:src="@drawable/ic_launcher" 
    android:keepScreenOn="true"> 
    </ImageSwitcher> 

코드 :

public class GalleryActivity extends Activity implements ViewFactory { 

    private ImageSwitcher imageSwitcher ; 


    @Override 
    public void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activities); 

     imageSwitcher = (ImageSwitcher) findViewById(R.id.imageSwitcher); 
     imageSwitcher.setFactory(this); 
     imageSwitcher.setInAnimation(AnimationUtils.loadAnimation(this, android.R.anim.fade_in)); 
     imageSwitcher.setOutAnimation(AnimationUtils.loadAnimation(this, android.R.anim.fade_out)); 

     imageSwitcher.setImageResource(R.drawable.menu); 

     imageSwitcher.setOnTouchListener(new OnTouchListener() { 

      public boolean onTouch(View v, MotionEvent event) { 
       findViewById(R.drawable.menu); 
       imageSwitcher.addView((ImageSwitcher) findViewById(R.drawable.etkinlik)); 
       imageSwitcher.showNext(); 

       return false; 
      } 
     }); 
    } 
     public View makeView() { 
     ImageView iView = new ImageView(this); 
     iView.setScaleType(ImageView.ScaleType.FIT_CENTER); 
     iView.setLayoutParams(new 
        ImageSwitcher.LayoutParams(
           LayoutParams.FILL_PARENT,LayoutParams.FILL_PARENT)); 
     iView.setBackgroundColor(0xFF000000); 
     return iView; 
    } 
} 

@imran 칸의 솔루션을 조금 편집 한 후, 여기에 아주 잘 작동 코드가 .

해결책 :

ImageSwitcher imageSwitcher ; 

Integer[] imageList = { 
     R.drawable.gallery, 
     R.drawable.menu, 
     R.drawable.promotion, 
     R.drawable.info, 
     R.drawable.activities  
}; 

int curIndex=0; 
int downX,upX; 

@Override 
public void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activities); 

    imageSwitcher = (ImageSwitcher) findViewById(R.id.imageSwitcher); 
    imageSwitcher.setFactory(this); 
    imageSwitcher.setInAnimation(AnimationUtils.loadAnimation(this,android.R.anim.fade_in)); 
    imageSwitcher.setOutAnimation(AnimationUtils.loadAnimation(this,android.R.anim.fade_out)); 

    imageSwitcher.setImageResource(imageList[curIndex]); 
    imageSwitcher.setOnTouchListener(new OnTouchListener() { 
    @Override 
    public boolean onTouch(View v, MotionEvent event) { 

     if (event.getAction() == MotionEvent.ACTION_DOWN) { 
      downX = (int) event.getX(); 
      Log.i("event.getX()", " downX " + downX); 
      return true; 
     } 

     else if (event.getAction() == MotionEvent.ACTION_UP) { 
      upX = (int) event.getX(); 
      Log.i("event.getX()", " upX " + downX); 
      if (upX - downX > 100) { 

       //curIndex current image index in array viewed by user 
       curIndex--; 
       if (curIndex < 0) { 
        curIndex = imageList.length-1; 
       } 

       imageSwitcher.setInAnimation(AnimationUtils.loadAnimation(GalleryActivity.this,R.anim.slide_in_left)); 
       imageSwitcher.setOutAnimation(AnimationUtils.loadAnimation(GalleryActivity.this,R.anim.slide_out_right)); 

       imageSwitcher.setImageResource(imageList[curIndex]); 
       //GalleryActivity.this.setTitle(curIndex); 
      } 

      else if (downX - upX > -100) { 

       curIndex++; 
       if (curIndex > 4) { 
        curIndex = 0; 
       } 

       imageSwitcher.setInAnimation(AnimationUtils.loadAnimation(GalleryActivity.this,R.anim.slide_in_right)); 
       imageSwitcher.setOutAnimation(AnimationUtils.loadAnimation(GalleryActivity.this,R.anim.slide_out_left)); 

       imageSwitcher.setImageResource(imageList[curIndex]); 
       //GalleryActivity.this.setTitle(curIndex); 
      } 
       return true; 
      } 
      return false; 
     } 
    }); 
} 
@Override 
public View makeView() { 
    ImageView i = new ImageView(this); 
    i.setScaleType(ImageView.ScaleType.FIT_CENTER); 
    return i; 
} 
+0

이 코드를 실행하면 어떻게됩니까? –

+0

난 그냥 전체 화면에서 이미지를보고 touch.I에 대한 해결책을 게시하지만 난 다른 문제가 생겼어, 당신이 원한다면 볼 수 있습니다. – osayilgan

+0

ok osayilgan이 내 대답을보고 ImageSwitcher onTouch.i에서 시도해보십시오. 도움이 되었으면 좋겠습니다. –

답변

2

이 방법으로 시도 :

imageSwitcher1 = (ImageSwitcher) findViewById(R.id.imageSwitcher1); 
imageSwitcher1.setFactory(this); 
imageSwitcher1.setInAnimation(AnimationUtils.loadAnimation(this, 
android.R.anim.fade_in)); 
imageSwitcher1.setOutAnimation(AnimationUtils.loadAnimation(this, 
android.R.anim.fade_out)); 
imageSwitcher1.setImageResource(R.drawable.girl2); 
imageSwitcher1.setOnTouchListener(new OnTouchListener() { 
     @Override 
     public boolean onTouch(View v, MotionEvent event) { 
      if (event.getAction() == MotionEvent.ACTION_DOWN) { 
       downX = (int) event.getX(); 
       return true; 
      } else if (event.getAction() == MotionEvent.ACTION_UP) { 
       upX = (int) event.getX(); 
       if (upX - downX > 100) { 
       imageSwitcher1.setInAnimation(AnimationUtils.loadAnimation(ShowPhotoActivity.this, 
       android.R.anim.slide_in_left)); 
       mageSwitcher1.setOutAnimation(AnimationUtils.loadAnimation(ShowPhotoActivity.this, 
       android.R.anim.slide_out_right)); 
       imageSwitcher1.setImageResource(R.drawable.girl1); 
       } else if (downX - upX > 100)//    { 
       imageSwitcher1.setInAnimation(AnimationUtils.loadAnimation(ShowPhotoActivity.this, 
       android.R.anim.slide_in_left)); 
       imageSwitcher1.setOutAnimation(AnimationUtils.loadAnimation(ShowPhotoActivity.this, 
       android.R.anim.slide_out_right)); 
       imageSwitcher1.setImageResource(R.drawable.girl2); 
       } 
       return true; 
      } 
      return false; 
     } 
    }; 

및 u는 이미지 배열이있는 경우 다음이 시도 :

imgSwitcher.setOnTouchListener(new OnTouchListener() { 
      @Override 
      public boolean onTouch(View v, MotionEvent event) { 
       // TODO Auto-generated method stub 
       if (event.getAction() == MotionEvent.ACTION_DOWN) { 
        downX = (int) event.getX(); 
        Log.i("event.getX()", " downX " + downX); 
        return true; 
       } else if (event.getAction() == MotionEvent.ACTION_UP) { 
        upX = (int) event.getX(); 
        Log.i("event.getX()", " upX " + downX); 
        if (upX - downX > 100) { 
         imgSwitcher.setInAnimation(AnimationUtils 
         .loadAnimation(firstActivity.this, 
         android.R.anim.slide_in_left)); 
         imgSwitcher.setOutAnimation(AnimationUtils 
         .loadAnimation(firstActivity.this, 
         android.R.anim.slide_out_right)); 
         //curIndex current image index in array viewed by user 
         curIndex--; 
         if (curIndex < 0) { 
          curIndex = 5; 
         } 
         //IMAGE_LIST :-image list array 
         imgSwitcher.setImageResource(IMAGE_LIST[curIndex]); 
         firstActivity.this.switchTitle(curIndex); 
        } else if (downX - upX > 100) { 
         imgSwitcher.setInAnimation(AnimationUtils 
         .loadAnimation(firstActivity.this, 
         R.anim.slide_out_left)); 
         imgSwitcher.setOutAnimation(AnimationUtils 
         .loadAnimation(firstActivity.this, 
         R.anim.slide_in_right)); 
         curIndex++; 
         if (curIndex > 5) { 
          curIndex = 0; 
         } 
         imgSwitcher.setImageResource(IMAGE_LIST[curIndex]); 
         firstActivity.this.switchTitle(curIndex); 
        } 
        return true; 
       } 
       return false; 
      } 
     }); 
+0

imran, 작동하지 않았습니다. – osayilgan

+0

검은 화면이 나타나고 터치시 충돌이 발생합니다. – osayilgan

+0

이제 작동합니다. 내 편집 내용을 해결책으로 확인할 수 있습니다. 다시 한번 imran khan에게 감사드립니다. – osayilgan

1

은 내가 compatability library를 사용하는 것이 좋습니다 다음 view pager을 사용합니다. 그런 식으로 안드로이드가 당신을 위해 모든 무거운 짐을 다하고있을 것이고 당신이해야 할 일은 ViewPager에 당신이 가지고있는 이미지의 수를 알려주는 것입니다.

다음은 google의 예입니다. 조금 변경되었습니다.

public class FragmentPagerSupport extends FragmentActivity { 
static final int NUM_ITEMS = 10; 

MyAdapter mAdapter; 

ViewPager mPager; 

@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.fragment_pager); 

    mAdapter = new MyAdapter(getSupportFragmentManager()); 

    mPager = (ViewPager)findViewById(R.id.pager); 
    mPager.setAdapter(mAdapter); 

    // Watch for button clicks. 
    Button button = (Button)findViewById(R.id.goto_first); 
    button.setOnClickListener(new OnClickListener() { 
     public void onClick(View v) { 
      mPager.setCurrentItem(0); 
     } 
    }); 
    button = (Button)findViewById(R.id.goto_last); 
    button.setOnClickListener(new OnClickListener() { 
     public void onClick(View v) { 
      mPager.setCurrentItem(NUM_ITEMS-1); 
     } 
    }); 
} 

public static class MyAdapter extends FragmentPagerAdapter { 
    public MyAdapter(FragmentManager fm) { 
     super(fm); 
    } 

    @Override 
    public int getCount() { 
     return NUM_ITEMS; 
    } 

    @Override 
    public Fragment getItem(int position) { 
     return ArrayListFragment.newInstance(position); 
    } 
} 

public static class ArrayListFragment extends ListFragment { 
    int mNum; 

    /** 
    * Create a new instance of CountingFragment, providing "num" 
    * as an argument. 
    */ 
    static ArrayListFragment newInstance(int num) { 
     ArrayListFragment f = new ArrayListFragment(); 

     // Supply num input as an argument. 
     Bundle args = new Bundle(); 
     args.putInt("num", num); 
     f.setArguments(args); 

     return f; 
    } 

    /** 
    * When creating, retrieve this instance's number from its arguments. 
    */ 
    @Override 
    public void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     mNum = getArguments() != null ? getArguments().getInt("num") : 1; 
    } 

    /** 

    */ 
    @Override 
    public View onCreateView(LayoutInflater inflater, ViewGroup container, 
      Bundle savedInstanceState) { 
     View v = inflater.inflate(R.layout.fragment_pager_list, container, false); 
     View tv = v.findViewById(R.id.text); 
     ((ImageView)tv).setImageDrawable(#INSERT YOUR IMAGEHERE) //you can pass the id of the drawable into the mNum. Or you could make it a String instead of int and pass the url. 
     return v; 
    } 

    @Override 
    public void onActivityCreated(Bundle savedInstanceState) { 
     super.onActivityCreated(savedInstanceState); 
     setListAdapter(new ArrayAdapter<String>(getActivity(), 
       android.R.layout.simple_list_item_1, Cheeses.sCheeseStrings)); 
    } 

    @Override 
    public void onListItemClick(ListView l, View v, int position, long id) { 
     Log.i("FragmentList", "Item clicked: " + id); 
    } 
} 
} 

편집 : 포맷이 떨어져 비트인지 죄송합니다 ...

+1

조언을 주셔서 감사합니다. 나는 그것을위한 길을 찾은 것 같습니다. 그러나 그것이 얼마나 적절한 지 나는 모른다. 이 방식으로 수행하는 작업은 기본적으로 갤러리보기 영역의 크기를 사용자 정의 갤러리보기에서 변경하는 것입니다. 그런 식으로 터치 이벤트를 듣는 대신 오른쪽 및 왼쪽 슬라이드를 포함한 사용자 지정 갤러리보기 기능을 사용할 수 있습니다. 여기에 내 코드가있다. – osayilgan

관련 문제