Android 的繪圖相關物件

android.graphics.Camera

android.graphics.drawable.Drawable

 

Paint

  setStyle

  setColor

  setStrokeWidth(strokeWidth);

  setAntiAlias(true);

  setARGB(255, 255, 255, 255);

Path

PathEffect

 

Matrix

.setPolyToPoly(src, 0, dst, 0, src.length >> 1);  

Paint.FontMetrics

mFontMetrics = mPaint.getFontMetrics();


整合練習 - 繪圖版範例


2D 動畫功能

Bitmap

createBitmap(int[] colors, int width, int height, boolean hasAlpha)

int         getPixel(int x, int y)

int         height()

void  setPixels(int[] pixels, int offset, int stride, int x, int y, int width, int height)

int         width()

Canvas :

  setDevice()

  boolean     clipPath(Path path, ClipMode mode)

boolean      clipRect(float left, float top, float right, float bottom)

       Intersect the current clip with the specified rectangle, which is expressed in local coordinates.

void          drawBitmap(Bitmap bitmap, Rect src, Rect dst, Paint paint)

void    drawBitmap(Bitmap bitmap, float left, float top, Paint paint)

void    drawBitmap(Bitmap bitmap, Rect src, RectF dst, Paint paint)

void    drawCircle(float cx, float cy, float radius, Paint paint)

void    drawColor(int color)

void    drawColor(int color, Mode mode)

void    drawLine(float startX, float startY, float stopX, float stopY, Paint paint)

void    drawOval(RectF oval, Paint paint)

void    drawPaint(Paint paint)

void    drawPath(Path path, Paint paint)

void    drawPoint(float x, float y, Paint paint)

void    drawPoints(float[] pts, int offset, int length, Paint paint)

void    drawPoints(float[] pts, Paint paint)

void    drawPosText(char[] text, int index, int count, float[] pos, Paint paint)

void    drawPosText(String text, float[] pos, Paint paint)

void    drawRGB(int r, int g, int b)

void    drawRect(RectF rect, Paint paint)

void    drawRect(float left, float top, float right, float bottom, Paint paint)

void    drawRect(Rect r, Paint paint)

void    drawRoundRect(RectF rect, float rx, float ry, Paint paint)

void    drawText(String text, int start, int end, float x, float y, Paint paint)

void    drawText(char[] text, int index, int count, float x, float y, Paint paint)

void    drawText(String text, float x, float y, Paint paint)

void    drawText(CharSequence text, int start, int end, float x, float y, Paint paint)

void    drawTextOnPath(String text, Path path, float hOffset, float vOffset, Paint paint)

void    drawTextOnPath(char[] text, int index, int count, Path path, float hOffset, float vOffset, Paint paint)

變形與轉換

Android 中二維的動畫功能主要在 android.view.animation 函式庫中,包含兩種動畫功能,第一種是二維轉換 (tweened animation) ,第二種是逐畫面的動畫(frame by frame animation)。

二維轉換總共有四種基本轉換

1.    AlphaAnimation 透明度轉換 (transparency changes)

2.    RotateAnimation旋轉 (rotations)

3.    ScaleAnimation 縮放 (growing or shrinking)

4.    TranslateAnimation 位移 (position changes)

 

描述二維轉換的方式又可分為 1. 使用 XML 2. 使用 Java 程式 兩種方法。

 

(1)  使用 XML 描述二維轉換

<set android:shareInterpolator="true"

     android:interpolator="@android:anim/ease_in_interpolator">

 

    <translate android:fromXDelta="0"

               android:toXDelta="30"

               android:duration="800"

               android:fillAfter="true"/>

   

    <set android:duration="800"

         android:pivotX="50%"

         android:pivotY="50%" >

 

        <rotate android:fromDegrees="0"

                android:toDegrees="-90"

                android:fillAfter="true"

                android:startOffset="800"/>

   

        <scale android:fromXScale="1.0"

                android:toXScale="2.0"

                android:fromYScale="1.0"

                android:toYScale="2.0"

                android:startOffset="800" />

    </set>

 

    <translate android:toYDelta="-100"

               android:fillAfter="true"

               android:duration="800"

               android:startOffset="1600"/>

</set>

 

(2)  使用 Java 實作二維轉換


// Create root AnimationSet.

AnimationSet rootSet = new AnimationSet(true);

rootSet.setInterpolator(new EaseInInterpolator());

rootSet.setRepeatMode(Animation.NO_REPEAT);

 

// Create and add first child, a motion animation.

TranslateAnimation trans1 = new TranslateAnimation(0, 30, 0, 0);

trans1.setStartOffset(0);

trans1.setDuration(800);

trans1.setFillAfter(true);

rootSet.addAnimation(trans1);

 

// Create a rotate and a size animation.

RotateAnimation rotate = new RotateAnimation(

       0,

       -90,

       RotateAnimation.RELATIVE_TO_SELF, 0.5f,

       RotateAnimation.RELATIVE_TO_SELF, 0.5f);

       rotate.setFillAfter(true);

       rotate.setDuration(800);

 

ScaleAnimation scale = new ScaleAnimation(

       1, 2, 1, 2, // From x, to x, from y, to y

       ScaleAnimation.RELATIVE_TO_SELF, 0.5f,

       ScaleAnimation.RELATIVE_TO_SELF, 0.5f);

       scale.setDuration(800);

       scale.setFillAfter(true);

 

// Add rotate and size animations to a new set,

// then add the set to the root set.

AnimationSet childSet = new AnimationSet(true);

childSet.setStartOffset(800);

childSet.addAnimation(rotate);

childSet.addAnimation(scale);

rootSet.addAnimation(childSet);

 

// Add a final motion animation to the root set.

TranslateAnimation trans2 = new TranslateAnimation(0, 0, 0, -100);

trans2.setFillAfter(true);

trans2.setDuration(800);

trans2.setStartOffset(1600);

rootSet.addAnimation(trans2);

 

// Start the animation.

animWindow.startAnimation(rootSet);

 

載入與啟動二維轉換

// Hook into the object to be animated.

TextView animWindow = (TextView)findViewById(R.id.anim);

 

// Load the animation from XML (XML file is res/anim/move_animation.xml).

Animation anim = AnimationUtils.loadAnimation(AnimationSample.this, R.anim.move_animation);

anim.setRepeatMode(Animation.NO_REPEAT);

 

// Play the animation.

animWindow.startAnimation(anim);

3D 動畫 OpenGL

Android 使用 OpenGL這個 3D 動畫遊戲引擎的嵌入式版本,稱為 OpenGL|ES,這和 J2ME 中的 JSR239 OpenGL ES API 大至上是相同的,但並不完全一樣。


OpenGL 的範例


在 Android 中使用 OpenGL 時,首先要先繼承 View 這個物件,然後在程式中取得 OpenGLContext 這個物件,接著在 onDraw() 這個函數中執行對應的繪圖的功能,以下是一個範例。

 

class GLView extends View

{

    public GLView(Context context)

    {

        mGLContext = new OpenGLContext(0);

    }

    @Override

    protected void onDraw(Canvas canvas) {

        GL10 gl = (GL10)(mGLContext.getGL());

        mGLContext.waitNative(canvas, this);

            gl.glViewport(0, 0, w, h);

            float ratio = (float)w / h;

            gl.glMatrixMode(gl.GL_PROJECTION);

            gl.glLoadIdentity();

            gl.glFrustumf(-ratio, ratio, -1, 1, 2, 12);

            gl.glDisable(gl.GL_DITHER);

            gl.glClearColor(1,1,1,1);

            gl.glEnable(gl.GL_SCISSOR_TEST);

            gl.glScissor(0, 0, w, h);

            gl.glClear(gl.GL_COLOR_BUFFER_BIT);

            gl.glMatrixMode(gl.GL_MODELVIEW);

            gl.glLoadIdentity();

            gl.glTranslatef(0, 0, -3.0f);

            gl.glScalef(0.5f, 0.5f, 0.5f);

            gl.glRotatef(mAngle, 0, 1, 0);

            gl.glRotatef(mAngle*0.25f,  1, 0, 0);

            gl.glColor4f(0.7f, 0.7f, 0.7f, 1.0f);

            gl.glEnableClientState(gl.GL_VERTEX_ARRAY);

            gl.glEnableClientState(gl.GL_COLOR_ARRAY);

            gl.glEnable(gl.GL_CULL_FACE);

            mCube.draw(gl);

            mAngle += 1.2f;

     mGLContext.waitGL();

          }

    }

 Android 的 android.media.MediaPlayer 函式庫提供了影片與聲音的播放功能,另外也在android.media.MediaRecorder中提供了錄音與錄影的功能,但這些功能在模擬器當中將無法運作,只有真實的手機才會具備此功能。

原文網站: Henry Pu