OnTouch Events in Android events

Source: Internet
Author: User
Tags gety

OnTouch events in Android events:

How to Implement:

1 monitoring

2 Back Adjustment


1 Monitoring:

Package Com.example.conflicttest;import Android.os.bundle;import Android.app.activity;import android.util.Log; Import Android.view.menu;import Android.view.motionevent;import Android.view.view;import Android.view.view.ontouchlistener;import Android.widget.imageview;import Android.widget.linearlayout;public Class Mainactivity extends Activity implements Ontouchlistener {private linearlayout sublayout;private ImageView imgview;/* ( Non-javadoc) * @see android.app.activity#oncreate (android.os.Bundle) */@Overrideprotected void OnCreate (Bundle Savedinstancestate) {super.oncreate (savedinstancestate); Setcontentview (r.layout.activity_main); subLayout = ( LinearLayout) Findviewbyid (r.id.sublayout); Imgview = (ImageView) Findviewbyid (R.id.imgview);// Sublayout.setontouchlistener (this);/** * Event Monitoring the first scenario: Set up listening! * */imgview.setontouchlistener (this);} @Overridepublic boolean Oncreateoptionsmenu (Menu menu) {//Inflate the menu; This adds items to the action bar if it is PR Esent.getmenuinflater (). Inflate (r.mEnu.main, menu); return true;}  /** * Return False event return call, passed to parent class! * */@Overridepublic Boolean OnTouch (View V, motionevent event) {//TODO auto-generated method Stubint action = Event.geta Ction (); Float rawx = event.getrawx (); Float Rawy = Event.getrawy (); float x = Event.getx (); Float y = event.gety (); LOG.D ("Lvoe", "----on touch:" + V + "raw[" + rawx + "," + Rawy + "],xy[" + x+ "," + y + "]"); return true;}

2 Callback ":

Package Com.example.conflicttest;import Android.content.context;import Android.util.attributeset;import Android.util.log;import android.view.motionevent;import android.widget.linearlayout;/** * @author Hades * * */public Class Customlinerlayout extends LinearLayout {public customlinerlayout (context context) {super (context);//TODO Auto-generated Constructor Stub}public customlinerlayout (context context, AttributeSet Attrs) {Super (context, attrs); /TODO auto-generated Constructor Stub}public customlinerlayout (context context, AttributeSet attrs, int defstyle) {Super (Context, attrs, defstyle);//TODO auto-generated Constructor stub}/** * * Second Implementation way: * * Custom view class: * * Handle touch events, you can use OnT Ouch () * * * * @Overridepublic boolean ontouchevent (Motionevent event) {//TODO auto-generated method Stubfloat rawx = even T.GETRAWX (); Float Rawy = Event.getrawy (); float x = Event.getx (); Float y = event.gety (); LOG.D ("Lvoe", "----on Touch:" + "raw x y = [" + Rawx + "," + Rawy + "],x,y=[" + x+ "," + y + "]"); return super.ontouchevent (event);}} 



OnTouch Events in Android events

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.