Hallo Zusammen
Ich habe hier meinen Code der Linien macht in zufälligen Farben. Nun hab ich 3 Buttons zum Layout hinzugefügt welche einen jeweiligen Wert besitzen, Button1 = 1, 2 =2 und 3 = 3, nun muss ich diese Werte in meiner View - Klasse aufrufen können und benutzen können um dort die Farbe zu verändern.
Ich habe bereits etwas mit getter und setter, sowohl intents probiert, bin bis anhin aber erfolglos geblieben...
Wäre um Hilfe dankbar!
Hauptklasse
Java
package com.example.drawproject;
import android.support.v7.app.ActionBarActivity;
import android.support.v7.app.ActionBar;
import android.support.v4.app.Fragment;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.os.Build;
public class MainActivity extends Activity {
Button b1; //Gelb
Button b2; //Blau
Button b3; //Grün
public int value = 0;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.drawlayout);
DrawArea da = new DrawArea(this, null);
b1 = (Button) findViewById(R.id.button1);
b1.setOnClickListener(handler);
b2 = (Button) findViewById(R.id.button2);
b2.setOnClickListener(handler);
b3 = (Button) findViewById(R.id.button3);
b3.setOnClickListener(handler);
}
View.OnClickListener handler = new View.OnClickListener(){
public void onClick(View v){
if(v==b1){
value = 1;
}
if(v==b2){
value = 2;
}
if(v==b3){
value = 3;
}
}};
}
Alles anzeigen
HauptLayout
Java
<?xml version="1.0" encoding="utf-8"?><RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" > <com.example.drawproject.DrawArea android:layout_width="match_parent" android:layout_height="match_parent" android:layout_below="@+id/button1" /><Button android:id="@+id/button1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignParentLeft="true" android:layout_alignParentTop="true" android:layout_marginLeft="84dp" android:layout_marginTop="79dp" android:text="Yellow" /><Button android:id="@+id/button2" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignBaseline="@+id/button1" android:layout_alignBottom="@+id/button1" android:layout_marginLeft="165dp" android:layout_toRightOf="@+id/button1" android:text="Blue" /><Button android:id="@+id/button3" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignBottom="@+id/button2" android:layout_alignParentRight="true" android:layout_marginRight="134dp" android:text="Green" /></RelativeLayout>
View Klasse
Java
package com.example.drawproject;import android.content.Context;import android.graphics.*;import android.util.AttributeSet;import android.util.SparseArray;import android.view.MotionEvent;import android.view.View;import java.util.ArrayList;import java.util.List;import java.util.Random;public class DrawArea extends View {private List<Stroke> _allStrokes; //all strokes that need to be drawnprivate SparseArray<Stroke> _activeStrokes; //use to retrieve the currently drawn strokesprivate Random _rdmColor = new Random();int count = 1;public DrawArea(Context context, AttributeSet attrs) { super(context, attrs); _allStrokes = new ArrayList<Stroke>(); _activeStrokes = new SparseArray<Stroke>(); setFocusable(true); setFocusableInTouchMode(true);}public void onDraw(Canvas canvas) { if (_allStrokes != null) { for (Stroke stroke: _allStrokes) { if (stroke != null) { Path path = stroke.getPath(); Paint painter = stroke.getPaint(); if ((path != null) && (painter != null)) { if(count%2 != 0){ canvas.drawPath(path, painter); } } } } }}@Overridepublic boolean onTouchEvent(MotionEvent event) { final int action = event.getActionMasked(); final int pointerCount = event.getPointerCount(); switch (action) { case MotionEvent.ACTION_DOWN: { count++; if(count%2 != 1) {pointDown((int)event.getX(), (int)event.getY(), event.getPointerId(0)); break; } if (count%2 != 0){ for (int pc = 0; pc < pointerCount; pc++) { pointDown((int)event.getX(pc), (int)event.getY(pc), event.getPointerId(pc)); } } } case MotionEvent.ACTION_MOVE: { break; } case MotionEvent.ACTION_UP: { break; } } invalidate(); return true;}private void pointDown(int x, int y, int id) { if(count%2 !=1){ //create a paint with random color Paint paint = new Paint(); paint.setStyle(Paint.Style.STROKE); paint.setStrokeWidth(10); paint.setColor(_rdmColor.nextInt()); //Here should the values be added! //create the Stroke Point pt = new Point(x, y); Stroke stroke = new Stroke(paint); stroke.addPoint(pt); _activeStrokes.put(id, stroke); _allStrokes.add(stroke); } if (count%2 != 0){ //retrieve the stroke and add new point to its path Stroke stroke = _activeStrokes.get(id); if (stroke != null) { Point pt = new Point(x, y); stroke.addPoint(pt); } }}}
Zusatz View Klasse
Java
package com.example.drawproject;import android.graphics.Paint;import android.graphics.Path;import android.graphics.Point;public class Stroke { private Path _path; private Paint _paint; public Stroke (Paint paint) { _paint = paint; } public Path getPath() { return _path; } public Paint getPaint() { return _paint; } public void addPoint(Point pt) { if (_path == null) { _path = new Path(); _path.moveTo(pt.x, pt.y); } else { _path.lineTo(pt.x, pt.y); } }}