展会信息港展会大全

Android Gallery运用
来源:互联网   发布日期:2015-10-03 10:44:59   浏览:1958次  

导读: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.conte...

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));www.2cto.com

/* 设定Gallery背景图 */

i.setBackgroundResource(mGalleryItemBackground);

/* 传回imageView对象 */

return i;

}

}

// public void setWallpaper(InputStream data) throws IOException {

// // TODO Auto-generated method stub

// super.setWallpaper(data);

// }

作者:gongzibai

赞助本站

人工智能实验室

相关热词: android开发 教程

AiLab云推荐
展开

热门栏目HotCates

Copyright © 2010-2024 AiLab Team. 人工智能实验室 版权所有    关于我们 | 联系我们 | 广告服务 | 公司动态 | 免责声明 | 隐私条款 | 工作机会 | 展会港