Android development —— 9, drawing and multimedia

In Android, the most commonly used classes for drawing images are the Paint class (brush), Canvas class (canvas), Bitmap class (bitmap), BitmapFactory (tool class)

With a brush and a canvas, you can paint!

Brush
Creates a brush, specifying that the brush's color is red with a light gray shading:

Paint paint=new Paint();
paint.setColor(Color.RED);
paint.setShadowLayer(2,3,3,Color.rgb(180,180,180));

Commonly used properties


canvas

The Canvas class represents a canvas, and various graphics (rectangles, circles, lines, etc.) can be drawn through the methods provided by this class. Normally, to draw in Android, you need to create a view that inherits from the View class, override its onDraw(Canvas canvas) method in this class, and then add the view to the Activity that displays and spit.

Create the canvas:

public class DrawView extends View {
    /**
     * Function: Constructor
     */
    public DrawView(Context context, AttributeSet attrs) {
        super(context, attrs);
    }
    /*
     * Function: Override the onDraw() method
     */
    @Override
    protected void onDraw(Canvas canvas) {
        Paint paint=new Paint(); //Define a brush with default settings
        paint.setColor(Color.RED); //Set the color to red
        paint.setShadowLayer(2, 3, 3, Color.rgb(180, 180, 180)); //Set the shadow
        canvas.drawRect(40, 40, 200, 100, paint); //Draw a rectangle
        super.onDraw(canvas);
    }
}

Example:

Draw a circle:
Canvas.drawCricle(float cx,float cy,float radius,Paint paint);
draw a rectangle
Canvas.drawRect(float left,float top,float right,float bottom,Paint paint);
draw an ellipse
Canvas.drawOval(RectF oval,Paint paint);

 draw text

Canvas.drawText(String text,float x,float y,Paint paint);
text: the text to draw
x,y: horizontal and vertical coordinates
paint: brush

 Multimedia technology
uses MediaPlay to play audio

MediaPlay player=MediaPlayer.create(this,R.raw.d);
player.start();
player.pause ();
player.stop();

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325733287&siteId=291194637