Android基础篇之ImageSwitcher与Gallery

本文详细介绍了如何在Android中利用ImageSwitcher与Gallery组件结合使用,来实现图片浏览器的功能。通过配置布局文件和编写关键代码,实现了图片的循环展示和选择时的联动效果。

Android基础篇之ImageSwitcher与Gallery

2011-05-19 16:16:15

自己制作一个图片浏览器的时候ImageSwitcher与Gallery组合使用是一个比较简遍的方法,现在我们蓝看一下他们的基本用法。

1.配置布局文件 main.xml

<?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="fill_parent" > <ImageSwitcher android:id="@+id/imageSwitcher" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_centerInParent="true"/> <Gallery android:id="@+id/gallery" android:layout_width="fill_parent" android:layout_height="wrap_content" android:spacing="8dp" android:layout_alignParentBottom="true"/> </RelativeLayout>

2.程序主要代码 MainActivity.java

package com.yin.picture_view; import android.app.Activity; import android.content.Context; import android.os.Bundle; import android.view.View; import android.view.ViewGroup; import android.widget.AdapterView; import android.widget.AdapterView.OnItemSelectedListener; import android.widget.BaseAdapter; import android.widget.Gallery; import android.widget.ImageSwitcher; import android.widget.ImageView; import android.widget.ViewSwitcher.ViewFactory; public class MainActivity extends Activity { //显示图片的资源 private static int[] images = { R.drawable.s0 , R.drawable.s1, R.drawable.s2 , R.drawable.s3, R.drawable.s4 , R.drawable.s5, R.drawable.s6 , R.drawable.s7, R.drawable.s8 , R.drawable.s9 }; Gallery gallery; ImageSwitcher is; public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); gallery = (Gallery) findViewById(R.id.gallery); is = (ImageSwitcher) findViewById(R.id.imageSwitcher); gallery.setAdapter(new ImageAdapter(this)); //让选定的图片在中心显示 gallery.setSelection(images.length/2); //为Gallery绑定监听器; gallery.setOnItemSelectedListener(new OnItemSelectedListener() { public void onItemSelected(AdapterView<?> parent, View view, int position, long id) { //当在Gallery中选定一张图片是 ImageSwitcher同步显示同一张 //position%images.length 为了让图片循环显示 is.setImageResource(images[position%images.length]); } public void onNothingSelected(AdapterView<?> parent) { } }); is.setFactory(new ImageFactory(this)); } private class ImageAdapter extends BaseAdapter{ private Context context; public ImageAdapter(Context context) { this.context = context; } //可以return images.lenght(),在这里返回Integer.MAX_VALUE //是为了使图片循环显示 public int getCount() { return Integer.MAX_VALUE; } public Object getItem(int position) { return null; } public long getItemId(int position) { return 0; } public View getView(int position, View convertView, ViewGroup parent) { ImageView iv = new ImageView(context); iv.setImageResource(images[position%images.length]); iv.setLayoutParams(new Gallery.LayoutParams(90,90)); iv.setAdjustViewBounds(true); return iv; } } private class ImageFactory implements ViewFactory{ private Context context; public ImageFactory(Context context){ this.context = context; } public View makeView() { ImageView iv = new ImageView(context); iv.setLayoutParams(new ImageSwitcher.LayoutParams(200,200)); return iv; } } }

【EI复现】基于深度强化学习的微能源网能量管理优化策略研究(Python代码实现)内容概要:本文围绕“基于深度强化学习的微能源网能量管理优化策略”展开研究,重点利用深度Q网络(DQN)等深度强化学习算法对微能源网中的能量调度进行建模优化,旨在应对可再生能源出力波动、负荷变化及运行成本等问题。文中结合Python代码实现,构建了包含光伏、储能、负荷等元素的微能源网模型,通过强化学习智能体动态决策能量分配策略,实现经济性、稳定性和能效的多重优化目标,并可能其他优化算法进行对比分析以验证有效性。研究属于电力系统人工智能交叉领域,具有较强的工程应用背景和学术参考价值。; 适合人群:具备一定Python编程基础和机器学习基础知识,从事电力系统、能源互联网、智能优化等相关方向的研究生、科研人员及工程技术人员。; 使用场景及目标:①学习如何将深度强化学习应用于微能源网的能量管理;②掌握DQN等算法在实际能源系统调度中的建模实现方法;③为相关课题研究或项目开发提供代码参考和技术思路。; 阅读建议:建议读者结合提供的Python代码进行实践操作,理解环境建模、状态空间、动作空间及奖励函数的设计逻辑,同时可扩展学习其他强化学习算法在能源系统中的应用。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值