Android Gallery运用
package bizhi.co.cc;
import java.io.InputStream;
import net.miidi.credit.MiidiCredit;
import android.app.Activity;
import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInte易做图ce;
import android.content.res.Resources;
import android.content.res.TypedArray;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.view.Window;
import android.view.WindowManager;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.Gallery;
import android.widget.ImageView;
public class BizhiActivity extends
Activity {
protected static InputStream iStream;
private ImageAdapter mImageAdapter;
ImageView mImageView;
private int[] image = {
R.drawable.shuzi1,
R.drawable.shuzi18,
R.drawable.shuzi2,
R.drawable.shuzi3,
R.drawable.shuzi4,
R.drawable.shuzi5,
R.drawable.shuzi6,
R.drawable.shuzi7,
R.drawable.shuzi9,
R.drawable.shuzi10,
R.drawable.shuzi11,
R.drawable.shuzi12,
R.drawable.shuzi13,
R.drawable.shuzi14,
R.drawable.shuzi15,
R.drawable.shuzi16,
R.drawable.shuzi17,
R.drawable.shuzi19,
R.drawable.shuzi20,
};
/** Called when the activity is first created. */
@Override
public void onCreate(
Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
getWindow()
.setFlags(
WindowManager.LayoutParams.FLAG_FULLSCREEN,
WindowManager.LayoutParams.FLAG_FULLSCREEN);
MiidiCredit.init(BizhiActivity.this, "5677",
"iuwruztprel0txtd", false);
setContentView(R.layout.main);
mImageAdapter = new ImageAdapter(
BizhiActivity.this,
image);
Gallery mGallery = (Gallery) findViewById(R.id.gallery1);
mImageView = (ImageView) findViewById(R.id.imageView1);
// int mGalleryItemBackground;
// TypedArray a = obtainStyledAttributes(R.styleable.Gallery);
// mGalleryItemBackground = a.getResourceId(
// R.styleable.Gallery_android_galleryItemBackground, 0);
// a.recycle();
// mImageView.setBackgroundResource(mGalleryItemBackground);
mGallery.setAdapter(mImageAdapter);
mGallery.setOnItemClickListener(new Gallery.OnItemClickListener() {
@Override
public void onItemClick(
AdapterView<?> arg0,
View arg1,
int arg2, long arg3) {
// TODO Auto-generated method stub
mImageView
.setImageResource(mImageAdapter.image[arg2]);
Resources resources = getBaseContext()
.getResources();
iStream = resources
.openRawResource(mImageAdapter.image[arg2]);
}
});
mImageView
.setOnClickListener(new OnClickListener() {
@Override
public void onClick(
View v) {
// TODO Auto-generated method stub
new AlertDialog.Builder(
BizhiActivity.this)
.setTitle(
"正确答案:")
.setMessage(
R.string.daan)
.setPositiveButton(
"确定",
new DialogInte易做图ce.OnClickListener() {
@Override
public void onClick(
DialogInte易做图ce dialog,
int which) {
// TODO Auto-generated method
// stub
}
})
.create()
.show();
}
});
}
public class ImageAdapter extends
BaseAdapter {
private Context context;
private int[] image;
int mGalleryItemBackground;
public ImageAdapter(Context c,
int[] image2) {
this.context = c;
this.image = image2;
TypedArray a = obtainStyledAttributes(R.styleable.Gallery);
mGalleryItemBackground = a
.getResourceId(
R.styleable.Gallery_android_galleryItemBackground,
0);
a.recycle();
}
@Override
public int getCount() {
// TODO Auto-generated method stub
return image.length;
}
@Override
public Object getItem(int arg0) {
// TODO Auto-generated method stub
return null;
}
@Override
public long getItemId(int arg0) {
// TODO Auto-generated method stub
return arg0;
}
@Override
public View getView(int arg0,
View arg1,
ViewGroup arg2) {
// TODO Auto-generated method stub
/* 产生ImageView对象 */
ImageView i = new ImageView(
context);
/* 设定图片给imageView对象 */
i.setImageResource(image[arg0]);
/* 重新设定图片的宽高 */
i.setScaleType(ImageView.ScaleType.FIT_XY);
/* 重新设定Layout的宽高 */
i.setLayoutParams(new Gallery.LayoutParams(
210, 190)); www.zzzyk.com
/* 设定Gallery背景图 */
i.setBackgroundResource(mGalleryItemBackground);
/* 传回imageView对象 */
return i;
}
}
// public void
补充:移动开发 , Android ,