访iPhone拖动相片特效

    你曾经被iPhone手机中用手指在屏幕上流畅拖动图片所吸引吗?在Android中通过Gallery就可以轻松做到。我们需要做的就是自定义一个适配器,作为Gallery控件里图片的源引用。还是先上图:

 

点击左右按钮或滑动图片区域可切换图片

 

 

清单文件:

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

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

    <application
        android:label="@string/app_name" >
        <activity
            android:label="@string/app_name"
            android:name=".GalleryActivity" >
            <intent-filter >
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>

</manifest>


按钮样式文件:

<?xml version="1.0" encoding="utf-8"?>
<!--定义按钮被点击,选中时的背景 -->
<selector
  xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:state_enabled="true" android:state_pressed="true" android:drawable="@drawable/main_btn_bg" />
    <item android:state_enabled="true" android:state_checked="true" android:drawable="@drawable/main_btn_bg" />
    <item android:drawable="@drawable/btn_left"/>
    <item android:state_focused="true" android:state_enabled="true" android:state_pressed="false" android:drawable="@drawable/btn_left" />
</selector>

<?xml version="1.0" encoding="utf-8"?>
<!--定义按钮被点击,选中时的背景 -->
<selector
  xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:state_enabled="true" android:state_pressed="true" android:drawable="@drawable/main_btn_bg" />
    <item android:state_enabled="true" android:state_checked="true" android:drawable="@drawable/main_btn_bg" />
    <item android:drawable="@drawable/btn_right"/>
    <item android:state_focused="true" android:state_enabled="true" android:state_pressed="false" android:drawable="@drawable/btn_right" />
</selector>


布局文件:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:background="#104E8B"
    android:orientation="vertical" >

 <LinearLayout android:orientation="horizontal"
               android:layout_width="fill_parent"
               android:layout_height="wrap_content">

    <Button    android:id="@+id/btn_left"
               android:background="@drawable/left_btn_bg_d"
               android:layout_width="40dip"
               android:layout_height="80dip"/>

    <Gallery   android:id="@+id/gallery"
			   android:layout_width="wrap_content"
			   android:layout_height="wrap_content"
			   android:background="@drawable/gallery_bg"
			   android:spacing="10dip"
			   android:layout_weight = "1"
	/>
	<Button    android:id="@+id/btn_right"
	           android:background="@drawable/right_btn_bg_d"
	           android:layout_width="40dip"
	           android:layout_height="80dip"/>
</LinearLayout>

</LinearLayout>


GalleryActivity.java文件:

package com.focus.gallery;

import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.Gallery;
import android.widget.ImageView;

public class GalleryActivity extends Activity implements OnClickListener {

	private Gallery gallery;

	private Button leftButton;
	private Button rightButton;

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);

		setContentView(R.layout.main);

		init();

	}

	public void init() {

		gallery = (Gallery) this.findViewById(R.id.gallery);
		gallery.setAdapter(new ImageAdapter(this));

		leftButton = (Button) this.findViewById(R.id.btn_left);
		rightButton = (Button) this.findViewById(R.id.btn_right);

		leftButton.setOnClickListener(this);
		rightButton.setOnClickListener(this);

	}

	@Override
	public void onClick(View v) {
		if (v == leftButton) {
			if (gallery.getSelectedItemPosition() == new GalleryActivity.ImageAdapter(
					this).imgSource.length - 1) {
				gallery.setSelection(0);
			}
			gallery.setSelection(gallery.getSelectedItemPosition() + 1);

		} else if (v == rightButton) {
			if (gallery.getSelectedItemPosition() == 0) {
				gallery.setSelection(new GalleryActivity.ImageAdapter(this).imgSource.length - 1);
			}
			gallery.setSelection(gallery.getSelectedItemPosition() - 1);
		}

	}

	// 存放图片的容器,用来存放Gallery所需要的图片
	public class ImageAdapter extends BaseAdapter {

		private Context mcontext;

		private int[] imgSource = { R.drawable.p1, R.drawable.p2,
				R.drawable.p3, R.drawable.p4, R.drawable.p5, R.drawable.p6,
				R.drawable.p7, R.drawable.p8 };

		public ImageAdapter(Context c) {

			this.mcontext = c;

		}

		@Override
		public int getCount() {
			return imgSource == null ? 0 : imgSource.length;
		}

		@Override
		public Object getItem(int position) {
			return imgSource == null ? null : imgSource[position];
		}

		@Override
		public long getItemId(int position) {
			return imgSource == null ? 0 : position;
		}

		@Override
		public View getView(int position, View convertView, ViewGroup parent) {

			ImageView imageView = new ImageView(this.mcontext);
			imageView.setImageResource(imgSource[position]);
			imageView.setScaleType(ImageView.ScaleType.FIT_XY);
			imageView.setLayoutParams(new Gallery.LayoutParams(120, 120));
			return imageView;
		}
	}

}


好了,比较简单,不多废话,今天到此为止。

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值