`

ch011 Android Galley与ImageSwitch

阅读更多

--------------------------------------------Layout activity_main.xml--------------------------

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"

    xmlns:tools="http://schemas.android.com/tools"

    android:id="@+id/LinearLayout1"

    android:layout_width="match_parent"

    android:layout_height="match_parent"

    android:orientation="vertical" >

    <ImageSwitcher

        android:id="@+id/imageSwitcher"

        android:layout_width="fill_parent"

        android:layout_height="fill_parent"

        android:layout_weight="0.2" >

    </ImageSwitcher>

    <Gallery

        android:id="@+id/gallery"

        android:layout_width="fill_parent"

        android:layout_height="fill_parent"

        android:layout_weight="0.8" android:spacing="10sp"/>

</LinearLayout>

--------------------------------------------Layout gallery_item.xml---------------------------

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"

    xmlns:tools="http://schemas.android.com/tools"

    android:id="@+id/RelativeLayout1"

    android:layout_width="match_parent"

    android:layout_height="match_parent"

    android:orientation="vertical" >

    <ImageView

        android:id="@+id/image"

        android:layout_width="wrap_content"

        android:layout_height="wrap_content" android:src="@drawable/ic_launcher"/>

</RelativeLayout>

--------------------------------------------Adapter ImageAdapter.xml------------------------

package com.ch11;

import android.content.Context;

import android.view.LayoutInflater;

import android.view.View;

import android.view.ViewGroup;

import android.view.ViewGroup.LayoutParams;

import android.widget.BaseAdapter;

import android.widget.Gallery;

import android.widget.ImageView;

/**

 * 

 * 项目名称:com.ch11    

 * 类名称:ImageAdapter    

 * 类描述:  自定义适配器,为Gallery提供数据

 * 创建人:方勇   

 * 创建时间:2012-11-16 下午4:08:11   

 * Copyright (c) 方勇-版权所有

 */

public class ImageAdapter extends BaseAdapter {

/* 上下文对象 */

private Context mContext;

/* 布局管理对象 */

private LayoutInflater layoutInfalter;

/* 图片资源集合 */

public static Integer[] images = { R.drawable.a, R.drawable.b, R.drawable.c, R.drawable.d, R.drawable.e };

public ImageAdapter(Context context) {

super();

this.mContext = context;

layoutInfalter = LayoutInflater.from(mContext);

}

/* Gallery对应的图片个数 */

@Override

public int getCount() {

// TODO Auto-generated method stub

return images.length;

}

@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) {

ImageView imageView;

/* 实例化ImageView对象 */

if (null == convertView) {

/* 获取根视图对象 */

View view = layoutInfalter.inflate(R.layout.gallery_itemnull);

/* 图片对象 */

imageView = (ImageView) view.findViewById(R.id.image);

else {

imageView = (ImageView) convertView;

}

/* 图片源 */

imageView.setImageResource(images[position]);

/* Gallery图片布局类型 */

imageView.setLayoutParams(new Gallery.LayoutParams(100, LayoutParams.FILL_PARENT));

return imageView;

}

}

--------------------------------------------MainActivity.java-----------------------------------

package com.ch11;

import android.app.Activity;

import android.os.Bundle;

import android.util.Log;

import android.view.MotionEvent;

import android.view.View;

import android.view.View.OnTouchListener;

import android.view.ViewGroup.LayoutParams;

import android.widget.AdapterView;

import android.widget.AdapterView.OnItemSelectedListener;

import android.widget.Gallery;

import android.widget.ImageSwitcher;

import android.widget.ImageView;

import android.widget.ViewSwitcher.ViewFactory;

/**

 * 

 * 项目名称:com.ch11    

 * 类名称:MainActivity    

 * 类描述:  Galley,ImageSwitch,自定义适配器

 * 创建人:方勇   

 * 创建时间:2012-11-16 上午9:04:16   

 * Copyright (c) 方勇-版权所有

 */

public class MainActivity extends Activity implements OnItemSelectedListener, ViewFactory {

/* 相册控件 */

private Gallery gallery;

/* 图片切换控件 */

private ImageSwitcher imageSwitcher;

// 选中的图片索引

private int selectedTag = 0;

// 松开时的坐标x

private int upX;

// 按下时 的坐标x

private int downX;

@Override

public void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);

setContentView(R.layout.activity_main);

findViews();

setListeners();

init();

}

/* 初始化UI */

private void findViews() {

gallery = (Gallery) findViewById(R.id.gallery);

imageSwitcher = (ImageSwitcher) findViewById(R.id.imageSwitcher);

}

/* 监听事件 */

private void setListeners() {

gallery.setOnItemSelectedListener(this);

imageSwitcher.setOnTouchListener(onTouchListener);

}

/* 设置参数 */

private void init() {

/* 绑定图片适配器 */

ImageAdapter imageAdapter = new ImageAdapter(this);

gallery.setAdapter(imageAdapter);

/* Sets the factory used to create the two views between which the ViewSwitcher will flip */

/* 创建两个视图,手指滑动时,相互切换 */

imageSwitcher.setFactory(this);

}

/* 选中事件 */

@Override

public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {

imageSwitcher.setImageResource(ImageAdapter.images[position]);

selectedTag = position;

}

/* ImageSwitcher的触摸事件 */

private OnTouchListener onTouchListener = new OnTouchListener() {

@Override

public boolean onTouch(View v, MotionEvent event) {

if (event.getAction() == MotionEvent.ACTION_DOWN) {

downX = (int) event.getX(); // 取得按下时 的坐标x

return true;

else if (event.getAction() == MotionEvent.ACTION_UP) {

upX = (int) event.getX(); // 取得松开时的坐标x

}

if (upX - downX > 100) { // 从左拖到右,即看前一张

// 如果是第一,则去到尾部

if (gallery.getSelectedItemPosition() == 0)

selectedTag = gallery.getCount() - 1;

else

selectedTag = gallery.getSelectedItemPosition() - 1;

}

if (downX - upX > 100)// 从右拖到左,即看后一张

{

// 如果是最后,则去到第一

if (gallery.getSelectedItemPosition() == (gallery.getCount() - 1))

selectedTag = 0;

else

selectedTag = gallery.getSelectedItemPosition() + 1;

}

// Jump directly to a specific item in the adapter data.

// 直接跳到数据适配器中指定项。

// 改变gallery图片所选,自动触发ImageSwitcher的setOnItemSelectedListener

gallery.setSelection(selectedTagtrue);

return true;

}

};

@Override

public void onNothingSelected(AdapterView<?> parent) {

// TODO Auto-generated method stub

}

@Override

public View makeView() {

Log.v("fy""makeView start ...");

ImageView imageView = new ImageView(this);

// 伸缩比例与图片位置

imageView.setScaleType(ImageView.ScaleType.FIT_CENTER);

// 布局

imageView.setLayoutParams(new ImageSwitcher.LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT));

Log.v("fy""makeView end ...");

return imageView;

}

}

--------------------------------------------效果图-----------------------------------------------

<!--EndFragment-->
  • 大小: 113.3 KB
0
0
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics