L7 Graphics Introduction

11
Faizan Iftikhar Lecturer University of Central Punjab

Transcript of L7 Graphics Introduction

Page 1: L7 Graphics Introduction

8/3/2019 L7 Graphics Introduction

http://slidepdf.com/reader/full/l7-graphics-introduction 1/11

Faizan Iftikhar

LecturerUniversity of Central Punjab

Page 2: L7 Graphics Introduction

8/3/2019 L7 Graphics Introduction

http://slidepdf.com/reader/full/l7-graphics-introduction 2/11

Things to ponder ony Its important to consider what your

graphical demand is

y Varying graphical tasks are bestaccomplished by varying techniques

yGraphics and animations for a rather staticapplication should be implemented muchdifferently than graphics and animationsfor an interactive game

Page 3: L7 Graphics Introduction

8/3/2019 L7 Graphics Introduction

http://slidepdf.com/reader/full/l7-graphics-introduction 3/11

Graphics drawing optionsyCanvas and Drawables

y

Hardware AccelerationyOpenGL

Page 4: L7 Graphics Introduction

8/3/2019 L7 Graphics Introduction

http://slidepdf.com/reader/full/l7-graphics-introduction 4/11

Canvas and Drawables

ySet of ViewsyExtend the views to change their

look and behaviouryCustom 2D Rendering

yUsing various drawing methods

yCreate Drawable objects for texturedbuttons or frame by frame animation

Page 5: L7 Graphics Introduction

8/3/2019 L7 Graphics Introduction

http://slidepdf.com/reader/full/l7-graphics-introduction 5/11

Draw on a Viewy Used for apps requiring less frame rate or processing

y Create a custom view

y class CustomView extends Viewy Constructor with Context as parameter

y onDraw(Canvas canvas)

y onDraw() is called on need basis

y To draw your view, you need to invalidate your viewusing invalidate() method

y Leads to your onDraw() being called

Page 6: L7 Graphics Introduction

8/3/2019 L7 Graphics Introduction

http://slidepdf.com/reader/full/l7-graphics-introduction 6/11

Draw on a View: Sampleclass Panel extends View {

public Panel(Context context) {

super(context);mBitmap = BitmapFactory.decodeResource(getResources(),

R.drawable.icon);

}

@Overridepublic void onDraw(Canvas canvas) {

canvas.drawColor(Color.BLACK);canvas.drawBitmap(mBitmap, 10, 10, null);

}

Page 7: L7 Graphics Introduction

8/3/2019 L7 Graphics Introduction

http://slidepdf.com/reader/full/l7-graphics-introduction 7/11

Draw on a View: Sampleimport android.app.Activity ;

import android.os.Bundle;

public class Tutorial2D extends Activity {

@Override

public void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);setContentView(R.layout.main);

}

}

Page 8: L7 Graphics Introduction

8/3/2019 L7 Graphics Introduction

http://slidepdf.com/reader/full/l7-graphics-introduction 8/11

Draw with Canvasy Canvas is an interface to the actual surface on which

graphics are to be drawn

y

Primarily responsible for drawing on surfacey Drawing is done on an underlying Bitmap

y Bitmap b = Bitmap.createBitmap(100, 100,Bitmap.Config. ARGB_8888);Canvas c = new Canvas(b);

y To create a canvas, creating bitmap is a must!

y onDraw(Canvas canvas)

Page 9: L7 Graphics Introduction

8/3/2019 L7 Graphics Introduction

http://slidepdf.com/reader/full/l7-graphics-introduction 9/11

Draw with Canvasy Other drawing methods

y drawBitmap()

y

Draws an Image onto a Bitmap that is underlying a Canvasy drawRect()

y Draw the specified Rect using the specified paint

y drawText()

y

Draw the text, with origin at (x,y), using the specified painty Incase of Drawable

y draw(Canvas c) is used

Page 10: L7 Graphics Introduction

8/3/2019 L7 Graphics Introduction

http://slidepdf.com/reader/full/l7-graphics-introduction 10/11

CodeLinearLayout mLinearLayout;

protected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);

// Create a LinearLayout in which to add the ImageViewmLinearLayout = new LinearLayout(this);

// Instantiate an ImageView and define its propertiesImageView i = new ImageView(this);i.setImageResource(R .drawable.my_image);i.setAdjustViewBounds(true); // set the ImageView bounds to match the

Drawable's dimensionsi.setLayoutParams(new

Gallery .LayoutParams(LayoutParams. WRAP_CONTENT,LayoutParams. WRAP_CONTENT));

// Add the ImageView to the layout and set the layout as the content viewmLinearLayout.addView(i);setContentView(mLinearLayout);

}

Page 11: L7 Graphics Introduction

8/3/2019 L7 Graphics Introduction

http://slidepdf.com/reader/full/l7-graphics-introduction 11/11

Drawablesy custom 2D graphics library for drawing shapes and

images

y android.graphics.drawable packagey common classes used for drawing

y Drawable class

y

Three ways of instantiated a Drawable:y Using an Image

y Using an xml

y Using a constructor