l7 graphics introduction

Post on 06-Apr-2018

225 Views

Category:

Documents

0 Downloads

Preview:

Click to see full reader

TRANSCRIPT

8/3/2019 L7 Graphics Introduction

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

Faizan Iftikhar

LecturerUniversity of Central Punjab

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

8/3/2019 L7 Graphics Introduction

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

Graphics drawing optionsyCanvas and Drawables

y

Hardware AccelerationyOpenGL

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

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

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);

}

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);

}

}

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)

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

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);

}

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

top related