java - How to change a view from a different activity. -
right goal have colors activity allow users hit button change color drawing with. how instance of canvas in activity_main.xml colors activity can change color drawn on it?
activity_main.xml
<?xml version="1.0" encoding="utf-8"?> <relativelayout 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="#09b8c1" android:paddingbottom="@dimen/activity_vertical_margin" android:paddingleft="@dimen/activity_horizontal_margin" android:paddingright="@dimen/activity_horizontal_margin" android:paddingtop="@dimen/activity_vertical_margin" tools:context="com.app.mainactivity"> <com.app.color.drawingview android:layout_width="match_parent" android:layout_height="200dp" android:id="@+id/drawing" android:layout_marginleft="5dp" android:layout_marginright="5dp" android:background="#ffffffff" android:layout_above="@+id/settings" android:layout_alignparenttop="true" /> <button android:layout_width="wrap_content" android:layout_height="wrap_content" android:onclick="changecolor" android:text="@string/settings" android:id="@+id/settings" android:layout_alignparentbottom="true" android:layout_alignparentright="true" android:layout_alignparentend="true" /> <button android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="clear" android:id="@+id/button5" android:layout_alignparentbottom="true" android:layout_alignparentleft="true" android:layout_alignparentstart="true" android:onclick="clearscreen" /> </relativelayout>
mainactivity.java
package com.app.color; import android.content.intent; import android.graphics.canvas; import android.graphics.color; import android.support.v7.app.appcompatactivity; import android.os.bundle; import android.view.view; public class mainactivity extends appcompatactivity { drawingview dv = new drawingview(); @override protected void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.activity_main); } public void changecolor(view view) { intent intent = new intent(mainactivity.this, colors.class); startactivity(intent); } public void clearscreen(view view) { } }
colors.java
package com.app.color; import android.content.intent; import android.support.v7.app.appcompatactivity; import android.os.bundle; import android.view.view; public class colors extends appcompatactivity { drawingview dv = new drawingview(,); @override protected void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.activity_colors); } public void towhite(view view) { dv.setcolor("#ffffff"); intent intent = new intent(colors.this, mainactivity.class); startactivity(intent); } public void toyellow(view view) { dv.setcolor("#ffff00"); intent intent = new intent(colors.this, mainactivity.class); startactivity(intent); } public void toblue(view view) { dv.setcolor("#0000ff"); intent intent = new intent(colors.this, mainactivity.class); startactivity(intent); } public void tored(view view) { dv.setcolor("#ff0000"); intent intent = new intent(colors.this, mainactivity.class); startactivity(intent); } public void togreen(view view) { dv.setcolor("#00ff00"); intent intent = new intent(colors.this, mainactivity.class); startactivity(intent); } public void toblack(view view) { dv.setcolor("#00000"); intent intent = new intent(colors.this, mainactivity.class); startactivity(intent); } }
drawingview.java
package com.app.color; import android.content.context; import android.graphics.canvas; import android.graphics.color; import android.graphics.paint; import android.graphics.path; import android.util.attributeset; import android.view.motionevent; import android.view.view; public class drawingview extends view { private paint paint = new paint(); private path path = new path(); public drawingview(context context, attributeset attrs) { super(context, attrs); paint.setantialias(true); paint.setstrokewidth(5f); paint.setcolor(color.black); paint.setstyle(paint.style.stroke); paint.setstrokejoin(paint.join.round); } @override protected void ondraw(canvas canvas) { canvas.drawpath(path, paint); } @override public boolean ontouchevent(motionevent event) { // coordinates of touch event. float eventx = event.getx(); float eventy = event.gety(); switch (event.getaction()) { case motionevent.action_down: // set new starting point path.moveto(eventx, eventy); return true; case motionevent.action_move: // connect points path.lineto(eventx, eventy); break; default: return false; } // makes our view repaint , call ondraw invalidate(); return true; } public void setcolor(string newcolor){ invalidate(); int paintcolor = color.parsecolor(newcolor); paint.setcolor(paintcolor); } }
activity_colors.xml
<?xml version="1.0" encoding="utf-8"?> <relativelayout 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:paddingbottom="@dimen/activity_vertical_margin" android:paddingleft="@dimen/activity_horizontal_margin" android:paddingright="@dimen/activity_horizontal_margin" android:paddingtop="@dimen/activity_vertical_margin" tools:context="com.app.colors"> <textview android:layout_width="wrap_content" android:layout_height="wrap_content" android:textappearance="?android:attr/textappearancelarge" android:text="choose color" android:id="@+id/textview" android:layout_alignparenttop="true" android:layout_centerhorizontal="true" /> <button android:layout_width="wrap_content" android:layout_height="wrap_content" android:id="@+id/button" android:layout_below="@+id/textview" android:onclick="towhite" android:layout_alignparentleft="true" android:layout_alignparentstart="true" android:layout_alignparentright="true" android:layout_alignparentend="true" android:background="#ffffff" android:text="@string/white" /> <button android:layout_width="wrap_content" android:layout_height="wrap_content" android:id="@+id/button2" android:layout_below="@+id/button" android:onclick="toblue" android:layout_alignright="@+id/button" android:layout_alignend="@+id/button" android:layout_alignparentleft="true" android:layout_alignparentstart="true" android:background="#153fc7" android:text="@string/blue" /> <button android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="@string/red" android:id="@+id/button3" android:layout_below="@+id/button2" android:onclick="tored" android:layout_alignright="@+id/button2" android:layout_alignend="@+id/button2" android:layout_alignparentleft="true" android:layout_alignparentstart="true" android:background="#f01010" /> <button android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="@string/yellow" android:id="@+id/button4" android:background="#f7e80f" android:layout_below="@+id/button3" android:onclick="toyellow" android:layout_alignparentleft="true" android:layout_alignparentstart="true" android:layout_alignright="@+id/button3" android:layout_alignend="@+id/button3" /> <button android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="@string/green" android:id="@+id/button6" android:layout_below="@+id/button4" android:onclick="togreen" android:layout_alignright="@+id/button4" android:layout_alignend="@+id/button4" android:layout_alignparentleft="true" android:layout_alignparentstart="true" android:background="#00ff00" /> <button android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="@string/black" android:id="@+id/button7" android:layout_below="@+id/button6" android:background="#000000" android:onclick="toblack" android:textcolor="#ffffff" android:layout_alignparentright="true" android:layout_alignparentend="true" android:layout_alignparentleft="true" android:layout_alignparentstart="true" /> </relativelayout>
my understanding that, want change color of drawingview view on event (ex: button click).
for please follow below code :
firstly, create attribute styles in attrs.xml
<declare-styleable name="drawingview"> <!-- drawing view color attributes --> <attr name="dv_background_color" format="color" /> <attr name="dv_foreground_color" format="color" /> <!-- can add other custom attributes --> <attr name="dv_padding" format="dimension" /> <attr name="dv_width" format="dimension" /> </declare-styleable>
now write below code in java class file :
public class drawingview extends view { //default colors private int mdrawingviewbackgroundcolor = 0xff0000ff; private int mdrawingviewforegroundcolor = 0xff0000ff; //default padding private int mviewpadding = 5; private int mviewwidth = 10; private paint mforegroundcolorpaint = new paint(); private path mbackgroundcolorpaint = new path(); public drawingview(context context, attributeset attrs) { super(context, attrs); typedarray typearr = context.obtainstyledattributes(attrs, r.styleable.drawingview); try { //inflate attributes of drawingview xml initialize in activity_main.xml layout file. mdrawingviewbackgroundcolor = typearr.getcolor(r.styleable.drawingview_dv_background_color, mdrawingviewbackgroundcolor); mdrawingviewforegroundcolor = (r.styleable.drawingview_dv_background_color, mdrawingviewforegroundcolor); mviewpadding = (int)typearr.getdimension(r.styleable.drawingview_dv_padding, mviewpadding); mviewwidth = (int)typearr.getdimension(r.styleable.drawingview_dv_width, mviewwidth); }finally { typearr.recycle(); } setuppaints(); } setuppaints() { //render drawingview colors - foreground color mforegroundcolorpaint.setcolor(mdrawingviewforegroundcolor); mforegroundcolorpaint.setantialias(true); mforegroundcolorpaint.setstrokewidth(5f); mforegroundcolorpaint.setstyle(paint.style.stroke); mforegroundcolorpaint.setstrokejoin(paint.join.round); //render drawingview colors - background color mbackgroundcolorpaint.setcolor(mdrawingviewbackgroundcolor); mbackgroundcolorpaint.setantialias(true); mbackgroundcolorpaint.setstrokewidth(5f); mbackgroundcolorpaint.setstyle(paint.style.stroke); mbackgroundcolorpaint.setstrokejoin(paint.join.round); } @override public void invalidate() { setuppaints(); super.invalidate(); } @override protected void ondraw(canvas canvas) { canvas.drawpath(path, paint); }
}
Comments
Post a Comment