无法在 canvas 上绘图并同时收听触摸以查看
Can't draw on canvas & listen touch to view simultaneously
我的主要 activity 包含一个 textView
。如果触摸,它会 运行 动画。
在同一个屏幕上,我可以用扩展视图画线(示例)。
canvas 中的 OnTouchEvent
必须 return 为真,但 textView 中的 onTouch
将永远无法得到它。
我希望触摸屏对两者都适用。我该怎么做?
MainActivity.java
public class MainActivity extends AppCompatActivity {
TextView tv,lettres;
TranslateAnimation animation;
ConstraintLayout constraintLayout;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
tv = findViewById(R.id.tv);
tv.setX(100);
tv.setY(100);
constraintLayout = findViewById(R.id.cl);
lettres = new TextView(this);
constraintLayout.addView(lettres);
lettres.setVisibility(View.GONE);
lettres.setText("test");
tv.setOnTouchListener(touchListener);
}
private View.OnTouchListener touchListener = new View.OnTouchListener() {
@Override
public boolean onTouch(View view, MotionEvent motionEvent) {
if (view == tv){
lettres.setX(50);
lettres.setY(50);
animation = new TranslateAnimation(0,10,0,10);
lettres[i].startAnimation(animation[i]);
}
return false;
}
};
}
activity_main.XML
<?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"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/cl"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<TextView
android:id="@+id/tv"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Hello World!" />
<fr.DrawingView
android:id="@+id/myDrawingView"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</androidx.constraintlayout.widget.ConstraintLayout>
DrawingView.java
public class DrawingView extends View {
Paint myPaint;
int debutX=0;
int debutY=0;
int finX=0;
int finY=0;
public DrawingView(Context context, AttributeSet attributeSet) {
super(context,attributeSet);
myPaint = new Paint();
myPaint.setColor(Color.RED);
myPaint.setStyle(Paint.Style.FILL_AND_STROKE);
myPaint.setStrokeWidth(20);
}
@Override
protected void onDraw(Canvas canvas) {
canvas.drawLine(debutX, debutY, finX, finY, myPaint);
invalidate();
}
@Override
public boolean onTouchEvent(MotionEvent event) {
switch (event.getAction()) {
case MotionEvent.ACTION_DOWN:
debutX = (int) event.getX();
debutY = (int) event.getY();
finX = (int) event.getX();
finY = (int) event.getY();
invalidate();
break;
case MotionEvent.ACTION_MOVE:
finX = (int) event.getX();
finY = (int) event.getY();
invalidate();
break;
case MotionEvent.ACTION_UP:
invalidate();
break;
}
return true;
}
}
因为你的主布局消耗了触摸事件。
而你 return true inside onTouchEvent
我的主要 activity 包含一个 textView
。如果触摸,它会 运行 动画。
在同一个屏幕上,我可以用扩展视图画线(示例)。
OnTouchEvent
必须 return 为真,但 textView 中的 onTouch
将永远无法得到它。
我希望触摸屏对两者都适用。我该怎么做?
MainActivity.java
public class MainActivity extends AppCompatActivity {
TextView tv,lettres;
TranslateAnimation animation;
ConstraintLayout constraintLayout;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
tv = findViewById(R.id.tv);
tv.setX(100);
tv.setY(100);
constraintLayout = findViewById(R.id.cl);
lettres = new TextView(this);
constraintLayout.addView(lettres);
lettres.setVisibility(View.GONE);
lettres.setText("test");
tv.setOnTouchListener(touchListener);
}
private View.OnTouchListener touchListener = new View.OnTouchListener() {
@Override
public boolean onTouch(View view, MotionEvent motionEvent) {
if (view == tv){
lettres.setX(50);
lettres.setY(50);
animation = new TranslateAnimation(0,10,0,10);
lettres[i].startAnimation(animation[i]);
}
return false;
}
};
}
activity_main.XML
<?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"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/cl"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<TextView
android:id="@+id/tv"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Hello World!" />
<fr.DrawingView
android:id="@+id/myDrawingView"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</androidx.constraintlayout.widget.ConstraintLayout>
DrawingView.java
public class DrawingView extends View {
Paint myPaint;
int debutX=0;
int debutY=0;
int finX=0;
int finY=0;
public DrawingView(Context context, AttributeSet attributeSet) {
super(context,attributeSet);
myPaint = new Paint();
myPaint.setColor(Color.RED);
myPaint.setStyle(Paint.Style.FILL_AND_STROKE);
myPaint.setStrokeWidth(20);
}
@Override
protected void onDraw(Canvas canvas) {
canvas.drawLine(debutX, debutY, finX, finY, myPaint);
invalidate();
}
@Override
public boolean onTouchEvent(MotionEvent event) {
switch (event.getAction()) {
case MotionEvent.ACTION_DOWN:
debutX = (int) event.getX();
debutY = (int) event.getY();
finX = (int) event.getX();
finY = (int) event.getY();
invalidate();
break;
case MotionEvent.ACTION_MOVE:
finX = (int) event.getX();
finY = (int) event.getY();
invalidate();
break;
case MotionEvent.ACTION_UP:
invalidate();
break;
}
return true;
}
}
因为你的主布局消耗了触摸事件。 而你 return true inside onTouchEvent