Gallery的使用(一)
来源:互联网 发布:淘宝不能用网银支付 编辑:程序博客网 时间:2024/05/22 13:27
Gallerty组件一般用于显示图像列表,Gallery只能水平显示一行,而且支持水平滑动效果。单击、选中或拖动Gallerty中的图像,Gallery中的图像列表会根据不同的情况向左或向右移动,直到显示到最后一个图像为止。
package com.example.androidtest;import android.os.Bundle;import android.app.Activity;import android.content.Context;import android.content.res.TypedArray;import android.view.View;import android.view.ViewGroup;import android.widget.AdapterView;import android.widget.AdapterView.OnItemClickListener;import android.widget.BaseAdapter;import android.widget.Gallery;import android.widget.ImageView;import android.widget.Toast;public class MainActivity extends Activity {Integer[] imageIDs = {R.drawable.ic_launcher,R.drawable.ic_launcher,R.drawable.ic_launcher,R.drawable.ic_launcher,R.drawable.ic_launcher,R.drawable.ic_launcher,R.drawable.ic_launcher};public void onCreate(Bundle savedInstanceState){super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);Gallery gallery = (Gallery)findViewById(R.id.gallery1);gallery.setAdapter(new ImageAdapter(this));gallery.setOnItemClickListener(new OnItemClickListener() {public void onItemClick(AdapterView<?> parent, View v, int position, long id){Toast.makeText(getBaseContext(), "pic" + (position + 1) + " selected", Toast.LENGTH_SHORT).show();ImageView imageView = (ImageView)findViewById(R.id.image1);imageView.setImageResource(imageIDs[position]);}});}public class ImageAdapter extends BaseAdapter{private Context context;private int itemBackground;public ImageAdapter(Context c){context = c;TypedArray a = obtainStyledAttributes(R.styleable.Gallery1);itemBackground = a.getResourceId(R.styleable.Gallery1_android_galleryItemBackground, 0);a.recycle();}public int getCount(){return imageIDs.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 imageView = new ImageView(context);imageView.setImageResource(imageIDs[position]);imageView.setScaleType(ImageView.ScaleType.FIT_XY);imageView.setLayoutParams(new Gallery.LayoutParams(150, 120));imageView.setBackgroundResource(itemBackground);return imageView;}}}
在res/values下添加styleable.xml文件:
<?xml version="1.0" encoding="UTF-8"?><resources> <declare-styleable name="Gallery1"> <attr name="android:galleryItemBackground" /> </declare-styleable> </resources>
activity_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"> <TextView android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="Images of San Francisco"/> <Gallery android:id="@+id/gallery1" android:layout_width="fill_parent" android:layout_height="wrap_content"/> <ImageView android:id="@+id/image1" android:layout_width="320px" android:layout_height="250px" android:scaleType="fitXY"/> </LinearLayout>
0 0
- Gallery的使用(一)
- Gallery使用示例(一)
- ImageSwitcher的使用(一):与Gallery配合使用
- Gallery的使用
- android gallery的使用
- Gallery控件的使用
- Gallery的使用
- Gallery控件的使用
- Gallery的使用
- Gallery的使用
- Gallery的使用
- Gallery的使用
- android 这 Gallery的使用
- Gallery控件的基本使用
- ImageSwitcher 和 Gallery 的使用
- ImageView和Gallery的使用
- Android组件的使用:Gallery
- Android中的Gallery的使用
- Java hashCode() 和 equals()的若干问题解答
- 二零一四年,我又开始博客了
- 继承、组合、委托
- java内部类
- node.js第一课(简介)
- Gallery的使用(一)
- js数组的操作
- 一道经典面试题,atoi函数的实现
- 第一次写东西
- 数据库中的一些语句
- OpenMP编程->库函数
- write的多进程/线程安全性
- java线程中的传统定时器Timer和TimerTask的使用方法
- 返回键使程序后台,使用notification进入前台