(Android) 单击屏幕事件和滑动屏幕事件共存的解决方案
实现功能:布局文件中有两个控件,分别是ImageSwitcher和Gallery控件,Gallery默认的情况下是不可见的。当进入程序的界面时,如果滑动屏幕,会切换到下一张或者前一张图片,根据你滑动屏幕的方向来判断,当单击屏幕时,会显示Gallery控件,再单击屏幕时,会隐藏Gallery控件,这个问题不是我自己解决的,通过论坛提问,AMinfo帮我解决的,感谢他。
心得:我解决这个问题的方案是:要实现两个易做图,分别是:OnTouchListener和OnGestureListener,怎么尝试都无法区分单击屏幕和滑动屏幕动作。AMinfo的解决方案是在OnTouchListener的重写onTouch方法中添加如下的代码,如果我在一个人这样做下去,我都不知道什么时候完成这么一个小功能呢?让我明白需要大家帮助的时候,不可一意孤行,不但浪费时间,而且伤身啊,当然,自己也要先想想怎么去解决这个问题喽。
[java]
// 数字4和数字0分别表示的是不可见和可见,
// 也可以使用View.INVISIBLE和View.VISIBLE来代替数字.
else
{
if (gallery.getVisibility() == 4)
gallery.setVisibility(0);
else
gallery.setVisibility(4);
}
下面,贴上完整的小应用代码:
Album.java,项目中要用到的图片自己提供哦
[java]
package com.treasure.ui;
import android.app.Activity;
import android.content.Context;
import android.content.res.TypedArray;
import android.os.Bundle;
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;
/**
* 实现相册浏览功能
*/
public class Album extends Activity implements OnItemSelectedListener, ViewFactory, OnTouchListener
{
private ImageSwitcher imageSwitcher;
private Gallery gallery;
private int selectedTag = 0;
private int downX, upX;
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};
@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);
}
@Override
public View makeView()
{
ImageView image = new ImageView(this);
image.setScaleType(ImageView.ScaleType.FIT_XY);
image.setLayoutParams(new ImageSwitcher.LayoutParams(
LayoutParams.FILL_PARENT, LayoutParams.FILL_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)
{
&nb
补充:移动开发 , Android ,