13 APR 2020

drawing-apps bitmap-to-file

andy4

draw4

Canvas onDraw

When you create you own custom view you will probably give the view its own special appearance. You probably want to draw the elements of the view onto the screen somehow. This is achieved by overriding the onDraw method of the View class. The signature of this method is

protected void onDraw (Canvas canvas);

When you implement the method you are given a Canvas object on which you can draw you view. This method will be called when the system decides that your view should be rendered onto the screen.

Example:

public class MyView extends View {
   Paint paint = new Paint();
   protected void onDraw (Canvas canvas) {
      int w = getWidth();
      int h = getHeight();
      int radius = Math.min(w,h)/2 - 10;
      paint.setARGB(255, 255, 0, 0);
      canvas.drawCircle(w/2, h/2, radius, paint);
   }
}

The combination of a Paint object with the Canvas is a powerful tool for creating complex drawings.

When is onDraw called

The onDraw method is called whenever android thinks that your view should be redrawn. This can be tha case when your view is animated, in which case onDraw is called for every frame in the animation. It is also called when the layout changes and your view is re-positioned on the screen.

But what if some data inside your view has changed and you want to make sure that the view is redrawn. You can’t call onDraw directly. Instead you should call invalidate to tell the view that it needs to redraw itself.

Performance

The onDraw method is called from the UI thread. This means that you should make sure that the code inside the method performs as fast as possible. Anything that takes too much time should either be done during initialization or in a separate thread. Allocating objects with new is can be quite a performance drag, especially if done repeatedly. For this reason you should avoid allocating new objects within the onDraw method.

If your drawing becomes elaborate and requires some time consuming computation you should consider placing the code in a separate thread. This will take load off the UI thread and make your view more responsive. One class that is very useful in this context is the Picture class.

… full article

Drawing using xml

We are going to draw a circle on a View. The circle is defined in an XML file. The manifest file does not need to be modified.

oval.xml
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
    android:shape="oval">
    
   <solid 
       android:color="#666666"/>
   <size 
       android:width="70dp"
       android:height="70dp"/>
</shape> 

In the oval.xml file we create a circle shape. We define its colour and size. The oval.xml file is located in the res/drawable directory.

… full article

DialogFragment?

draw5

In a very simple sentence, a Dialog Fragment is a fragment that is used to make Dialogs that floats on some Activity.

DialogFragment is a utility class which extends the Fragment class. So, a DialogFragment displays or shows a Dialog but inside a Fragment i.e. all the information regarding the Dialog or the data associated with the Dialog will be stored or managed in the Fragment only. You can use the DialogFragment in API level 11 or higher.

Since DialogFragment is associated with Fragment, so it has it’s own Life Cycle and now the Activity need not manage the lifecycle of Dialogs. Due to this reason, DialogFragments are recommended to be used while implementing Alert Dialog or any other type of Dialogs in Android.

… full article

more topics covered:

  • getting album directory
  • read-write bmp files
  • PrintHelper.systemSupportsPrint
  • PrintHelper.printBitmap
  • calling FragmantDialog
  • Bitmap.Config.ARGB_8888

Links:

Leave a comment

Design a site like this with WordPress.com
Get started