In this video it shows how one can make his/ her paint or drawing android app. In this it creates a free hand drawing/ sketching App.
It uses onTouchEvent to get the X and Y points of user's touch movement on the screen. Then it draws those lines on the imageView using the draw line method of the canvas.
The App shown in this video is also hosted on Play Store. Please refer at - [ Ссылка ]
I hope you like this video. For any questions, suggestions or appreciation please contact us at: [ Ссылка ] or email at: programmerworld1990@gmail.com
Complete source code and other details/ steps of this video are posted in the below link:
[ Ссылка ]
However, the main Java code is copied below also for reference:
package com.programmerworld.paint_drawingapp;
import android.Manifest;
import android.content.pm.PackageManager;
import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.os.Bundle;
import android.os.Environment;
import android.view.MotionEvent;
import android.view.View;
import android.widget.ImageView;
import android.widget.Toast;
import androidx.appcompat.app.AppCompatActivity;
import androidx.core.app.ActivityCompat;
import java.io.File;
import java.io.FileOutputStream;
import java.util.Calendar;
public class MainActivity extends AppCompatActivity {
private ImageView imageView;
private float floatStartX = -1, floatStartY = -1,
floatEndX = -1, floatEndY = -1;
private Bitmap bitmap;
private Canvas canvas;
private Paint paint = new Paint();
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ActivityCompat.requestPermissions(this
,new String[]{Manifest.permission.READ_EXTERNAL_STORAGE,
Manifest.permission.WRITE_EXTERNAL_STORAGE},
PackageManager.PERMISSION_GRANTED);
imageView = findViewById(R.id.imageView);
}
private void drawPaintSketchImage(){
if (bitmap == null){
bitmap = Bitmap.createBitmap(imageView.getWidth(),
imageView.getHeight(),
Bitmap.Config.ARGB_8888);
canvas = new Canvas(bitmap);
paint.setColor(Color.RED);
paint.setAntiAlias(true);
paint.setStyle(Paint.Style.STROKE);
paint.setStrokeWidth(8);
}
canvas.drawLine(floatStartX,
floatStartY-220,
floatEndX,
floatEndY-220,
paint);
imageView.setImageBitmap(bitmap);
}
@Override
public boolean onTouchEvent(MotionEvent event) {
if (event.getAction() == MotionEvent.ACTION_DOWN){
floatStartX = event.getX();
floatStartY = event.getY();
}
if (event.getAction() == MotionEvent.ACTION_MOVE){
floatEndX = event.getX();
floatEndY = event.getY();
drawPaintSketchImage();
floatStartX = event.getX();
floatStartY = event.getY();
}
if (event.getAction() == MotionEvent.ACTION_UP){
floatEndX = event.getX();
floatEndY = event.getY();
drawPaintSketchImage();
}
return super.onTouchEvent(event);
}
public void buttonSaveImage(View view){
File fileSaveImage = new File(this.getExternalFilesDir(Environment.DIRECTORY_PICTURES),
Calendar.getInstance().getTime().toString() + ".jpg");
try {
FileOutputStream fileOutputStream = new FileOutputStream(fileSaveImage);
bitmap.compress(Bitmap.CompressFormat.JPEG, 100, fileOutputStream);
fileOutputStream.flush();
fileOutputStream.close();
Toast.makeText(this,
"File Saved Successfully",
Toast.LENGTH_LONG).show();
} catch (Exception e) {
e.printStackTrace();
}
}
}
-
Ещё видео!