标签:
本节目的:用android手势事件做一个图片浏览器。
简述:可能一见到图片浏览器,大家脑海里首先出现的是ViewPager吧。但其实用GestureDetector也可以实现,
优点是:
1.简单。
2.消耗内存少。经过笔者测试,这种方式在24张图片下依然不会出现OOM异常。而ViewPager呢,笔者手机里放7张就不行了。
(这种方法其实替换掉了原来的图片,本身就只会有一张图片在内存)
3.图片可以随意拉伸
缺点是:
没有滑动的动画效果。
至于以后选择哪种方式实现,读者自行选择。
GestureDetector的详细用法参见:
http://blog.csdn.net/getchance/article/details/8478993
步骤:
1.视图文件中:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
>
<ImageView
android:id="@+id/imageview"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
/>
</RelativeLayout>
2.控制文件中:
package com.example.imagebrowser;
import android.app.Activity;
import android.content.res.Resources;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.view.GestureDetector;
import android.view.MotionEvent;
import android.widget.ImageView;
public class MainActivity extends Activity {
//Data field
private int[] mImageId;
private ImageView mImageView;
private int mCurrentPosition=0;
final int mRightFlag = 0;
final int mLeftFlag = 1;
private GestureDetector gestureDetector;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
initComponent();
setImage();
processGestureDetector();
}
//The method is used to init UI components
private void initComponent(){
mImageView=(ImageView)findViewById(R.id.imageview);
}
//The method is used to set image
private void setImage(){
mImageId=new int[]{R.drawable.pic1,R.drawable.pic2,R.drawable.pic3,R.drawable.pic4,
R.drawable.pic5,R.drawable.pic6,R.drawable.pic7,R.drawable.pic8};
Resources resources = getBaseContext().getResources();
Drawable imageDrawable = resources.getDrawable(mImageId[mCurrentPosition]);
mImageView.setBackgroundDrawable(imageDrawable);
}
//The method is used to process GestureDetector
private void processGestureDetector(){
GestureDetector.OnGestureListener onGestureListener =new GestureDetector.SimpleOnGestureListener() {//实例化监听器
@Override
public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX,
float velocityY) {
float x = e2.getX() - e1.getX();
float y = e2.getY() - e1.getY();
if (x > 0) {
doResult(mRightFlag);
} else if (x < 0) {
doResult(mLeftFlag);
}
return true;
}
};
gestureDetector = new GestureDetector(MainActivity.this,onGestureListener);//用此监听器生成GestureDetector实例
}
//The method override onTouchEvent method
public boolean onTouchEvent(MotionEvent event) {
return gestureDetector.onTouchEvent(event);
}
//The method to handle switch behavior :switch image
public void doResult(int action) {
switch (action) {
case mRightFlag:
mCurrentPosition=(mCurrentPosition+mImageId.length+1)%mImageId.length;
setImage();
break;
case mLeftFlag:
mCurrentPosition=(mCurrentPosition+mImageId.length-1)%mImageId.length;
setImage();
break;
}
}
}
版权声明:本文为博主原创文章,未经博主允许不得转载。
标签:
原文地址:http://blog.csdn.net/qq_16912257/article/details/48013851