Salve a tutti, non riesco a cambiare il colore per disegnare sullo schermo.
Mi potreste aiutare? Grazie.
Ho usato il color picker AmbilWarna.
Questo è il codice che ho usato:
L'Activity
codice:
import yuku.ambilwarna.AmbilWarnaDialog;
import yuku.ambilwarna.AmbilWarnaDialog.OnAmbilWarnaListener;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.Toast;
import android.app.Activity;
import android.graphics.Color;
public class MainActivity extends Activity
{
public int initialColor = 0xffff0000;
Button colorPicker;
public int r = 255, g = 0, b = 0;
//Identificatore delle preferenze dell'applicazione
public static String MY_PREFERENCES = "MyPref";
@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//Ottiene il riferimento al bottone
this.colorPicker = (Button)findViewById(R.id.btnColorPicker);
//Definisce il click event listener per il bottone
OnClickListener clickListener = new OnClickListener()
{
@Override
public void onClick(View v)
{
colorpicker();
}
};
//Setta il click event listener per il bottone
this.colorPicker.setOnClickListener(clickListener);
}
public void colorpicker()
{
//initialColor è il colore iniziale selezionato mostrato nel rettangolo alla sinistra della freccia
AmbilWarnaDialog dialog = new AmbilWarnaDialog(this, this.initialColor, new OnAmbilWarnaListener()
{
//Eseguita quando l'utente clicca il bottone Ok
//color è il colore selezionato dall'utente
@Override
public void onOk(AmbilWarnaDialog dialog, int color)
{
//Setta il colore iniziale al colore scelto dall'utente
initialColor = color;
r = Color.red(initialColor);
g = Color.green(initialColor);
b = Color.blue(initialColor);
Toast.makeText(getBaseContext(), "Selected Color: " + Color.red(initialColor) + ", " + Color.green(initialColor) + ", " + Color.blue(initialColor), Toast.LENGTH_LONG).show();
}
//Eseguita quando l'utente clicca il bottone Cancel
@Override
public void onCancel(AmbilWarnaDialog dialog)
{
}
});
//Mostra il colorpicker
dialog.show();
}
}
La View
codice:
import java.util.ArrayList;
import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Path;
import android.graphics.RectF;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.view.View;
public class MultitouchView extends View
{
public static final int MAX_FINGERS = 5;
private Path[] mFingerPaths = new Path[MAX_FINGERS];
private Paint mFingerPaint;
private ArrayList<Path> mCompletedPaths;
private RectF mPathBounds = new RectF();
public MultitouchView(Context context)
{
super(context);
}
public MultitouchView(Context context, AttributeSet attrs)
{
super(context, attrs);
}
public MultitouchView(Context context, AttributeSet attrs, int defStyle)
{
super(context, attrs, defStyle);
}
@Override
protected void onAttachedToWindow()
{
super.onAttachedToWindow();
MainActivity color = new MainActivity();
mCompletedPaths = new ArrayList<Path>();
mFingerPaint = new Paint();
mFingerPaint.setAntiAlias(true);
mFingerPaint.setColor(Color.rgb(color.r, color.g, color.b));
mFingerPaint.setStyle(Paint.Style.STROKE);
mFingerPaint.setStrokeWidth(6);
mFingerPaint.setStrokeCap(Paint.Cap.BUTT);
}
@Override
protected void onDraw(Canvas canvas)
{
super.onDraw(canvas);
for (Path completedPath : mCompletedPaths)
{
canvas.drawPath(completedPath, mFingerPaint);
}
for (Path fingerPath : mFingerPaths)
{
if (fingerPath != null)
{
canvas.drawPath(fingerPath, mFingerPaint);
}
}
}
@Override
public boolean onTouchEvent(MotionEvent event)
{
int pointerCount = event.getPointerCount();
int cappedPointerCount = pointerCount > MAX_FINGERS ? MAX_FINGERS : pointerCount;
int actionIndex = event.getActionIndex();
int action = event.getActionMasked();
int id = event.getPointerId(actionIndex);
if ((action == MotionEvent.ACTION_DOWN || action == MotionEvent.ACTION_POINTER_DOWN) && id < MAX_FINGERS)
{
mFingerPaths[id] = new Path();
mFingerPaths[id].moveTo(event.getX(actionIndex), event.getY(actionIndex));
}
else if ((action == MotionEvent.ACTION_POINTER_UP || action == MotionEvent.ACTION_UP) && id < MAX_FINGERS)
{
mFingerPaths[id].setLastPoint(event.getX(actionIndex), event.getY(actionIndex));
mCompletedPaths.add(mFingerPaths[id]);
mFingerPaths[id].computeBounds(mPathBounds, true);
invalidate((int) mPathBounds.left, (int) mPathBounds.top,
(int) mPathBounds.right, (int) mPathBounds.bottom);
mFingerPaths[id] = null;
}
for(int i = 0; i < cappedPointerCount; i++)
{
if(mFingerPaths[i] != null)
{
int index = event.findPointerIndex(i);
mFingerPaths[i].lineTo(event.getX(index), event.getY(index));
mFingerPaths[i].computeBounds(mPathBounds, true);
invalidate((int) mPathBounds.left, (int) mPathBounds.top,
(int) mPathBounds.right, (int) mPathBounds.bottom);
}
}
return true;
}
}