Acerca de Android Paint dibujo de color
DrawView.java
public class DrawView extends View implements OnTouchListener { private Canvas mCanvas; private Path mPath; public Paint mPaint; ArrayList<Path> paths = new ArrayList<Path>(); private ArrayList<Path> undonePaths = new ArrayList<Path>(); private MaskFilter mEmboss; private MaskFilter mBlur; private Bitmap im; public DrawView(Context context) { super(context); setFocusable(true); setFocusableInTouchMode(true); this.setOnTouchListener(this); paths.clear(); undonePaths.clear(); mPaint = new Paint(); mPaint.setAntiAlias(true); mPaint.setDither(true); mPaint.setColor(Color.BLUE); mPaint.setStyle(Paint.Style.STROKE); mPaint.setStrokeJoin(Paint.Join.ROUND); mPaint.setStrokeCap(Paint.Cap.ROUND); mPaint.setStrokeWidth(4); mEmboss = new EmbossMaskFilter(new float[] { 1, 1, 1 }, 0.4f, 6, 3.5f); mBlur = new BlurMaskFilter(8, BlurMaskFilter.Blur.NORMAL); mCanvas = new Canvas(); mPath = new Path(); // paths.add(mPath); im = BitmapFactory.decodeResource(context.getResources(), R.drawable.ic_launcher); } @Override protected void onSizeChanged(int w, int h, int oldw, int oldh) { super.onSizeChanged(w, h, oldw, oldh); } @Override protected void onDraw(Canvas canvas) { canvas.drawColor(Share.cColor); for (Path p : paths) { canvas.drawPath(p, mPaint); } canvas.drawPath(mPath, mPaint); } private float mX, mY; private static final float TOUCH_TOLERANCE = 4; private void touch_start(float x, float y) { mPaint.setColor(Share.dColor); undonePaths.clear(); mPath.reset(); mPath.moveTo(x, y); mX = x; mY = y; Log.e("", "pathsize:::" + paths.size()); Log.e("", "undonepathsize:::" + undonePaths.size()); } private void touch_move(float x, float y) { float dx = Math.abs(x - mX); float dy = Math.abs(y - mY); if (dx >= TOUCH_TOLERANCE || dy >= TOUCH_TOLERANCE) { mPath.quadTo(mX, mY, (x + mX) / 2, (y + mY) / 2); mX = x; mY = y; } } private void touch_up() { mPath.lineTo(mX, mY); // commit the path to our offscreen mCanvas.drawPath(mPath, mPaint); // kill this so we don't double draw paths.add(mPath); mPath = new Path(); Log.e("", "pathsize:::" + paths.size()); Log.e("", "undonepathsize:::" + undonePaths.size()); } public void onClickUndo() { Log.e("", "pathsize:::" + paths.size()); Log.e("", "undonepathsize:::" + undonePaths.size()); if (paths.size() > 0) { undonePaths.add(paths.remove(paths.size() - 1)); invalidate(); } else { } // toast the user } public void onClickRedo() { Log.e("", "pathsize:::" + paths.size()); Log.e("", "undonepathsize:::" + undonePaths.size()); if (undonePaths.size() > 0) { paths.add(undonePaths.remove(undonePaths.size() - 1)); invalidate(); } else { } // toast the user } @Override public boolean onTouch(View arg0, MotionEvent event) { float x = event.getX(); float y = event.getY(); switch (event.getAction()) { case MotionEvent.ACTION_DOWN: touch_start(x, y); invalidate(); break; case MotionEvent.ACTION_MOVE: touch_move(x, y); invalidate(); break; case MotionEvent.ACTION_UP: touch_up(); invalidate(); break; } return true; }
}
- Cree una ruta de trazado con Google Maps sin conexión
- ¿Cómo dibujo una flecha (en Android)?
- Anular onDraw () o draw ()?
- Explicación del ejemplo de JetBoy para dev.android.com?
- Dibujo de un pulgar hueco redondeado sobre arco
Estoy tratando de dibujar pintura de dedo con diferentes colores, pero siempre que cambiar el color de la pintura, entonces todos los anteriores camino o dibujo obtener y dibujar con color actualizado quiero dibujar con diferentes colores ¿cómo puedo hacer eso? Por favor, dame algunas soluciones para esto.
- Android cómo dibujar un rect rectangular y sombra Layout
- Android drawBitmap (...) el método es lento?
- Crear triángulo con cornors redondeados
- Un método más eficiente de dibujar miles de partículas (Java / Android)
- Dibuja línea suave entre geopuntos en android
- Unwanted Warp efecto de dibujo de texto en la ruta android
- Centrar el texto en el círculo
- Android: trazado de dibujo como superposición en MapView
Para ello, tendría que crear una nueva Paint
para cada objeto dibujado. Esto se debe a que cuando el Canvas
vuelve a dibujar, hace referencia a ese mismo objeto Paint
cada vez, por lo que todos los caminos utilizarán esta pintura.
En primer lugar, cambiaría su matriz de paths
para contener una Paint
y una Path
. Puede lograr esto usando el Pair
tipo de Android.
ArrayList<Pair<Path, Paint>> paths = new ArrayList<Pair<Path, Paint>>();
También tendrá que convertir su variable undonePaths
de esta manera.
A continuación, en el método touch_up()
, debe agregar este nuevo objeto Paint
.
Paint newPaint = new Paint(mPaint); // Clones the mPaint object paths.add(new Pair<Path, Paint>(mPath, newPaint));
Por último, su bucle tiene que ser ajustado para esto también:
for (Pair<Path, Paint> p : paths) { canvas.drawPath(p.first, p.second); }
Esto es bastante intensivo de memoria, por lo que tendrá que tener mucho cuidado para restablecer estos elementos cuando ya no están en uso, pero para tener tantos colores diferentes, debe tener todos estos diferentes objetos de Paint
.
- Diferencia entre compilar vs compilar árbol vs compilar archivos?
- Esta es una orden de prueba, no se le cobrará