• 博客园logo
  • 会员
  • 众包
  • 新闻
  • 博问
  • 闪存
  • 赞助商
  • HarmonyOS
  • Chat2DB
    • 搜索
      所有博客
    • 搜索
      当前博客
  • 写随笔 我的博客 短消息 简洁模式
    用户头像
    我的博客 我的园子 账号设置 会员中心 简洁模式 ... 退出登录
    注册 登录
lamber
博客园    首页    新随笔       管理    订阅  订阅

Android 的基本组件之一 Gallery

声明:本人博客纯属个人学习过程中的一些仿写的简单练习记录,其他论坛也有类似内容!(可能不免有错误之处,还望见谅,指出)

这是一个最简单可以滑动查看图片的应用程序:

首先创建项目名为: Gallery My photo

代码如下:
package com.slide.demo;
import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.Gallery;
import android.widget.ImageView;
public class Slide extends Activity {
private Gallery mgallery; //设置Gallery对象名
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
mgallery = (Gallery) findViewById(R.id.Picture); //联系到视图布局
ImageAdapter madapter = new ImageAdapter(this); //构建一个适配器
mgallery.setAdapter(madapter);
mgallery.setSpacing(6); //设置图片之间的距离
}
private class ImageAdapter extends BaseAdapter {
int[] images = { R.drawable.image0, R.drawable.image1,
R.drawable.image3, R.drawable.image4, R.drawable.image5,
R.drawable.image2, }; //联系到drawable文件夹中要显示的图片(这里有6张)
Context mContext;
//构造函数
public ImageAdapter(Context ctx) {
mContext = ctx;
}
public int getCount() {
return images.length;
} //得到图片的数量
public Object getItem(int position) {
return images[position];
} //继承父类中的方法,获取图片数据中指定位置的设置
public long getItemId(int position) {
return 0;
} //继承父类中的方法,获取id和图片集里面的位置
@Override
public View getView(int position, View convertView, ViewGroup parent) {
ImageView iv = null;
if (convertView == null) {
iv = new ImageView(mContext);
iv.setImageResource(images[position]);
} else {
iv = (ImageView) convertView;
}
return iv; //判断图片显示位置是否为空,若为空即设置下一张图片填充
}
}
}

视图布局文件 main xml :
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="fill_parent"
android:layout_height="fill_parent" android:gravity="center"
android:longClickable="true">
>
<Gallery android:id="@+id/Picture" android:layout_width="fill_parent"
android:layout_height="wrap_content"/>
</LinearLayout>
应用配置文件 AndroidMainfest xml :
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
      package="com.slide.demo"
      android:versionCode="1"
      android:versionName="1.0">
    <application android:icon="@drawable/icon" android:label="@string/app_name">
        <activity android:name=".Slide"
                  android:label="@string/app_name">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>
</manifest> 
想要获得成功,首先要自己相信自己,再者要赢得周围朋友的信任!
posted @ 2010-11-26 18:41  android5k  阅读(641)  评论(0)    收藏  举报
刷新页面返回顶部
博客园  ©  2004-2025
浙公网安备 33010602011771号 浙ICP备2021040463号-3