Android 입문 Gallery 사용법 실례 분석
이 인 스 턴 스 코드 가 실 행 된 효과 도 는 다음 과 같 습 니 다.
main.xml 원본 코드 는 다음 과 같 습 니 다.
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<Gallery android:id="@+id/gallery" android:layout_height="fill_parent" android:layout_width="fill_parent"></Gallery>
</LinearLayout>
Java 프로그램 원본 코드 는 다음 과 같 습 니 다.
package com.testImageView;
import java.lang.reflect.Field;
import java.util.ArrayList;
import android.app.Activity;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Bundle;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.Gallery;
import android.widget.ImageView;
import android.widget.AdapterView.OnItemClickListener;
public class testImageView extends Activity {
private Gallery mGallery;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
mGallery = (Gallery)findViewById(R.id.gallery);
try {
mGallery.setAdapter(new ImageAdapter(this));
} catch (IllegalArgumentException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IllegalAccessException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
mGallery.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView parent, View v, int position, long id) {
testImageView.this.setTitle(String.valueOf(position));
}
});
}
/*
* class ImageAdapter is used to control gallery source and operation.
*/
private class ImageAdapter extends BaseAdapter{
private Context mContext;
private ArrayList<Integer> imgList=new ArrayList<Integer>();
private ArrayList<Object> imgSizes=new ArrayList<Object>();
public ImageAdapter(Context c) throws IllegalArgumentException, IllegalAccessException{
mContext = c;
// ID
Field[] fields = R.drawable.class.getDeclaredFields();
for (Field field : fields)
{
if (!"icon".equals(field.getName()))// icon
{
int index=field.getInt(R.drawable.class);
// ID
imgList.add(index);
//
int size[]=new int[2];
Bitmap bmImg=BitmapFactory.decodeResource(getResources(),index);
size[0]=bmImg.getWidth();size[1]=bmImg.getHeight();
imgSizes.add(size);
}
}
}
@Override
public int getCount() {
// TODO Auto-generated method stub
return imgList.size();
}
@Override
public Object getItem(int position) {
// TODO Auto-generated method stub
return position;
}
@Override
public long getItemId(int position) {
// TODO Auto-generated method stub
return position;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
// TODO Auto-generated method stub
ImageView i = new ImageView (mContext);
// imgList ID
i.setImageResource(imgList.get(position).intValue());
i.setScaleType(ImageView.ScaleType.FIT_XY);
// imgSizes
int size[]= new int[2];
size=(int[]) imgSizes.get(position);
i.setLayoutParams(new Gallery.LayoutParams(size[0], size[1]));
return i;
}
};
}
이 내용에 흥미가 있습니까?
현재 기사가 여러분의 문제를 해결하지 못하는 경우 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에 따라 라이센스가 부여됩니다.