編輯:Android開發實例
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>
程序源碼:
- 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<Integer> imgList=new ArrayList<Integer>();
- private ArrayList<Object> imgSizes=new ArrayList<Object>();
- public ImageAdapter(Context c) throws IllegalArgumentException, IllegalAccessException{
- mContext = c;
- //用反射機制來獲取資源中的圖片ID和尺寸
- Field[] fields = R.drawable.class.getDeclaredFields();
- for (Field field : fields)
- {
- if (!"icon".equals(field.getName()))//除了icon之外的圖片
- {
- int index=field.getInt(R.drawable.class);
- //保存圖片ID
- imgList.add(index);
- //保存圖片大小
- int size[]=new int[2];
- Bitmap bmImg=BitmapFactory.decodeResource(getResources(),index);
- size[0]=bmImg.getWidth();size[1]=bmImg.getHeight();
- imgSizes.add(size);
- }
- }
- }
- @Override
- public int getCount() {
- // TODO Auto-generated method stub
- return imgList.size();
- }
- @Override
- public Object getItem(int position) {
- // TODO Auto-generated method stub
- return position;
- }
- @Override
- public long getItemId(int position) {
- // TODO Auto-generated method stub
- return position;
- }
- @Override
- public View getView(int position, View convertView, ViewGroup parent) {
- // TODO Auto-generated method stub
- ImageView i = new ImageView (mContext);
- //從imgList取得圖片ID
- i.setImageResource(imgList.get(position).intValue());
- i.setScaleType(ImageView.ScaleType.FIT_XY);
- //從imgSizes取得圖片大小
- int size[]= new int[2];
- size=(int[]) imgSizes.get(position);
- i.setLayoutParams(new Gallery.LayoutParams(size[0], size[1]));
- return i;
- }
- };
- }
Android提供了許多方法來控制播放的音頻/視頻文件和流。其中該方法是通過一類稱為MediaPlayer。Android是提供MediaPlayer類訪問內置的媒體播放
登錄應用程序的屏幕,詢問憑據登錄到一些特定的應用。可能需要登錄到Facebook,微博等本章介紹了,如何創建一個登錄界面,以及如何管理安全問題和錯誤嘗試。首先,必須定義兩
Android模擬器報Failed To Allocate memory 8錯誤的解決辦法Android在啟動模擬器AVD時,出現下面的異常:“Failed to
可以顯示在的Android任務,通過加載進度條的進展。進度條有兩種形狀。加載欄和加載微調(spinner)。在本章中,我們將討論微調(spinner)。Spinner 用