说到相册不得不介绍Android中一种重要的视图,网格视图:GridView是以网格形式显示所有的组件的,例如制作相册,所有的图片以相同大小显示在格子里

  • 网格视图制作相册

两种方法,一种定义simpleAdapter这一种与前面ListView封装十分相似

那么一样要用到模板grid_layout.xml

<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"    android:layout_width="wrap_content"    android:layout_height="wrap_content"    android:background="#000000"    android:orientation="horizontal" >    <ImageView        android:id="@+id/img"        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:padding="3px"           android:scaleType="center" /></LinearLayout>

我们只需要放图片,接着我们在drawable文件夹中保存几张图片,均以l开头格式

定义Main组件

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"    xmlns:tools="http://schemas.android.com/tools"    android:id="@+id/LinearLayout1"    android:layout_width="fill_parent"    android:layout_height="fill_parent"    android:orientation="vertical"        tools:context=".MainActivity" >    <GridView        android:id="@+id/gridView"        android:layout_width="fill_parent"        android:layout_height="wrap_content"                android:stretchMode="columnWidth"               android:numColumns="3" >    </GridView></LinearLayout>

在面代码中我们仍会定义一个initAdapter()方法,此方法通过反射机制取出定义的全部图片,这一节前一节课讲过。

定义Activity程序:

public class MainActivity extends Activity {private List<Map<String,Integer>> list=new ArrayList<Map<String,Integer>>();private SimpleAdapter simpleadapter=null;private GridView gridview=null;protected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);this.gridview=(GridView)super.findViewById(R.id.gridView);this.initAdapter();//初始化适配器this.gridview.setAdapter(simpleadapter);//设置图片,将封装好的图片放入this.gridview.setOnItemClickListener(new ItemListener());//设置组件监听器}private class ItemListener implements OnItemClickListener{@Overridepublic void onItemClick(AdapterView<?> arg0, View arg1, int position,long id) {// TODO Auto-generated method stub//相当于之前的转换工厂,不使用将会报错ImageView imageview=new ImageView(MainActivity.this);imageview.setBackgroundColor(0xFFFFFFFF);//设置背景imageview.setScaleType(ImageView.ScaleType.CENTER);//居中//自适应图片imageview.setLayoutParams(new ImageSwitcher.LayoutParams(LayoutParams.FILL_PARENT,LayoutParams.FILL_PARENT));//取出初始化适配器装入的mapMap<String,Integer> map=(Map<String,Integer>)MainActivity.this.simpleadapter.getItem(position);//开始放图imageview.setImageResource(map.get("img"));Dialog dialog=new AlertDialog.Builder(MainActivity.this).setIcon(R.drawable.check).setTitle("查看图片").setView(imageview).setNegativeButton("关闭", null).create();dialog.show();}}public boolean onCreateOptionsMenu(Menu menu) {// Inflate the menu; this adds items to the action bar if it is present.getMenuInflater().inflate(R.menu.main, menu);return true;}private void initAdapter() {// TODO Auto-generated method stubField[] field=R.drawable.class.getDeclaredFields();//取得全部属性,反射机制动态取图for(int i=0;i<field.length;i++){if(field[i].getName().startsWith("h")){Map<String,Integer> map=new HashMap<String,Integer>();try {map.put("img", field[i].getInt(R.drawable.class));//图片资源放入map} catch (IllegalAccessException e) {// TODO Auto-generated catch blocke.printStackTrace();} catch (IllegalArgumentException e) {// TODO Auto-generated catch blocke.printStackTrace();}this.list.add(map);    //保存map}}//使用参见listview博客this.simpleadapter=new SimpleAdapter(this,this.list,R.layout.grid_layout,//模板new String[]{"img"},//将要装入的键new int[]{R.id.img});//装的值}}

实现一个每当用户选择一张图片,就会弹出一个对话框显示用户选择的图片。程序运行效果如下:


点击某一图片后:



第二种方法是定义一个适配器类,继承BaseAdapter

这里不再对第二种方法多做阐述,为了以后设计方便建议采用第一种方法,1符合MVC设计模式

  • 时钟组件

直接布局文件中设置时钟组件

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"    xmlns:tools="http://schemas.android.com/tools"    android:id="@+id/LinearLayout1"    android:layout_width="match_parent"    android:layout_height="match_parent"    android:orientation="vertical"    android:paddingBottom="@dimen/activity_vertical_margin"    android:paddingLeft="@dimen/activity_horizontal_margin"    android:paddingRight="@dimen/activity_horizontal_margin"    android:paddingTop="@dimen/activity_vertical_margin"    tools:context=".MainActivity" >    <AnalogClock        android:id="@+id/analogClock1"        android:layout_width="wrap_content"        android:layout_height="wrap_content" /></LinearLayout>

然后效果图就出来了,可以再加上DIgitalClock



  • 计时器:Chronometer 本节我们来做个秒表小小闹钟到点会震动提示

首先定义布局文件

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"    xmlns:tools="http://schemas.android.com/tools"    android:id="@+id/LinearLayout1"    android:layout_width="fill_parent"    android:layout_height="fill_parent"    android:orientation="vertical"        tools:context=".MainActivity" >    <Chronometer        android:id="@+id/chronometer1"        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:text="Chronometer" />    <LinearLayout        android:layout_width="match_parent"        android:layout_height="wrap_content" >        <Button            android:id="@+id/button1"            android:layout_width="wrap_content"            android:layout_height="wrap_content"            android:text="开始" />        <Button            android:id="@+id/button2"            android:layout_width="wrap_content"            android:layout_height="wrap_content"            android:text="停止" />        <Button            android:id="@+id/button3"            android:layout_width="wrap_content"            android:layout_height="wrap_content"            android:text="复位" />        <Button            android:id="@+id/button4"            android:layout_width="wrap_content"            android:layout_height="wrap_content"            android:text="格式化显示" />    </LinearLayout></LinearLayout>

然后定义acticity代码

public class MainActivity extends Activity {private Chronometer clock=null;private Button start=null;//开始计时private Button stop=null;//停止计时private Button base=null;//设置基准时间private Button format=null;//设置格式private Vibrator vibrator=null;protected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);this.clock=(Chronometer)super.findViewById(R.id.chronometer1);this.start=(Button)super.findViewById(R.id.button1);this.stop=(Button)super.findViewById(R.id.button2);this.base=(Button)super.findViewById(R.id.button3);this.format=(Button)super.findViewById(R.id.button4);this.vibrator=(Vibrator)super.getApplication().getSystemService(Service.VIBRATOR_SERVICE);this.start.setOnClickListener(new StartListener());this.stop.setOnClickListener(new StopListener());this.base.setOnClickListener(new BaseListener());this.format.setOnClickListener(new FormatListener());this.clock.setOnChronometerTickListener(new OnChronometer());}private class OnChronometer implements OnChronometerTickListener{@Overridepublic void onChronometerTick(Chronometer arg0) {// TODO Auto-generated method stubString time=clock.getText().toString().replaceAll("[^(\\d{2}:\\d{2})]", "");if("01:00".equals(time)){MainActivity.this.vibrator.vibrate(new long[]{1000, 10,1000,100},0);//设置震动周期以及循环震动}}}private class StartListener implements OnClickListener{@Overridepublic void onClick(View arg0) {// TODO Auto-generated method stubMainActivity.this.clock.start();//开始计时}}private class StopListener implements OnClickListener{@Overridepublic void onClick(View arg0) {// TODO Auto-generated method stubMainActivity.this.clock.stop();//停止计时MainActivity.this.vibrator.cancel();//取消震动}}private class BaseListener implements OnClickListener{@Overridepublic void onClick(View arg0) {// TODO Auto-generated method stubMainActivity.this.clock.setBase(SystemClock.elapsedRealtime());//复位}}private class FormatListener implements OnClickListener{@Overridepublic void onClick(View arg0) {// TODO Auto-generated method stubMainActivity.this.clock.setFormat("新显示格式:%s。");}}@Overridepublic boolean onCreateOptionsMenu(Menu menu) {// Inflate the menu; this adds items to the action bar if it is present.getMenuInflater().inflate(R.menu.main, menu);return true;}}

最后记得配置权限

<uses-permission        android:name="android.permission.VIBRATE"                 />

最后运行效果如下:

设置满一分钟震动




更多相关文章

  1. Android入门:查看服务器图片应用
  2. 用android来实现图片的绘制以及旋转缩放案例分析
  3. android学习笔记(三)基础UI组件1——按钮,文本框,CheckBox,Radiobutto
  4. Android(安卓)8.1 关机充电动画(二)Uboot模式
  5. Android开发Error(建议收藏下来以备不时之需)android.os.Transacti
  6. Android(安卓)BaseAdapter应用
  7. android 生成二维码图片
  8. Android(安卓)开源项目推荐
  9. 个人android遇到的问题总结

随机推荐

  1. Android(安卓)Audio相关 AudioFlinger类
  2. Android(安卓)ant 命令行手动编译打包详
  3. Android(安卓)-wifi 直连(wifi direct )
  4. Android(安卓)使用Glide加载图片变色
  5. 重学Android——View的事件分发
  6. Android(安卓)glide 4.x找不到GlideApp
  7. Android(安卓)WebView Scrolling Perform
  8. Android图片压缩——Luban鲁班压缩
  9. android dpi dp 和px之间的关系
  10. android 使用SharedPreferences保存list