Saturday, March 2, 2019

Android displaying image using view

Animation on Android Studio

Android Studio provides many ways to create animation in the android. One of the ways is by extending the class using View.

Let's Code 


  1. Create a new project name it as demoo.
  2. Go to the project and make a new java class name it as Bitmapimage.





        Extend the Bitmapimage class with View and implements the method Context.

                    public class Bitmapimage extends View {
                    public Bitmapimage(Context context) {
                    super( context );
                           }
                      }


          Override this method with onDraw Canvas method

                   @Override
                   protected void onDraw(Canvas canvas) {
                   super.onDraw( canvas );
                    }

      3. Go to the resource folder and open drawable. Paste an image in it and name it as images.

                    import android.graphics.Bitmap;
                    import android.graphics.BitmapFactory;
                    private Bitmap bitmap;
 
                    public Bitmapimage(Context context) {
                    super( context );
  //store image in bitmap
                   bitmap=BitmapFactory.decodeResource( getResources(),R.drawable.images );
                   }
                   go to the canvas and draw the image

                    @Override
                     protected void onDraw(Canvas canvas) {
                     super.onDraw( canvas );
     
                      canvas.drawBitmap( bitmap,100,100,null );
                     }
                 }

      4. Now go to the main activity and setContentView Bitmapimage
     

            Bitmapimage bitmapimage=new Bitmapimage( this );
            setContentView( bitmapimage );


 See the whole code 

         
Things written on the MainActivity -:


  1.                 import android.support.v7.app.AppCompatActivity;
  2.                 import android.os.Bundle;
  3.                 public class MainActivity extends AppCompatActivity {
  4.                 Bitmapimage bitmapimage;
  5.  
  6.                 @Override
  7.     
  8.                 protected void onCreate(Bundle savedInstanceState) {
  9.                 super.onCreate( savedInstanceState );
  10.                 bitmapimage=new Bitmapimage( this );
  11.                 setContentView( bitmapimage );
  12.                        }
  13.                 }



Things wrote of BitmapImage class


  1.                 import android.content.Context;
  2.                 import android.graphics.Bitmap;
  3.                 import android.graphics.BitmapFactory;
  4.                 import android.graphics.Canvas;
  5.                 import android.view.View;


  6.                 public class Bitmapimage extends View {
  7.            
  8.                Bitmap bitmap;
  9.                public Bitmapimage(Context context) {
  10.                super( context );
  11.                bitmap=BitmapFactory.decodeResource( getResources(),R.drawable.images );
  12.                     }
  13.   
  14.                 @Override
  15.               protected void onDraw(Canvas canvas) {
  16.                super.onDraw( canvas );
  17.               canvas.drawBitmap( bitmap,100,100,null );
  18.                      }
  19.              }



 Copy this whole code in your android project and paste the image in drawable.
Now run your android project it will show the image which you have pasted in the drawable. To change the position of image change the x, y  coordinates in the canvas.drawBitmap(bitmap,x,y, null). Put the values of x and y by your own.








See on the video how the project looks like. Now run your project.