gpt4 book ai didi

java - 无法在 Canvas 上绘图并同时聆听触摸以查看

转载 作者:行者123 更新时间:2023-12-02 09:20:38 24 4
gpt4 key购买 nike

我的主要 Activity 包含一个textView。如果触摸它会运行动画。在同一屏幕中,我可以使用扩展 View 绘制线条(示例)。

canvas 中的

OnTouchEvent 必须返回 true,但 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;
}
}

最佳答案

因为你的主布局消耗了触摸事件。然后你在 onTouchEvent 中返回 true

关于java - 无法在 Canvas 上绘图并同时聆听触摸以查看,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58720760/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com