Android 프로 그래 밍 슬라이딩 효과 갤러리+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、GalleryGridView 의 그림 표시,감청 이 벤트 를 완료 한 후,현재 그림 을 클릭 하면 현재 클릭 한 그림 을 표시 하 는 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>
전체 인 스 턴 스 코드 는 여 기 를 클릭 하 십시오본 사이트 다운로드더 많은 안 드 로 이 드 관련 내용 에 관심 이 있 는 독자 들 은 본 사이트 의 주 제 를 볼 수 있 습 니 다.,과 을 볼 수 있 습 니 다.
본 고 에서 말 한 것 이 여러분 의 안 드 로 이 드 프로 그래 밍 에 도움 이 되 기 를 바 랍 니 다.
이 내용에 흥미가 있습니까?
현재 기사가 여러분의 문제를 해결하지 못하는 경우 AI 엔진은 머신러닝 분석(스마트 모델이 방금 만들어져 부정확한 경우가 있을 수 있음)을 통해 가장 유사한 기사를 추천합니다:
Bitrise에서 배포 어플리케이션 설정 테스트하기이 글은 Bitrise 광고 달력의 23일째 글입니다. 자체 또는 당사 등에서 Bitrise 구축 서비스를 사용합니다. 그나저나 며칠 전 Bitrise User Group Meetup #3에서 아래 슬라이드를 발표했...
텍스트를 자유롭게 공유하거나 복사할 수 있습니다.하지만 이 문서의 URL은 참조 URL로 남겨 두십시오.
CC BY-SA 2.5, CC BY-SA 3.0 및 CC BY-SA 4.0에 따라 라이센스가 부여됩니다.