Android入门基础之Gallery用法
来源:爱站网时间:2022-09-14编辑:网友分享
对于正在学习Android的朋友们,掌握一些基础知识是必然的。今天爱站技术频道小编就带大家来看看Android入门基础之Gallery用法,感兴趣的小伙伴千万不要错过了。
本文实例介绍的Android的Gallery控件是个很不错的看图控件,可以大大减轻开发者对于看图功能的开发,并且效果也很美观。本文实例中的Gallery的用法,主要实现用反射机制来动态读取资源中的图片。
该实例代码运行的效果图如下:
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"
>
<Gallery android:id="@+id/gallery" android:layout_height="fill_parent" android:layout_width="fill_parent"></Gallery>
</LinearLayout>
Java程序源码如下:
package com.testImageView;
import java.lang.reflect.Field;
import java.util.ArrayList;
import android.app.Activity;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Bundle;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.Gallery;
import android.widget.ImageView;
import android.widget.AdapterView.OnItemClickListener;
public class testImageView extends Activity {
private Gallery mGallery;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
mGallery = (Gallery)findViewById(R.id.gallery);
try {
mGallery.setAdapter(new ImageAdapter(this));
} catch (IllegalArgumentException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IllegalAccessException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
mGallery.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView parent, View v, int position, long id) {
testImageView.this.setTitle(String.valueOf(position));
}
});
}
/*
* class ImageAdapter is used to control gallery source and operation.
*/
private class ImageAdapter extends BaseAdapter{
private Context mContext;
private ArrayList imgList=new ArrayList();
private ArrayList
Android入门基础之Gallery用法内容分享到这里就结束了,希望朋友们对此都有所了解。关注爱站技术频道网站,每天带你体验不一样的技术文章。
