forked from daleige/AndroidSamples
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
6 changed files
with
200 additions
and
13 deletions.
There are no files selected for viewing
38 changes: 36 additions & 2 deletions
38
TouchEventDemo/app/src/main/java/com/cyq/toucheventdemo/MainActivity.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,14 +1,48 @@ | ||
package com.cyq.toucheventdemo; | ||
|
||
import androidx.appcompat.app.AppCompatActivity; | ||
|
||
import android.os.Bundle; | ||
import android.view.MotionEvent; | ||
import android.view.View; | ||
|
||
import androidx.appcompat.app.AppCompatActivity; | ||
|
||
public class MainActivity extends AppCompatActivity { | ||
private static final String Log = "TouchEventDemo"; | ||
private MyView myView; | ||
|
||
@Override | ||
protected void onCreate(Bundle savedInstanceState) { | ||
super.onCreate(savedInstanceState); | ||
setContentView(R.layout.activity_main); | ||
myView = findViewById(R.id.my_view); | ||
myView.setOnTouchListener(new View.OnTouchListener() { | ||
@Override | ||
public boolean onTouch(View view, MotionEvent motionEvent) { | ||
switch (motionEvent.getAction()) { | ||
case MotionEvent.ACTION_DOWN: | ||
android.util.Log.d(Log, "my_view:手指按下"); | ||
break; | ||
case MotionEvent.ACTION_MOVE: | ||
android.util.Log.d(Log, "my_view:手指移动"); | ||
break; | ||
case MotionEvent.ACTION_UP: | ||
android.util.Log.d(Log, "my_view:手指抬起"); | ||
break; | ||
} | ||
return false; | ||
} | ||
}); | ||
} | ||
|
||
@Override | ||
public boolean dispatchTouchEvent(MotionEvent ev) { | ||
android.util.Log.d(Log, "MainActivity:dispatchTouchEvent"); | ||
return super.dispatchTouchEvent(ev); | ||
} | ||
|
||
@Override | ||
public boolean onTouchEvent(MotionEvent event) { | ||
android.util.Log.d(Log, "MainActivity:onTouchEvent"); | ||
return super.onTouchEvent(event); | ||
} | ||
} |
24 changes: 24 additions & 0 deletions
24
TouchEventDemo/app/src/main/java/com/cyq/toucheventdemo/MyButton.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
package com.cyq.toucheventdemo; | ||
|
||
import android.content.Context; | ||
import android.util.AttributeSet; | ||
import android.widget.Button; | ||
|
||
/** | ||
* Create by 陈扬齐 | ||
* Create on 2019-09-03 | ||
* description: | ||
*/ | ||
public class MyButton extends Button { | ||
public MyButton(Context context) { | ||
super(context); | ||
} | ||
|
||
public MyButton(Context context, AttributeSet attrs) { | ||
super(context, attrs); | ||
} | ||
|
||
public MyButton(Context context, AttributeSet attrs, int defStyleAttr) { | ||
super(context, attrs, defStyleAttr); | ||
} | ||
} |
31 changes: 31 additions & 0 deletions
31
TouchEventDemo/app/src/main/java/com/cyq/toucheventdemo/MyScrollView.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
package com.cyq.toucheventdemo; | ||
|
||
import android.content.Context; | ||
import android.util.AttributeSet; | ||
import android.view.MotionEvent; | ||
import android.widget.ScrollView; | ||
|
||
/** | ||
* Create by 陈扬齐 | ||
* Create on 2019-09-03 | ||
* description: | ||
*/ | ||
public class MyScrollView extends ScrollView { | ||
public MyScrollView(Context context) { | ||
super(context); | ||
} | ||
|
||
public MyScrollView(Context context, AttributeSet attrs) { | ||
super(context, attrs); | ||
} | ||
|
||
public MyScrollView(Context context, AttributeSet attrs, int defStyleAttr) { | ||
super(context, attrs, defStyleAttr); | ||
} | ||
|
||
@Override | ||
public boolean onInterceptTouchEvent(MotionEvent ev) { | ||
boolean result = super.onInterceptTouchEvent(ev); | ||
return result; | ||
} | ||
} |
41 changes: 41 additions & 0 deletions
41
TouchEventDemo/app/src/main/java/com/cyq/toucheventdemo/MyView.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
package com.cyq.toucheventdemo; | ||
|
||
import android.content.Context; | ||
import android.util.AttributeSet; | ||
import android.view.MotionEvent; | ||
import android.view.View; | ||
|
||
import androidx.annotation.Nullable; | ||
|
||
/** | ||
* Create by 陈扬齐 | ||
* Create on 2019-09-03 | ||
* description: | ||
*/ | ||
public class MyView extends View { | ||
private static final String Log = "TouchEventDemo"; | ||
|
||
public MyView(Context context) { | ||
super(context); | ||
} | ||
|
||
public MyView(Context context, @Nullable AttributeSet attrs) { | ||
super(context, attrs); | ||
} | ||
|
||
public MyView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) { | ||
super(context, attrs, defStyleAttr); | ||
} | ||
|
||
@Override | ||
public boolean dispatchTouchEvent(MotionEvent ev) { | ||
android.util.Log.d(Log, "MyView:dispatchTouchEvent"); | ||
return super.dispatchTouchEvent(ev); | ||
} | ||
|
||
@Override | ||
public boolean onTouchEvent(MotionEvent event) { | ||
android.util.Log.d(Log, "MyView:onTouchEvent"); | ||
return super.onTouchEvent(event); | ||
} | ||
} |
60 changes: 60 additions & 0 deletions
60
TouchEventDemo/app/src/main/java/com/cyq/toucheventdemo/MyViewGroup.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
package com.cyq.toucheventdemo; | ||
|
||
import android.content.Context; | ||
import android.util.AttributeSet; | ||
import android.view.MotionEvent; | ||
import android.widget.FrameLayout; | ||
|
||
import androidx.annotation.NonNull; | ||
import androidx.annotation.Nullable; | ||
|
||
/** | ||
* Create by 陈扬齐 | ||
* Create on 2019-09-03 | ||
* description: | ||
*/ | ||
public class MyViewGroup extends FrameLayout { | ||
private static final String Log = "TouchEventDemo"; | ||
|
||
public MyViewGroup(@NonNull Context context) { | ||
super(context); | ||
} | ||
|
||
public MyViewGroup(@NonNull Context context, @Nullable AttributeSet attrs) { | ||
super(context, attrs); | ||
} | ||
|
||
public MyViewGroup(@NonNull Context context, @Nullable AttributeSet attrs, int defStyleAttr) { | ||
super(context, attrs, defStyleAttr); | ||
} | ||
|
||
@Override | ||
public boolean dispatchTouchEvent(MotionEvent ev) { | ||
android.util.Log.d(Log, "MyViewGroup:dispatchTouchEvent"); | ||
return super.dispatchTouchEvent(ev); | ||
} | ||
|
||
@Override | ||
public boolean onInterceptTouchEvent(MotionEvent ev) { | ||
android.util.Log.d(Log, "MyViewGroup:onInterceptTouchEvent"); | ||
return super.onInterceptTouchEvent(ev); | ||
} | ||
|
||
@Override | ||
public boolean onTouchEvent(MotionEvent event) { | ||
android.util.Log.d(Log, "MyViewGroup:onTouchEvent"); | ||
// switch (event.getAction()) { | ||
// case MotionEvent.ACTION_DOWN: | ||
// android.util.Log.d(Log, "MyViewGroup:手指按下"); | ||
// break; | ||
// case MotionEvent.ACTION_MOVE: | ||
// android.util.Log.d(Log, "MyViewGroup:手指移动"); | ||
// break; | ||
// case MotionEvent.ACTION_UP: | ||
// android.util.Log.d(Log, "MyViewGroup:手指抬起"); | ||
// break; | ||
// } | ||
|
||
return super.onTouchEvent(event); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,18 +1,15 @@ | ||
<?xml version="1.0" encoding="utf-8"?> | ||
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android" | ||
xmlns:app="http://schemas.android.com/apk/res-auto" | ||
<com.cyq.toucheventdemo.MyViewGroup 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" | ||
android:background="@color/colorPrimary" | ||
tools:context=".MainActivity"> | ||
|
||
<TextView | ||
android:layout_width="wrap_content" | ||
android:layout_height="wrap_content" | ||
android:text="Hello World!" | ||
app:layout_constraintBottom_toBottomOf="parent" | ||
app:layout_constraintLeft_toLeftOf="parent" | ||
app:layout_constraintRight_toRightOf="parent" | ||
app:layout_constraintTop_toTopOf="parent" /> | ||
<com.cyq.toucheventdemo.MyView | ||
android:id="@+id/my_view" | ||
android:layout_width="200dp" | ||
android:layout_height="200dp" | ||
android:background="@color/colorAccent" /> | ||
|
||
</androidx.constraintlayout.widget.ConstraintLayout> | ||
</com.cyq.toucheventdemo.MyViewGroup> |