Codigo Main Activity
Código:
Codigo Lienzo:import android.os.Bundle; import android.support.v7.app.AppCompatActivity; import android.support.v7.widget.Toolbar; import android.view.View; import android.view.Menu; import android.view.MenuItem; import android.widget.ImageButton; public class MainActivity extends AppCompatActivity implements View.OnClickListener { ImageButton negro,blanco,rojo,verde,azul; public static Lienzo lienzo; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar); setSupportActionBar(toolbar); negro = (ImageButton) findViewById(R.id.Negro); blanco = (ImageButton) findViewById(R.id.Blanco); rojo = (ImageButton) findViewById(R.id.Rojo); verde = (ImageButton) findViewById(R.id.Verde); azul = (ImageButton) findViewById(R.id.Azul); negro.setOnClickListener(this); blanco.setOnClickListener(this); rojo.setOnClickListener(this); verde.setOnClickListener(this); azul.setOnClickListener(this); lienzo = (Lienzo) findViewById(R.id.lienzo); } @Override public boolean onCreateOptionsMenu(Menu menu) { // Inflate the menu; this adds items to the action bar if it is present. getMenuInflater().inflate(R.menu.menu_main, menu); return true; } @Override public boolean onOptionsItemSelected(MenuItem item) { // Handle action bar item clicks here. The action bar will // automatically handle clicks on the Home/Up button, so long // as you specify a parent activity in AndroidManifest.xml. int id = item.getItemId(); //noinspection SimplifiableIfStatement if (id == R.id.action_settings) { return true; } return super.onOptionsItemSelected(item); } @Override public void onClick(View view) { String color=null; switch (view.getId()){ case (R.id.Negro): color= view.getTag().toString(); lienzo.setColor(color); break; case (R.id.Blanco): color= view.getTag().toString(); lienzo.setColor(color); break; case (R.id.Rojo): color= view.getTag().toString(); lienzo.setColor(color); break; case (R.id.Verde): color= view.getTag().toString(); lienzo.setColor(color); break; case (R.id.Azul): color= view.getTag().toString(); lienzo.setColor(color); break; default: break; } } }
Código:
Codigo Layout:import android.content.Context; import android.graphics.Bitmap; 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 Lienzo extends View{ //Path que utilizara para pintar las lineas private Path drawPath; //Paint de dibujar y paint de canvas private Paint drawPaint,canvasPaint; //Color inicial private int paintColor = 0xFFFF0000; //Canvas private Canvas drawCanvas; //Canvas para guardar private Bitmap canvasBitmap; public Lienzo(Context context, AttributeSet attrs) { super(context, attrs); setupDrawing(); } private void setupDrawing(){ //Configuracion del area sobre la que va a pintar drawPath = new Path(); drawPaint = new Paint(); drawPaint.setColor(paintColor); drawPaint.setAntiAlias(true); drawPaint.setStrokeWidth(20); drawPaint.setStyle(Paint.Style.STROKE); drawPaint.setStrokeJoin(Paint.Join.ROUND); drawPaint.setStrokeCap(Paint.Cap.ROUND); canvasPaint = new Paint(Paint.DITHER_FLAG); } //Tamaño asignado a la vista cuando crea el objeto @Override protected void onSizeChanged(int w, int h, int oldw, int oldh){ super.onSizeChanged(w,h,oldw,oldh); canvasBitmap = Bitmap.createBitmap(w,h,Bitmap.Config.ARGB_8888); drawCanvas = new Canvas(canvasBitmap); } //Pinta la vista. Sera llamado desde el onTouch @Override protected void onDraw(Canvas canvas){ canvas.drawBitmap(canvasBitmap,0,0,canvasPaint); canvas.drawPath(drawPath,drawPaint); } //Registra los Touch del usuario @Override public boolean onTouchEvent(MotionEvent event){ float touchX = event.getX(); float touchY = event.getY(); switch (event.getAction()){ case MotionEvent.ACTION_DOWN: drawPath.moveTo(touchX,touchY); break; case MotionEvent.ACTION_MOVE: drawPath.lineTo(touchX,touchY); break; case MotionEvent.ACTION_UP: drawPath.lineTo(touchX,touchY); drawCanvas.drawPath(drawPath,drawPaint); drawPath.reset(); break; default: return false; } //Repintar invalidate(); return true; } //Actualiza color public void setColor (String newColor){ invalidate(); paintColor = Color.parseColor(newColor); drawPaint.setColor(paintColor); } }
Código:
Desde ya gracias <?xml version="1.0" encoding="utf-8"?> <LinearLayout 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:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" app:layout_behavior="@string/appbar_scrolling_view_behavior" tools:context="com.example.nicolas.paint.MainActivity" tools:showIn="@layout/activity_main"> <TextView android:id="@+id/textView" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="TextView" /> <LinearLayout android:layout_width="match_parent" android:layout_height="0dp" android:layout_weight="2" android:orientation="horizontal"> <ImageButton android:id="@+id/button" android:layout_width="0dp" android:layout_height="match_parent" android:layout_weight="1" android:src="@drawable/ic_stat_name" /> <ImageButton android:id="@+id/button2" android:layout_width="0dp" android:layout_height="match_parent" android:layout_weight="1" android:src="@drawable/trazo" /> <ImageButton android:id="@+id/button3" android:layout_width="0dp" android:layout_height="match_parent" android:layout_weight="1" android:src="@drawable/borrar" /> <ImageButton android:id="@+id/button4" android:layout_width="0dp" android:layout_height="match_parent" android:layout_weight="1" android:src="@drawable/guardar" /> </LinearLayout> <view class="com.example.nicolas.paint.Lienzo" id="@+id/lienzo" layout_height="0dp" layout_weight="1" layout_width="match_parent" android:layout_width="match_parent" android:layout_height="0dp" android:layout_weight="12" android:background="#FFFFFFFF" /> <LinearLayout android:layout_width="match_parent" android:layout_height="0dp" android:layout_weight="1" android:orientation="horizontal"> <ImageButton android:id="@+id/Negro" android:layout_width="0dp" android:layout_height="@dimen/alturacolor" android:layout_weight="1" android:background="#FF000000" android:tag="#FF000000" /> <ImageButton android:id="@+id/Blanco" android:layout_width="0dp" android:layout_height="@dimen/alturacolor" android:layout_weight="1" android:background="#FFFFFFFF" android:tag="#FFFFFFFF" /> <ImageButton android:id="@+id/Azul" android:layout_width="0dp" android:layout_height="@dimen/alturacolor" android:layout_weight="1" android:background="#FF0000FF" android:tag="#FF0000FF" /> <ImageButton android:id="@+id/Rojo" android:layout_width="0dp" android:layout_height="@dimen/alturacolor" android:layout_weight="1" android:background="#FFFF0000" android:tag="#FFFF0000" /> <ImageButton android:id="@+id/Verde" android:layout_width="0dp" android:layout_height="@dimen/alturacolor" android:layout_weight="1" android:background="#FF00FF00" android:tag="#FF00FF00" /> </LinearLayout> </LinearLayout>