Android 프로 그래 밍 슬라이딩 효과 갤러리+GridView 이미지 미리 보기 기능 구현(데모 원본 다운로드 첨부)

이 사례 는 안 드 로 이 드 프로 그래 밍 슬라이딩 효과 의 Gallery+GridView 가 이미지 미리 보기 기능 을 실현 하 는 것 을 보 여 준다.여러분 께 참고 하도록 공유 하 겠 습 니 다.구체 적 으로 는 다음 과 같 습 니 다.
Android 시스템 은 하나의 GridView 와 Gallery 두 개의 컨트롤 을 가지 고 있 습 니 다.GridView 격자 에 따 르 면 Gallery 는 하나의 탐색 을 하고 둘 을 결합 하면 Gallery 가 그림 을 탐색 하 는 효 과 를 진정 으로 실현 할 수 있 습 니 다.

이 예제 에 서 는 GridView 와 Gallery 두 개의 컨트롤 을 통 해 Gallery 이미지 세트 를 모방 하여 완전한 그림 탐색 효 과 를 실현 합 니 다.효과 도 는 다음 과 같다.

1、GridView
우선,GridView 컨트롤 을 채 울 그림 어댑터 를 사용자 정의 합 니 다.

public class GridImageAdapter extends BaseAdapter {
  private Context mContext;
  Drawable btnDrawable;
  public GridImageAdapter(Context context) {
    mContext = context;
    Resources resources = context.getResources();
    btnDrawable = resources.getDrawable(R.drawable.bg);
  }
  @Override
  public int getCount() {
    return ImageSource.mThumbIds.length;
  }
  @Override
  public Object getItem(int position) {
    return position;
  }
  @Override
  public long getItemId(int position) {
    return position;
  }
  @Override
  public View getView(int position, View convertView, ViewGroup parent) {
    ImageViewExt imageView;
    int space;
    if (convertView == null) {
      imageView = new ImageViewExt(mContext);
      if (imageCol == 5) {
        space = dm.heightPixels / imageCol - 6;
        imageView.setLayoutParams(new GridView.LayoutParams(space, space));
      } else {
        space = dm.widthPixels / imageCol - 6;
        imageView.setLayoutParams(new GridView.LayoutParams( space, space));
      }
      imageView.setAdjustViewBounds(true);
      imageView.setScaleType(ImageView.ScaleType.CENTER_CROP);  //            
       imageView.setPadding(3, 3, 3, 3);
    } else {
      imageView = (ImageViewExt) convertView;
    }
    imageView.setImageResource(ImageSource.mThumbIds[position]);
    return imageView;
  }
}

그리고 GridImageAdapter 로 GridView 를 채 웁 니 다.

gridView = (GridView) findViewById(R.id.myGrid);
gridImageAdapter = new GridImageAdapter(this);
gridView.setAdapter(gridImageAdapter);
gridView.setOnItemClickListener(listener); //         

마지막 으로 GridView 컨트롤 의 클릭 감청 이벤트 설정

AdapterView.OnItemClickListener listener = new AdapterView.OnItemClickListener() {
  @Override
  public void onItemClick(AdapterView<?> arg0, View arg1, int position, long id) {
    Intent intent = new Intent();
    intent.setClass(GridViewActivity.this, GalleryActivity.class);
    intent.putExtra("position", position);
    startActivity(intent);
  }
};

2、Gallery
GridView 의 그림 표시,감청 이 벤트 를 완료 한 후,현재 그림 을 클릭 하면 현재 클릭 한 그림 을 표시 하 는 Activity 를 시작 합 니 다.이때 그림 을 표시 하 는 컨트롤 은 Gallery 입 니 다.
우선,GridView 와 마찬가지 로 Gallery 를 채 울 이미지 어댑터 를 사용자 정의 합 니 다.

public class ImageAdapter extends BaseAdapter {
  private Context mContext;
  private int mPos;
  public ImageAdapter(Context context) {
    mContext = context;
  }
  public void setOwnposition(int ownposition) {
    this.mPos = ownposition;
  }
  public int getOwnposition() {
    return mPos;
  }
  @Override
  public int getCount() {
    return ImageSource.mThumbIds.length;
  }
  @Override
  public Object getItem(int position) {
    mPos=position;
    return position;
  }
  @Override
  public long getItemId(int position) {
    mPos=position;
    return position;
  }
  @Override
  public View getView(int position, View convertView, ViewGroup parent) {
    mPos=position;
    ImageView imageview = new ImageView(mContext);
    imageview.setBackgroundColor(0xFF000000);
    imageview.setScaleType(ImageView.ScaleType.FIT_CENTER);
    imageview.setLayoutParams(new myGallery.LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT));
    imageview.setImageResource(ImageSource.mThumbIds[position]);
    return imageview;
  }
}

그리고 ImageAdapter 로 Gallery 를 채 웁 니 다.

myGallery galllery = (myGallery) findViewById(R.id.mygallery);
Intent intent = getIntent();
position = intent.getIntExtra("position", 0); //   GridViewActivity       position
ImageAdapter imgAdapter=new ImageAdapter(this);
galllery.setAdapter(imgAdapter); //     ImageAdapter
galllery.setSelection(position); //         
  Animation an= AnimationUtils.loadAnimation(this,R.anim.scale ); // Gallery  
  galllery.setAnimation(an);

이때 세 심하게 살 펴 보면 우리 갤러리 도 자신 이 정의 한 my Gallery 입 니 다.구체 적 인 정 의 는 다음 과 같 습 니 다.

public class myGallery extends Gallery {
  boolean isFirst = false;
  boolean isLast = false;
  public myGallery(Context context) {
    super(context);
  }
  public myGallery(Context context, AttributeSet paramAttributeSet) {
    super(context, paramAttributeSet);
  }
  /**       (true -     ; false -     ) */
  private boolean isScrollingLeft(MotionEvent e1, MotionEvent e2) {
    return e2.getX() > e1.getX();
  }
  @Override
  public boolean onFling(MotionEvent e1, MotionEvent e2, float distanceX, float distanceY) {
    ImageAdapter ia = (ImageAdapter) this.getAdapter();
    int p = ia.getOwnposition(); //        position
    int count = ia.getCount(); //          count
    int kEvent;
    if (isScrollingLeft(e1, e2)) {
      if (p == 0 && isFirst) {
        Toast.makeText(this.getContext(), "     ", Toast.LENGTH_SHORT).show();
      } else if (p == 0) {
        isFirst = true;
      } else {
        isLast = false;
      }
      kEvent = KeyEvent.KEYCODE_DPAD_LEFT;
    } else {
      if (p == count - 1 && isLast) {
        Toast.makeText(this.getContext(), "      ", Toast.LENGTH_SHORT).show();
      } else if (p == count - 1) {
        isLast = true;
      } else {
        isFirst = false;
      }
      kEvent = KeyEvent.KEYCODE_DPAD_RIGHT;
    }
    onKeyDown(kEvent, null);
    return true;
  }
}

Gallery Activity 의 레이아웃 파일 gallery.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  android:layout_width="match_parent"
  android:layout_height="match_parent"
  android:gravity="center"
  android:orientation="horizontal"
  android:padding="10dip" >
  <RelativeLayout
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:background="#000000"
    android:padding="2dip" >
    <com.homer.gridgallery.myGallery
      android:id="@+id/mygallery"
      android:layout_width="fill_parent"
      android:layout_height="fill_parent"
      android:spacing="16dp" />
  </RelativeLayout>
</LinearLayout>

전체 인 스 턴 스 코드 는 여 기 를 클릭 하 십시오본 사이트 다운로드
더 많은 안 드 로 이 드 관련 내용 에 관심 이 있 는 독자 들 은 본 사이트 의 주 제 를 볼 수 있 습 니 다.,과 을 볼 수 있 습 니 다.
본 고 에서 말 한 것 이 여러분 의 안 드 로 이 드 프로 그래 밍 에 도움 이 되 기 를 바 랍 니 다.

좋은 웹페이지 즐겨찾기