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.DialogInterface;
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 DialogInterface.OnClickListener() {

@Override
public void onClick(
DialogInterface 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));

/* 设定Gallery背景图 */
i.setBackgroundResource(mGalleryItemBackground);
/* 传回imageView对象 */
return i;
}

}

// public void setWallpaper(InputStream data) throws IOException {
// // TODO Auto-generated method stub
// super.setWallpaper(data);
// }

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值