java - Touch doesn't work in Canvas and Layout -
i start study java on android. in programs tried change color in layout background , canvas color when touch screen. thought it's easy, ...nothing happen when touch screen using implement ontouchlistener+ ontouch() .actually, log.w not shown in logcat . when use ontouchevent() in layout color changed, in canvas log.w informed me program answered touch, color didn’t change. so, have 2 issues:
1. implement ontouchlistener+ ontouch() doesn’t works @ in both cases. 2. ontouchevent() in canvas works partly , doesn’t change color.
please me understand mistakes are? wrong? tried use "return false" in ontouch() , replace listener+ontouch() in main activity class, , many others.
**canvas** import android.app.activity; import android.content.context; import android.graphics.canvas; import android.graphics.color; import android.os.bundle; import android.util.log; import android.view.motionevent; import android.view.view; import android.view.view.ontouchlistener; public class mainactivity extends activity { @override protected void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(new mycanvas(this)); } } class mycanvas extends view //implements ontouchlistener { private int a=0; public mycanvas(context context){ super(context); } @override public void ondraw( canvas canvas){ if(a==0) {canvas.drawcolor(color.magenta); } else { canvas.drawcolor(color.blue); } } /* @override public boolean ontouch(view view,motionevent event){ if(event.getaction()==motionevent.action_down) { log.w("my", "inside ontouch"); if(a==0)a=1; else a=0; invalidate(); } return false; }*/ public boolean ontouchevent(motionevent event){ if(event.getaction()==motionevent.action_down) { log.w("my", "inside ontouch"); if(a==0)a=1; else a=0; invalidate(); } return true; } } **layout** <?xml version="1.0" encoding="utf-8"?> <android.support.constraint.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/rayout1" android:layout_width="match_parent" android:layout_height="match_parent" android:background="@color/coloraccent" tools:context="com.example.cathy.touchlayout.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" /> </android.support.constraint.constraintlayout> import android.graphics.color; import android.support.constraint.constraintlayout; import android.support.v7.app.appcompatactivity; import android.os.bundle; import android.view.motionevent; import android.view.view; public class mainactivity extends appcompatactivity //implements view.ontouchlistener { constraintlayout mylayout; @override protected void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.activity_main); mylayout=(constraintlayout)findviewbyid(r.id.rayout1); } public boolean ontouchevent( motionevent event) { if(event.getaction()==motionevent.action_down) { mylayout.setbackgroundcolor(color.blue); } return true; } /* @override public boolean ontouch( view v,motionevent event) { if(event.getaction()==motionevent.action_down) { mylayout.setbackgroundcolor(color.blue); } return true; }*/ }
Comments
Post a Comment