Gallery gallery = (Gallery) findViewById(R.id.gallery);
ImageAdapter。 gallery.setAdapter(new ImageAdapter(this));
private Integer[] mps = {
R.drawable.icon1,
R.drawable.icon2,
R.drawable.icon3,
R.drawable.icon4,
R.drawable.icon5
};
public class ImageAdapter extends BaseAdapter {
private Context mContext;
public ImageAdapter(Context context) {
mContext = context;
}
public int getCount() {
return mps.length;
}
public Object getItem(int position) {
return position;
}
public long getItemId(int position) {
return position;
}
public View getView(int position, View convertView, ViewGroup parent) {
ImageView image = new ImageView(mContext);
image.setImageResource(mps[position]);
image.setAdjustViewBounds(true);
image.setLayoutParams(new Gallery.LayoutParams(
LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
return image;
}
}
gallery.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
@Override
public void onItemSelected(AdapterView<?> parent, View v,int position, long id) {
}
@Override
public void onNothingSelected(AdapterView<?> arg0) {
//这里不做响应
}
});
private boolean isScrollingLeft(MotionEvent e1, MotionEvent e2) {
return e2.getX() > e1.getX();
}
@Override
public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX,
float velocityY) {
int kEvent;
if (isScrollingLeft(e1, e2)) {
kEvent = KeyEvent.KEYCODE_DPAD_LEFT;
} else {
kEvent = KeyEvent.KEYCODE_DPAD_RIGHT;
}
onKeyDown(kEvent, null);
return true;
}
@Override
public int getCount() {
// TODO Auto-generated method stub
return Integer.MAX_VALUE;
}
……
@Override
public View getView(int position, View convertView, ViewGroup arg2) {
// TODO Auto-generated method stub
ImageView imageView = new ImageView(context);
imageView.setBackgroundResource(imagList.get(position%imagList.size()));
imageView.setScaleType(ScaleType.FIT_XY);
imageView.setLayoutParams(new Gallery.LayoutParams(Gallery.LayoutParams.FILL_PARENT
, Gallery.LayoutParams.WRAP_CONTENT));
return imageView;
}
//定时器和事件处理5秒刷新一次幻灯片
/** 展示图控制器,实现展示图切换 */
final Handler handler_gallery = new Handler() {
public void handleMessage(Message msg) {
/* 自定义屏幕按下的动作 */
MotionEvent e1 = MotionEvent.obtain(SystemClock.uptimeMillis(),
SystemClock.uptimeMillis(), MotionEvent.ACTION_UP,
89.333336f, 265.33334f, 0);
/* 自定义屏幕放开的动作 */
MotionEvent e2 = MotionEvent.obtain(SystemClock.uptimeMillis(),
SystemClock.uptimeMillis(), MotionEvent.ACTION_DOWN,
300.0f, 238.00003f, 0);
myGallery.onFling(e2, e1, -800, 0);
/* 给gallery添加按下和放开的动作,实现自动滑动 */
super.handleMessage(msg);
}
};
protected void onResume() {
autogallery();
super.onResume();
};
private void autogallery() {
/* 设置定时器,每5秒自动切换展示图 */
Timer time = new Timer();
TimerTask task = new TimerTask() {
@Override
public void run() {
Message m = new Message();
handler_gallery.sendMessage(m);
}
};
time.schedule(task, 8000, 5000);
}
//指示按钮和gallery初始化过程以及事件监听添加过程
//初始化
void init(){
myGallery = (DetailGallery)findViewById(R.id.myGallery);
gallery_points = (RadioGroup) this.findViewById(R.id.galleryRaidoGroup);
ArrayList<Integer> list = new ArrayList<Integer>();
list.add(R.drawable.banner1);
list.add(R.drawable.banner2);
list.add(R.drawable.banner3);
list.add(R.drawable.banner4);
GalleryIndexAdapter adapter = new GalleryIndexAdapter(list, context);
myGallery.setAdapter(adapter);
//设置小按钮
gallery_point = new RadioButton[list.size()];
for (int i = 0; i < gallery_point.length; i++) {
layout = (LinearLayout) inflater.inflate(R.layout.gallery_icon, null);
gallery_point[i] = (RadioButton) layout.findViewById(R.id.gallery_radiobutton);
gallery_point[i].setId(i);/* 设置指示图按钮ID */
int wh = Tool.dp2px(context, 10);
RadioGroup.LayoutParams layoutParams = new RadioGroup.LayoutParams(wh, wh); // 设置指示图大小
gallery_point[i].setLayoutParams(layoutParams);
layoutParams.setMargins(4, 0, 4, 0);// 设置指示图margin值
gallery_point[i].setClickable(false);/* 设置指示图按钮不能点击 */
layout.removeView(gallery_point[i]);//一个子视图不能指定了多个父视图
gallery_points.addView(gallery_point[i]);/* 把已经初始化的指示图动态添加到指示图的RadioGroup中 */
}
}
//添加事件
void addEvn(){
myGallery.setOnItemSelectedListener(new OnItemSelectedListener() {
@Override
public void onItemSelected(AdapterView<?> arg0, View arg1,
int arg2, long arg3) {
// TODO Auto-generated method stub
gallery_points.check(gallery_point[arg2%gallery_point.length].getId());
}
@Override
public void onNothingSelected(AdapterView<?> arg0) {
// TODO Auto-generated method stub
}
});
}
//由于是测试case,所以图片都是写死的为了区别,在position = 1的时候换了一张图片
public View getView(int position, View convertView, ViewGroup parent) {
// TODO Auto-generated method stub
ImageView imageView = (ImageView) LayoutInflater.from(context).inflate(R.layout.img,
null);
Bitmap bitmap = null;
try {
if(position == 1 ){
bitmap = BitmapFactory.decodeStream(assetManager.open("xpic11247_s.jpg"));
imageView.setTag("xpic11247_s.jpg");
}
else{
bitmap = BitmapFactory.decodeStream(assetManager.open("item0_pic.jpg"));
imageView.setTag("item0_pic.jpg");
}
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
// 加载图片之前进行缩放
int width = bitmap.getWidth();
int height = bitmap.getHeight();
float newHeight = 200;
float newWidth = width*newHeight/height;
float scaleWidth = ((float) newWidth) / width;
float scaleHeight = ((float) newHeight) / height;
// 取得想要缩放的matrix参数
Matrix matrix = new Matrix();
matrix.postScale(scaleWidth, scaleHeight);
// 得到新的图片
Bitmap newbm = Bitmap.createBitmap(bitmap, 0, 0, width, height, matrix, true);
System.out.println(newbm.getHeight()+"-----------"+newbm.getWidth());
imageView.setImageBitmap(newbm);
// }
return imageView;
}
<?xml version="1.0" encoding="utf-8"?> <ImageView xmlns:android="http://schemas.android.com/apk/res/android" android:id="@+id/waterfall_image" android:layout_width="fill_parent" android:layout_height="fill_parent" android:background="@drawable/image_border" > </ImageView>
layout.removeView(gallery_point[i]);//一个子视图不能指定了多个父视图
机械节能产品生产企业官网模板...
大气智能家居家具装修装饰类企业通用网站模板...
礼品公司网站模板
宽屏简约大气婚纱摄影影楼模板...
蓝白WAP手机综合医院类整站源码(独立后台)...苏ICP备2024110244号-2 苏公网安备32050702011978号 增值电信业务经营许可证编号:苏B2-20251499 | Copyright 2018 - 2025 源码网商城 (www.ymwmall.com) 版权所有