Android--手势触控屏幕控制/GestureDetector

实现本实例需要注意几个地方:

1.隐藏状态栏

 /* 隐藏状态栏 */
    this.getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
                              WindowManager.LayoutParams.FLAG_FULLSCREEN);

2.隐藏标题栏

/* 隐藏标题栏 */
    requestWindowFeature(Window.FEATURE_NO_TITLE);

这个例子实现的是手势拖动效果展示。

下面给出本实例的截图:

1.初步运行


我们看到这张图片的分辨率很高,我们不能看到这幅图片的全景,这时我们需要实现拖拉效果看到图片的全景,当我们实现了手势之后,运行的效果如下:


下面给出本实例的代码:

package irdc.ex07_19;

import android.app.Activity;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Bundle;
import android.view.GestureDetector;
import android.view.MotionEvent;
import android.view.Window;
import android.view.WindowManager;
import android.widget.ImageView;

public class EX07_19 extends Activity
{
  private ImageView image1;
  private Bitmap bm;
  private int bmWidth=0;
  private int bmHeight=0;
  private int width=0;
  private int height=0;
  private int pointX=0;
  private int pointY=0;
  private GestureDetector detector;
  private myGestureListener gListener;
  
  /** Called when the activity is first created. */
  @Override
  public void onCreate(Bundle savedInstanceState)
  {
    super.onCreate(savedInstanceState);
    /* 隐藏状态栏 */
    this.getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
                              WindowManager.LayoutParams.FLAG_FULLSCREEN);
    /* 隐藏标题栏 */
    requestWindowFeature(Window.FEATURE_NO_TITLE);
    /* 载丈main.xml Layout */
    setContentView(R.layout.main);
    /* 取得屏幕宽高 */    
    width=this.getWindowManager().getDefaultDisplay().getWidth();
    height=this.getWindowManager().getDefaultDisplay().getHeight();   
    /* Bitmap设定 */
    bm=BitmapFactory.decodeResource(getResources(),R.drawable.photo);
    bmWidth=bm.getWidth();
    bmHeight=bm.getHeight();
    /* ImageView初始化 */
    image1=(ImageView)findViewById(R.id.image1);
    Bitmap newB=Bitmap.createBitmap(bm,pointX,pointY, width, height);
    image1.setImageBitmap(newB);
    /* GestureDetector设置 */
    gListener = new myGestureListener();
    detector = new GestureDetector(EX07_19.this,gListener);
  }
  /* 当Activity的onTouchEvent()被触发时,
   * 触发GestureDetector的onTouchEvent() */
  @Override
  public boolean onTouchEvent(MotionEvent event)
  {
    if (detector.onTouchEvent(event))
    {
      return detector.onTouchEvent(event);
    }  
    else
    {
      return super.onTouchEvent(event);
    }
  }
  
  /* 自定义GestureListener类 */
  public class myGestureListener implements GestureDetector.OnGestureListener
  {
    /* 手指在屏幕上拖拉时触发此method */
    @Override
    public boolean onScroll(MotionEvent e1, MotionEvent e2,
                            float distanceX,  float distanceY)
    {
      /* 计算X轴基准点移动后的吵置 */
      if(pointX+distanceX>=0){
        if((pointX+distanceX)>(bmWidth-width)){  
          pointX=bmWidth-width;
        }else{
          pointX+=distanceX;
        }
      }else{
        pointX=0;
      }
      /* 计算Y轴基准点移动后的吵置 */
      if(pointY+distanceY>=0){  
        if((pointY+distanceY)>(bmHeight-height)){  
          pointY=bmHeight-height;
        }else{
          pointY+=distanceY;
        }
      }else{
        pointY=0;
      }
      /* 如果有移动,则更新Bitmap设定 */
      if(distanceX!=0&&distanceY!=0)
      {
        Bitmap newB=Bitmap.createBitmap(bm,pointX,pointY,width,height);
        image1.setImageBitmap(newB);
      }
      return false;
    }

    @Override
    public boolean onDown(MotionEvent arg0)
    {
      return false;
    }

    @Override
    public boolean onFling(MotionEvent e1, MotionEvent e2,
        float velocityX, float velocityY)
    {
      return false;
    }

    @Override
    public void onLongPress(MotionEvent e)
    {
    }

    @Override
    public void onShowPress(MotionEvent e)
    {
    }

    @Override
    public boolean onSingleTapUp(MotionEvent e)
    {
      return false;
    }
  }
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值