想要的效果是:通过单击屏幕,将Gallery控件显示或隐藏,现在的问题是,由于有了ImageSwitcher触摸屏幕切换图片的功能,单击屏幕的事件不知道如何去获取,如果将imageSwitcher.setOnTouchListener(this)语句注释掉,单击屏幕的事件就可以了。
可运行的代码如下,图片不好上传哦,麻烦大家自己添加图片,帮忙调试一下,谢谢啦:
import android.app.Activity;
import android.content.Context;
import android.content.res.TypedArray;
import android.os.Bundle;
import android.util.Log;
import android.view.GestureDetector;
import android.view.GestureDetector.SimpleOnGestureListener;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnTouchListener;
import android.view.ViewGroup;
import android.view.animation.AnimationUtils;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemSelectedListener;
import android.widget.Gallery.LayoutParams;
import android.widget.BaseAdapter;
import android.widget.Gallery;
import android.widget.ImageSwitcher;
import android.widget.ImageView;
import android.widget.ViewSwitcher.ViewFactory;
/**
* ImageSwitcherActivity.java
* @author cloay
* 2011-7-16
*/
public class Album extends Activity implements OnItemSelectedListener, ViewFactory, OnTouchListener {
private ImageSwitcher imageSwitcher;
private Gallery gallery;
private int selectedTag = 0;
private int downX, upX;
private GestureDetector gestureScanner;
private Integer [] imagesId = new Integer[]{R.drawable.b, R.drawable.c, R.drawable.d,
R.drawable.f, R.drawable.g};
private Integer [] selectId = new Integer[]{R.drawable.b, R.drawable.c, R.drawable.d,
R.drawable.f, R.drawable.g};
private static final String TAG = "Album";
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
imageSwitcher = (ImageSwitcher)findViewById(R.id.switcher);
imageSwitcher.setFactory(this);
//设置图片切换时的动画效果
imageSwitcher.setInAnimation(AnimationUtils.loadAnimation(this, android.R.anim.fade_in));
imageSwitcher.setOutAnimation(AnimationUtils.loadAnimation(this, android.R.anim.fade_out));
imageSwitcher.setOnTouchListener(this);
gallery = (Gallery)findViewById(R.id.gallery);
//自定义ImageAdapter继承于BaseAdapter,是一个内部类
gallery.setAdapter(new ImageAdapter(this));
gallery.setOnItemSelectedListener(this);
gestureScanner = new GestureDetector(new SimpleOnGestureListener()
{
@Override
public boolean onSingleTapConfirmed(MotionEvent e)
{
Log.i(TAG, "click");
return false;
}
});
}
@Override
public View makeView() {
ImageView image = new ImageView(this);
image.setScaleType(ImageView.ScaleType.FIT_CENTER);
image.setLayoutParams(new ImageSwitcher.LayoutParams(
LayoutParams.FILL_PARENT, LayoutParams.MATCH_PARENT));
return image;
}
@Override
public void onItemSelected(AdapterView<?> arg0, View arg1, int arg2,
long arg3) {
selectedTag = arg2;
imageSwitcher.setImageResource(imagesId[arg2]);
}
@Override
public void onNothingSelected(AdapterView<?> arg0) {
}
public class ImageAdapter extends BaseAdapter{
private Context context;
int galleryItemBackground;
public ImageAdapter (Context c){
context = c;
TypedArray typeArray = obtainStyledAttributes(R.styleable.Gallery);
galleryItemBackground = typeArray.getResourceId(R.styleable.Gallery_android_galleryItemBackground, 0);
typeArray.recycle();
}
@Override
public int getCount() {
//返回selectId[]的长度
return selectId.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 imageView = new ImageView(context);
//设置资源图片
imageView.setImageResource(selectId[position]);
imageView.setAdjustViewBounds(true); //允许调整边框
//设定底部画廊,自适应大小
imageView.setLayoutParams(new Gallery.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
//设置画廊背景
imageView.setBackgroundResource(galleryItemBackground);
return imageView;
}
}
@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;
imageSwitcher.setInAnimation(AnimationUtils.loadAnimation(this,
R.anim.push_right_in));
imageSwitcher.setOutAnimation(AnimationUtils.loadAnimation(this,
R.anim.push_right_out));
}
else if (downX - upX > 100)
{
// 从右拖到左,即看后一张
// 如果是最后,则去到第一
if (gallery.getSelectedItemPosition()
== (gallery.getCount() - 1))
selectedTag = 0;
else
selectedTag = gallery.getSelectedItemPosition() + 1;
imageSwitcher.setInAnimation(AnimationUtils.loadAnimation(this,
R.anim.push_left_in));
imageSwitcher.setOutAnimation(AnimationUtils.loadAnimation(this,
R.anim.push_left_out));
}
// 改变gallery图片所选,自动触发ImageSwitcher的setOnItemSelectedListener
gallery.setSelection(selectedTag, true);
return true;
}
return false;
}
@Override
public boolean onTouchEvent(MotionEvent event) {
return gestureScanner.onTouchEvent(event);
}
}
res\layout\main.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<ImageSwitcher android:id="@+id/switcher"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"/>
<Gallery android:id="@+id/gallery"
android:layout_width="fill_parent"
android:layout_alignParentLeft="true"
android:layout_height="60dp"
android:spacing="15dp"
android:layout_alignParentBottom="true"
android:gravity="center_vertical"
android:visibility="invisible"/>
</RelativeLayout>
res\values\attrs.xml
<?xml version="1.0" encoding="utf-8"?>
<resources>
<declare-styleable name="Gallery">
<attr name="android:galleryItemBackground" />
</declare-styleable>
</resources>
res\anim\push_left_in.xml
<?xml version="1.0" encoding="utf-8"?>
<!-- 左进渐变效果 -->
<set xmlns:android="http://schemas.android.com/apk/res/android" >
<translate
android:duration="1500"
android:fromXDelta="100%p"
android:toXDelta="0" />
<alpha
android:duration="1500"
android:fromAlpha="0.1"
android:toAlpha="1.0" />
</set>
res\anim\push_left_out.xml
<?xml version="1.0" encoding="utf-8"?>
<!-- 右出渐变效果 -->
<set xmlns:android="http://schemas.android.com/apk/res/android" >
<translate
android:duration="1500"
android:fromXDelta="0"
android:toXDelta="-100%p" />
<alpha
android:duration="1500"
android:fromAlpha="1.0"
android:toAlpha="0.1" />
</set>
res\anim\push_right_in.xml
<?xml version="1.0" encoding="utf-8"?>
<!-- 右进渐变效果 -->
<set xmlns:android="http://schemas.android.com/apk/res/android" >
<translate
android:duration="1500"
android:fromXDelta="-100%p"
android:toXDelta="0" />
<alpha
android:duration="1500"
android:fromAlpha="0.1"
android:toAlpha="1.0" />
</set>
res\anim\push_right_out.xml
<?xml version="1.0" encoding="utf-8"?>
<!-- 右出渐变效果 -->
<set xmlns:android="http://schemas.android.com/apk/res/android" >
<translate
android:duration="1500"
android:fromXDelta="0"
android:toXDelta="100%p" />
<alpha
android:duration="1500"
android:fromAlpha="1.0"
android:toAlpha="0.1" />
</set>
11 个解决方案
#1
OnClickListener是单击事件的监听接口
#2
把onTouch里面的return true;改为return false;
@Override
public boolean onTouch(View v, MotionEvent event) {
if (event.getAction() == MotionEvent.ACTION_DOWN)
{
downX = (int)event.getX(); // 取得按下时的坐标x
return false;
}
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;
imageSwitcher.setInAnimation(AnimationUtils.loadAnimation(this,
R.anim.push_right_in));
imageSwitcher.setOutAnimation(AnimationUtils.loadAnimation(this,
R.anim.push_right_out));
}
else if (downX - upX > 100)
{
// 从右拖到左,即看后一张
// 如果是最后,则去到第一
if (gallery.getSelectedItemPosition()
== (gallery.getCount() - 1))
selectedTag = 0;
else
selectedTag = gallery.getSelectedItemPosition() + 1;
imageSwitcher.setInAnimation(AnimationUtils.loadAnimation(this,
R.anim.push_left_in));
imageSwitcher.setOutAnimation(AnimationUtils.loadAnimation(this,
R.anim.push_left_out));
}
// 改变gallery图片所选,自动触发ImageSwitcher的setOnItemSelectedListener
gallery.setSelection(selectedTag, true);
return false;
}
return false;
}
#3
if (upX - downX > 100)
这些东西你都不加括号的啊
if ((upX - downX) > 100)
这些东西你都不加括号的啊
if ((upX - downX) > 100)
#4
- 符号的优先级高还是 > 的优先级高。
再说你回答的与我问的问题没有任何关联。呵呵!
#5
这个能用来单击屏幕吗?
#6
谢谢,这个我试过,没用,使得触摸与单击都失效。
你有这方面的 Demo 吗?
#7
还以为你已经加了隐藏显示gallery的代码!
将下面代码加到你原来代码:
upX = (int)event.getX(); // 取得松开时的坐标x;
的前面
if(gallery.getVisibility() == 4)
{
gallery.setVisibility(0);
}
else
{
gallery.setVisibility(4);
}
#8
以下是滑动时不处理。
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;
imageSwitcher.setInAnimation(AnimationUtils.loadAnimation(this,
R.anim.push_right_in));
imageSwitcher.setOutAnimation(AnimationUtils.loadAnimation(this,
R.anim.push_right_out));
}
else if (downX - upX > 100)
{
// 从右拖到左,即看后一张
// 如果是最后,则去到第一
if (gallery.getSelectedItemPosition()
== (gallery.getCount() - 1))
selectedTag = 0;
else
selectedTag = gallery.getSelectedItemPosition() + 1;
imageSwitcher.setInAnimation(AnimationUtils.loadAnimation(this,
R.anim.push_left_in));
imageSwitcher.setOutAnimation(AnimationUtils.loadAnimation(this,
R.anim.push_left_out));
}
else
{
if(gallery.getVisibility() == 4)
{
gallery.setVisibility(0);
}
else
{
gallery.setVisibility(4);
}
}
// 改变gallery图片所选,自动触发ImageSwitcher的setOnItemSelectedListener
gallery.setSelection(selectedTag, true);
return true;
}
#9
感谢你的无私奉献啊!方法奏效,刚上班就解决了一个问题,呵呵。
#10
正在试
#11
- 符号的优先级高还是 > 的优先级高。
再说你回答的与我问的问题没有任何关联。呵呵!不太喜欢 呵呵 两字!
#1
OnClickListener是单击事件的监听接口
#2
把onTouch里面的return true;改为return false;
@Override
public boolean onTouch(View v, MotionEvent event) {
if (event.getAction() == MotionEvent.ACTION_DOWN)
{
downX = (int)event.getX(); // 取得按下时的坐标x
return false;
}
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;
imageSwitcher.setInAnimation(AnimationUtils.loadAnimation(this,
R.anim.push_right_in));
imageSwitcher.setOutAnimation(AnimationUtils.loadAnimation(this,
R.anim.push_right_out));
}
else if (downX - upX > 100)
{
// 从右拖到左,即看后一张
// 如果是最后,则去到第一
if (gallery.getSelectedItemPosition()
== (gallery.getCount() - 1))
selectedTag = 0;
else
selectedTag = gallery.getSelectedItemPosition() + 1;
imageSwitcher.setInAnimation(AnimationUtils.loadAnimation(this,
R.anim.push_left_in));
imageSwitcher.setOutAnimation(AnimationUtils.loadAnimation(this,
R.anim.push_left_out));
}
// 改变gallery图片所选,自动触发ImageSwitcher的setOnItemSelectedListener
gallery.setSelection(selectedTag, true);
return false;
}
return false;
}
#3
if (upX - downX > 100)
这些东西你都不加括号的啊
if ((upX - downX) > 100)
这些东西你都不加括号的啊
if ((upX - downX) > 100)
#4
- 符号的优先级高还是 > 的优先级高。
再说你回答的与我问的问题没有任何关联。呵呵!
#5
这个能用来单击屏幕吗?
#6
谢谢,这个我试过,没用,使得触摸与单击都失效。
你有这方面的 Demo 吗?
#7
还以为你已经加了隐藏显示gallery的代码!
将下面代码加到你原来代码:
upX = (int)event.getX(); // 取得松开时的坐标x;
的前面
if(gallery.getVisibility() == 4)
{
gallery.setVisibility(0);
}
else
{
gallery.setVisibility(4);
}
#8
以下是滑动时不处理。
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;
imageSwitcher.setInAnimation(AnimationUtils.loadAnimation(this,
R.anim.push_right_in));
imageSwitcher.setOutAnimation(AnimationUtils.loadAnimation(this,
R.anim.push_right_out));
}
else if (downX - upX > 100)
{
// 从右拖到左,即看后一张
// 如果是最后,则去到第一
if (gallery.getSelectedItemPosition()
== (gallery.getCount() - 1))
selectedTag = 0;
else
selectedTag = gallery.getSelectedItemPosition() + 1;
imageSwitcher.setInAnimation(AnimationUtils.loadAnimation(this,
R.anim.push_left_in));
imageSwitcher.setOutAnimation(AnimationUtils.loadAnimation(this,
R.anim.push_left_out));
}
else
{
if(gallery.getVisibility() == 4)
{
gallery.setVisibility(0);
}
else
{
gallery.setVisibility(4);
}
}
// 改变gallery图片所选,自动触发ImageSwitcher的setOnItemSelectedListener
gallery.setSelection(selectedTag, true);
return true;
}
#9
感谢你的无私奉献啊!方法奏效,刚上班就解决了一个问题,呵呵。
#10
正在试
#11
- 符号的优先级高还是 > 的优先级高。
再说你回答的与我问的问题没有任何关联。呵呵!不太喜欢 呵呵 两字!