现在的位置: 首页 > 综合 > 正文

Android之Gallery

2018年02月05日 ⁄ 综合 ⁄ 共 3794字 ⁄ 字号 评论关闭

一. 初步认识

src

package cn.android.lyj;

import android.app.Activity;
import android.content.Context;
import android.content.res.TypedArray;
import android.os.Bundle;
import android.view.ContextMenu;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.view.ContextMenu.ContextMenuInfo;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.Gallery;
import android.widget.ImageView;
import android.widget.Toast;
import android.widget.AdapterView.AdapterContextMenuInfo;
import android.widget.AdapterView.OnItemClickListener;

public class Activity_01 extends Activity {

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.main);

		// Reference the Gallery view
		Gallery g = (Gallery) findViewById(R.id.gallery);
		// Set the adapter to our custom adapter (below)
		g.setAdapter(new ImageAdapter(this));

		// Set a item click listener, and just Toast the clicked position
		g.setOnItemClickListener(new OnItemClickListener() {
			
			public void onItemClick(AdapterView parent, View v, int position, long id) {
				Toast.makeText(Activity_01.this, "" + position,
						Toast.LENGTH_SHORT).show();
			}
		});

		// We also want to show context menu for longpressed items in the
		// gallery
		registerForContextMenu(g);
	}

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

	@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;

		public ImageAdapter(Context c) {
			mContext = c;
			TypedArray a = obtainStyledAttributes(R.styleable.Gallery1);
			mGalleryItemBackground = a.getResourceId(R.styleable.Gallery1_android_galleryItemBackground, 0);
			a.recycle();
		}

		public int getCount() {
			return mImageIds.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 i = new ImageView(mContext);

			i.setImageResource(mImageIds[position]);
			i.setScaleType(ImageView.ScaleType.FIT_XY); //图片在北京图片中得位置
			i.setLayoutParams(new Gallery.LayoutParams(136, 88));  //修改存放此图片的view的大小比例

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

			return i;
		}

		private Context mContext;

		private Integer[] mImageIds = { R.drawable.gallery_photo_1,
				R.drawable.gallery_photo_2, R.drawable.gallery_photo_3,
				R.drawable.gallery_photo_4, R.drawable.gallery_photo_5,
				R.drawable.gallery_photo_6, R.drawable.gallery_photo_7,
				R.drawable.gallery_photo_8 };
	}

}

main.xml

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

<Gallery xmlns:android="http://schemas.android.com/apk/res/android" 
    android:id="@+id/gallery"
	android:layout_width="match_parent"
	android:layout_height="wrap_content"
/>
       

attrs.xml

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

    <!--
         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>

</resources>

strings.xml

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

    <string name="gallery_2_text">Testing</string>
    <string name="app_name">Gallery</string>

</resources>

AndroidManifest.xml

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="cn.android.lyj"
    android:versionCode="1"
    android:versionName="1.0" >

    <uses-sdk android:minSdkVersion="10" />

    <application
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name" >
        <activity
            android:label="@string/app_name"
            android:name=".Activity_01" >
            <intent-filter >
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>

</manifest>

二. 运行结果

运行后


点击第一幅图片后


长按出现上下文菜单


点击其中一项后



【上篇】
【下篇】

抱歉!评论已关闭.