Paint en android
Enviado por astra123123 • 4 de Octubre de 2020 • Tarea • 5.535 Palabras (23 Páginas) • 74 Visitas
Activity main
[pic 1]
Código XML
xml version="1.0" encoding="utf-8"?>
<androidx.drawerlayout.widget.DrawerLayout 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:id="@+id/drawerLayout"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<androidx.constraintlayout.widget.ConstraintLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<LinearLayout
android:id="@+id/layoutToolBar"
android:layout_width="match_parent"
android:layout_height="?actionBarSize"
android:background="@color/colorPrimary"
android:gravity="center_vertical"
android:orientation="horizontal"
android:paddingStart="15dp"
android:paddingEnd="15dp"
app:layout_constraintTop_toTopOf="parent">
<ImageView
android:id="@+id/imageMenu"
android:layout_width="30dp"
android:layout_height="30dp"
android:contentDescription="@string/app_name"
android:src="@drawable/ic_menu"
android:tint="@color/colorWhite" />
<TextView
android:id="@+id/textTitle"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginStart="15dp"
android:text="@string/app_name"
android:textColor="@color/colorWhite"
android:textSize="18sp"
android:textStyle="bold" />
LinearLayout>
<androidx.constraintlayout.widget.ConstraintLayout
android:layout_width="match_parent"
android:layout_height="0dp"
app:layout_constraintTop_toBottomOf="@id/layoutToolBar"
app:layout_constraintBottom_toBottomOf="parent"
android:id="@+id/miLayout"
android:background="@color/colorWhite">
<com.example.paint.lienzo
android:id="@+id/lienzo"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@color/colorWhite"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
androidx.constraintlayout.widget.ConstraintLayout>
androidx.constraintlayout.widget.ConstraintLayout>
<com.google.android.material.navigation.NavigationView
android:id="@+id/navigationView"
android:layout_width="wrap_content"
android:layout_height="match_parent"
app:headerLayout="@layout/layout_navigation_header"
app:menu="@menu/navigation_menu"
android:layout_gravity="start" />
androidx.drawerlayout.widget.DrawerLayout>
Código java
package com.example.paint;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.appcompat.app.AlertDialog;
import androidx.appcompat.app.AppCompatActivity;
import androidx.core.view.GravityCompat;
import androidx.drawerlayout.widget.DrawerLayout;
import android.content.DialogInterface;
import android.content.Intent;
import android.graphics.Bitmap;
import android.net.Uri;
import android.os.Bundle;
import android.provider.MediaStore;
import android.text.InputType;
import android.view.MenuItem;
import android.view.View;
import android.widget.EditText;
import android.widget.Toast;
import com.google.android.material.navigation.NavigationView;
public class MainActivity extends AppCompatActivity {
DrawerLayout drawerLayout;
NavigationView navigationView;
lienzo lienzox;
Bitmap bitmap;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
drawerLayout =findViewById(R.id.drawerLayout);
navigationView = findViewById(R.id.navigationView);
lienzox= findViewById(R.id.lienzo);
//para al precionar en la imagen de el menu se presente
findViewById(R.id.imageMenu).setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
drawerLayout.openDrawer(GravityCompat.START);
}
});
//respetat colores de iconos
navigationView.setItemIconTintList(null);
//para seleccion de menu
navigationView.setNavigationItemSelectedListener(new NavigationView.OnNavigationItemSelectedListener() {
@Override
public boolean onNavigationItemSelected(@NonNull MenuItem item) {
useSelection(item);
return false;
}
});
}
//accion de seleccion de menu
public void useSelection(MenuItem menuItem){
String color = null;
switch (menuItem.getItemId()){
case R.id.menuNuevo:
drawerLayout.closeDrawer(GravityCompat.START);
AlertDialog.Builder newDialog = new AlertDialog.Builder(this);
newDialog.setTitle(R.string.txt_nuevo);
newDialog.setMessage(R.string.txt_advertencia);
newDialog.setPositiveButton(R.string.txt_aceptar, new DialogInterface.OnClickListener(){
public void onClick(DialogInterface dialog, int which){
lienzox.NuevoDibujo();
lienzox.setBorrado(false);
lienzox.setTamPunto(5);
lienzox.setColor("black");
bitmap=null;
dialog.dismiss();
}
});
newDialog.setNegativeButton(R.string.txt_cancelar, new DialogInterface.OnClickListener(){
public void onClick(DialogInterface dialog, int which){
dialog.cancel();
}
});
newDialog.show();
break;
case R.id.menuAbrir:
drawerLayout.closeDrawer(GravityCompat.START);
lienzox.NuevoDibujo();
Intent intent = new Intent(Intent.ACTION_GET_CONTENT);
intent.setType("image/*");
startActivityForResult(intent.createChooser(intent,"Seleccion de imagen"),10);
break;
case R.id.menuGuardar:
drawerLayout.closeDrawer(GravityCompat.START);
AlertDialog.Builder salvarDibujo = new AlertDialog.Builder(this);
final EditText input = new EditText(this);
input.setInputType(InputType.TYPE_CLASS_TEXT | InputType.TYPE_CLASS_TEXT);
salvarDibujo.setView(input);
salvarDibujo.setTitle(R.string.txt_guardar);
salvarDibujo.setMessage(R.string.txt_guardar2);
salvarDibujo.setPositiveButton(R.string.txt_aceptar, new DialogInterface.OnClickListener(){
public void onClick(DialogInterface dialog, int which){
String nombre=input.getText().toString();
lienzox.setDrawingCacheEnabled(true);
String imgSaved = MediaStore.Images.Media.insertImage
(getContentResolver(), lienzox.getDrawingCache(), nombre, "drawing");
if(imgSaved!=null)
Toast.makeText(MainActivity.this, R.string.txt_dibujo_guardado, Toast.LENGTH_SHORT).show();
else{
Toast.makeText(MainActivity.this, R.string.txt_dibujo_no_guardado, Toast.LENGTH_SHORT).show();
Toast.makeText(MainActivity.this, R.string.txt_dibujo_no_guardado2, Toast.LENGTH_SHORT).show();
}
lienzox.destroyDrawingCache();
}
});
salvarDibujo.setNegativeButton(R.string.txt_cancelar, new DialogInterface.OnClickListener(){
public void onClick(DialogInterface dialog, int which){
dialog.cancel();
}
});
salvarDibujo.show();
break;
case R.id.menuCancelar:
drawerLayout.closeDrawer(GravityCompat.START);
if(bitmap==null)
lienzox.NuevoDibujo();
else
lienzox.abrir(bitmap);
break;
case R.id.menuColorBlack:
color = "black";
lienzox.setColor(color);
Toast.makeText(this, R.string.txtColorN , Toast.LENGTH_SHORT).show();
break;
case R.id.menuColorGreen:
color = "green";
lienzox.setColor(color);
Toast.makeText(this, R.string.txtColorV, Toast.LENGTH_SHORT).show();
break;
case R.id.menuColorYellow:
color = "yellow";
lienzox.setColor(color);
Toast.makeText(this, R.string.txtColorA, Toast.LENGTH_SHORT).show();
break;
case R.id.menuColorRed:
color = "red";
lienzox.setColor(color);
Toast.makeText(this, R.string.txtColorR, Toast.LENGTH_SHORT).show();
break;
case R.id.menuLapiz1:
lienzox.setBorrado(false);
lienzox.setTamPunto(5);
Toast.makeText(this, R.string.txtL1 , Toast.LENGTH_SHORT).show();
break;
case R.id.menuLapiz2:
lienzox.setBorrado(false);
lienzox.setTamPunto(15);
Toast.makeText(this, R.string.txtL2 , Toast.LENGTH_SHORT).show();
break;
case R.id.menuLapiz3:
lienzox.setBorrado(false);
lienzox.setTamPunto(25);
Toast.makeText(this, R.string.txtL3 , Toast.LENGTH_SHORT).show();
break;
case R.id.menuLapiz4:
lienzox.setBorrado(false);
lienzox.setTamPunto(35);
Toast.makeText(this, R.string.txtL4 , Toast.LENGTH_SHORT).show();
break;
case R.id.menuLapiz5:
lienzox.setBorrado(false);
lienzox.setTamPunto(45);
Toast.makeText(this, R.string.txtL5 , Toast.LENGTH_SHORT).show();
break;
case R.id.menuBorrador1:
drawerLayout.closeDrawer(GravityCompat.START);
lienzox.setBorrado(true);
lienzox.setTamPunto(15);
Toast.makeText(this, R.string.txtB1 , Toast.LENGTH_SHORT).show();
break;
case R.id.menuBorrador2:
drawerLayout.closeDrawer(GravityCompat.START);
lienzox.setBorrado(true);
lienzox.setTamPunto(35);
Toast.makeText(this, R.string.txtB2, Toast.LENGTH_SHORT).show();
break;
case R.id.menuBorrador3:
drawerLayout.closeDrawer(GravityCompat.START);
lienzox.setBorrado(true);
lienzox.setTamPunto(55);
Toast.makeText(this, R.string.txtB3 , Toast.LENGTH_SHORT).show();
break;
default:
break;
}
}
@Override
protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if(resultCode==RESULT_OK){
try {
Uri imageUri = data.getData();
bitmap = MediaStore.Images.Media.getBitmap(this.getContentResolver(), imageUri);
lienzox.abrir(bitmap);
}catch (Exception e){
Toast.makeText(this, "Error", Toast.LENGTH_SHORT).show();
}
}
}
@Override
public void onBackPressed() {
AlertDialog.Builder newDialog = new AlertDialog.Builder(this);
newDialog.setTitle(R.string.app_name);
newDialog.setMessage(R.string.contenido);
newDialog.setPositiveButton(R.string.txt_salir, new DialogInterface.OnClickListener(){
public void onClick(DialogInterface dialog, int which){
finish();
}
});
newDialog.show();
}
@Override
protected void onDestroy() {
super.onDestroy();
}
}
...