Dispozitive Mobile de Calcul: Unit 13 - 2D Graphics
Dispozitive Mobile de Calcul: Unit 13 - 2D Graphics
Unit 13 – 2D Graphics
Agenda
Two dimensional
Graphics Capturing
Using images
• Images images
from the device
• Colors, tools, canvas using photo
gallery
area camera
• Geometrical shapes
Multimedia resources
• Images
Files • Audio files
• Video files
• 2D or 3D graphics
Memory • Audio sequences
• Animation
2D GRAPHICS
Drawing Task
To draw something, you need 4 basic components:
• a Bitmap to hold the pixels;
• a Canvas to host the draw calls (writing into the bitmap);
• a drawing primitive (e.g. Rect, Path, text, Bitmap);
• a paint (to describe the colors and styles for the drawing).
2D Graphics
• Drawing images in memory
– Bitmap class
• Colors and textures
• The drawing tool
– Paint class
• Drawing surfaces
– Canvas class
• Geometrical figures
– ShapeDrawable
Displaying images inside controls
• ImageView
• Drawable (abstract class)
– res/drawable, res/raw
– Images
• Image files
– Shapes
• XML files
• Canvas
– android.graphics
– Free drawings
Images
• Bitmap
• Editable or non editable
• Static methods
– createBitmap(width, height, config)
• Bitmap.Config
– createBitmap(srcBitmap) and other overloads
– createScaledBitmap(src, wDest, hDest, filter)
• Methods
– compress(format, quality, output_stream)
– getWidth(), getHeight()
• BitmapFactory
– Creating Bitmap objects from different sources
– Metode statice de tipul decodeSURSA()
• Resource, Stream, File etc.
Images
int width, height;
Bitmap bmp1 = Bitmap.createBitmap(width, height,
Bitmap.Config.ARGB_8888);
Paint class
Setting text properties:
Setting • void setTextAlign(Paint.Align align)
• void setTextSize(float textSize)
Paint class
• Other properties:
– void setStyle(Paint.Style style)
– void setStrokeWidth(float width)
– PathEffect setPathEffect(PathEffect effect)
– void setAntialias(boolean aa)
• Initializing gradients
– Shader setShader(Shader shader)
Paint class
Paint creionGrad = new Paint(), creionNegru = new Paint(), creionText = new Paint();
creionNegru.setColor(Color.BLACK);
creionNegru.setStyle(Paint.Style.STROKE);
creionNegru.setAntiAlias(true);
creionText.setTextAlign(Paint.Align.CENTER);
creionText.setColor(Color.GREEN);
creionText.setTextSize(24);
creionGrad.setShader(gradientBmps);
creionGrad.setStrokeWidth(30);
Drawing surface – Canvas class
• Methods for drawing shape, text and images
• draw…()
• Needs a drawing instrument
• Position
• Additional effects
Drawing inside controls
• The class must extent the View class
• The main method used is onDraw()
• The method is called whenever the objects needs to be drawn
• Other important methods
– invalidate() – from UI thread
– postInvalidate() – from a non-UI thread
Canvas
public class GraphView extends View {
…
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);
//drawing objects
}
}
Drawing bitamps
• Constructor
– Canvas(Bitmap)
• Steps
– Creating or getting a bitmap object
• Bitmap bmp = Bitmap.createBitmap(l, L, tip);
– Initializing the canvas object
• Canvas canvas = new Canvas(bmp);
Drawing geometric figures
• void drawPoint(float x, float y, Paint paint)
• void drawPoints(float[] pts, int offset, int count, Paint paint)
• void drawLine(float startX, float startY, float stopX, float stopY,
Paint paint)
• void drawLines(float[] pts, Paint paint)
Drawing geometric figures
• void drawRect(RectF rect, Paint paint)
• void drawRoundRect(RectF rect, float rx, float ry, Paint paint)
• void drawCircle(float cx, float cy, float radius, Paint paint)
• void drawOval(RectF oval, Paint paint)
• void drawArc(RectF oval, float startAngle, float sweepAngle,
boolean useCenter, Paint paint)
Displaying images
• void drawBitmap(Bitmap bmp, float left, float top, Paint paint)
– Bitmap bmp = BitmapFactory.decodeResource(getResources(),
R.drawable.id);
• void drawPicture(Picture picture, RectF dst)
Changing the background
• void drawColor(int color)
• void drawRGB(int r, int g, int b)
• void drawARGB(int a, int r, int g, int b)
• void drawPaint(Paint paint)
Drawing text
• void drawText(String text, float x, float y, Paint paint)
Drawing shapes
• Support for vector graphics
• Drawing geometrical shapes:
– addCircle()
– addRect()
– addArc()
– etc.
• Combining shapes
– addPath()
Example: Canvas
//background color
canvas.drawColor(Color.YELLOW);
//line
canvas.drawLine(10, 10, 300, 4000, pencil);
//circle
canvas.drawCircle(100, 100, 50, pencil);
// rectangle
canvas.drawRect(rect, pencil); //---------rect of type Rect or RectF
//elipse
canvas.drawOval(rect, pencil);
// Add a specified arc to the rect as a new contour
path.addArc(rect, 270, 90);
// draw a path
canvas.drawPath(path, pencil);
// draw a text over a path
canvas.drawTextOnPath(“Arc contour", path, 0, 40, pencil);
Drawing a text over a path
• void drawPath(Path path, Paint paint)
• void drawTextOnPath(String text, Path path, float hOffset,
float vOffset, Paint paint)
Cutting
• Clipping area - clip
– Sets the area which will be modified by using the Canvas draw
method
– Initially the clip is set onto the entire window
• Modifying the current clip:
– clipRect()
– clipPath()
– clipRegion()
Transformations
• Scaling
– scale(sx, sy)
• Translates (changing coordinates)
– translate(dx, dy)
• Rotations
– rotate(grade)
• Angle shifting
– skew(x, y)
GEOMETRICAL FIGURES
GEOMETRICAL FIGURES
• ShapeDrawable
• Shape objects
– RectShape
– OvalShape
– ArcShape
– PathShape
• Methods
– getPaint()
– draw(Canvas)
– setBounds(x1, y1, x2, y2)
GEOMETRICAL FIGURES
ShapeDrawable drawable;
Canvas canvas;
int x, y, width, height;
• XML reference :
– @drawable/fileNameForShapes
• Java reference:
• R.drawable. fileNameForShapes
• Resources#getDrawable
PROCESSING
CAMERA IMAGES
Camera
– startActivityForResult(takePictureIntent, actionCode);
• android.permission.CAMERA
Camera: installed apps
• Access it by using an intent object
• Capturing images
– Action MediaStore.ACTION_IMAGE_CAPTURE
• Video capture
– Action MediaStore.ACTION_VIDEO_CAPTURE
Camera: installed apps
• Launching the application
– void startActivityForResult(Intent intent, int actionCode)
• Getting the result
– protected void onActivityResult (int actionCode, int resultCode,
Intent intent)
Camera: installed apps
private Bitmap photo = null;
...
@Override
protected void onActivityResult(int actionCode,
int resultCode, Intent data) {
//intent
Intent intent = new Intent(Intent.ACTION_PICK,
MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
//starting the activity
startActivityForResult(intent, SELECT_IMAGE);
PROCESSING GALLERY IMAGES
protected void onActivityResult(int actionCode, int resultCode, Intent intent) {
} catch (Exception e) {
e.printStackTrace();
}
}
}
Resources
• S. Komatineni, D. MacLean – Pro Android 4, Apress, 2012
• http://developer.android.com