Android 控制項之Gallery圖片集

來源:互聯網
上載者:User

  Gallery是Android中的圖片庫控制項。先看效果,爽一番

 

 源碼下載

 一、簡介

  在中心鎖定,水平顯示列表的項。

二、執行個體

1.布局檔案

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
  xmlns:android="http://schemas.android.com/apk/res/android"
  android:layout_width="fill_parent"
   android:orientation="vertical"
  android:layout_height="wrap_content">
 
  <Gallery xmlns:android="http://schemas.android.com/apk/res/android" android:id="@+id/gallery"
 android:layout_width="match_parent"
 android:layout_height="wrap_content"
/>

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="wrap_content">

    <Gallery android:id="@+id/gallery1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:gravity="center_vertical"
        android:spacing="16dp"
    />

</LinearLayout>

</LinearLayout>

 

 2.屬性檔案

<?xml version="1.0" encoding="utf-8"?>

<resources>

    <declare-styleable name="TogglePrefAttrs">
        <attr name="android:preferenceLayoutChild" />
    </declare-styleable>
   
    <!-- These are the attributes that we want to retrieve from the theme
         in view/Gallery1.java -->
    <declare-styleable name="Gallery1">
        <attr name="android:galleryItemBackground" />
    </declare-styleable>
   
     <declare-styleable name="LabelView">
        <attr name="text" format="string" />
        <attr name="textColor" format="color" />
        <attr name="textSize" format="dimension" />
    </declare-styleable>
</resources>

 

3.代碼

/**
 *
 */
package wjq.WidgetDemo;

import android.R.layout;
import android.app.Activity;
import android.content.Context;
import android.content.res.TypedArray;
import android.database.Cursor;
import android.os.Bundle;
import android.provider.Contacts.People;
import android.view.ContextMenu;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.view.ContextMenu.ContextMenuInfo;
import android.widget.BaseAdapter;
import android.widget.Gallery;
import android.widget.ImageView;
import android.widget.SimpleCursorAdapter;
import android.widget.SpinnerAdapter;
import android.widget.Toast;
import android.widget.AdapterView.AdapterContextMenuInfo;

/**
 * @author 記憶的永恒
 *
 */
public class GalleryDemo extends Activity {
 private Gallery gallery;
 private Gallery gallery1;

 /*
  * (non-Javadoc)
  *
  * @see android.app.Activity#onCreate(android.os.Bundle)
  */
 @Override
 protected void onCreate(Bundle savedInstanceState) {
  // TODO Auto-generated method stub
  super.onCreate(savedInstanceState);
  setContentView(R.layout.gallerypage);
  gallery = (Gallery) findViewById(R.id.gallery);
  gallery.setAdapter(new ImageAdapter(this));
  
  registerForContextMenu(gallery);
  
   Cursor c = getContentResolver().query(People.CONTENT_URI, null, null, null, null);
         startManagingCursor(c);
        
         SpinnerAdapter adapter = new SimpleCursorAdapter(this,
         // Use a template that displays a text view
                 android.R.layout.simple_gallery_item,
                 // Give the cursor to the list adatper
                 c,
                 // Map the NAME column in the people database to...
                 new String[] {People.NAME},
                 // The "text1" view defined in the XML template
                 new int[] { android.R.id.text1 });

         gallery1= (Gallery) findViewById(R.id.gallery1);
         gallery1.setAdapter(adapter);
 }

 @Override
 public void onCreateContextMenu(ContextMenu menu, View v,
   ContextMenuInfo menuInfo) {
  menu.add("Action");
 }

 @Override
 public boolean onContextItemSelected(MenuItem item) {
  AdapterContextMenuInfo info = (AdapterContextMenuInfo) item
    .getMenuInfo();
  Toast.makeText(this, "Longpress: " + info.position, Toast.LENGTH_SHORT)
    .show();
  return true;
 }
 
 public class ImageAdapter extends BaseAdapter {
  int mGalleryItemBackground;
  private Context mContext;

  private Integer[] mImageIds = { R.drawable.b, R.drawable.c,
    R.drawable.d, R.drawable.f, R.drawable.g };

  public ImageAdapter(Context context) {
   mContext = context;

   TypedArray a = obtainStyledAttributes(R.styleable.Gallery1);
   mGalleryItemBackground = a.getResourceId(
     R.styleable.Gallery1_android_galleryItemBackground, 0);
   a.recycle();
  }

  @Override
  public int getCount() {
   return mImageIds.length;
  }

  @Override
  public Object getItem(int position) {
   return position;
  }

  @Override
  public long getItemId(int position) {
   return position;
  }

  @Override
  public View getView(int position, View convertView, ViewGroup parent) {
   ImageView i = new ImageView(mContext);

   i.setImageResource(mImageIds[position]);
   i.setScaleType(ImageView.ScaleType.FIT_XY);
   i.setLayoutParams(new Gallery.LayoutParams(300, 400));

   // The preferred Gallery item background
   i.setBackgroundResource(mGalleryItemBackground);

   return i;
  }

 }

}

 

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.