Android圖片處理執行個體分析_Android

來源:互聯網
上載者:User

本文執行個體講述了Android圖片處理的方法。分享給大家供大家參考,具體如下:

package cn.szbw.util;import Android.content.Context;import android.graphics.Bitmap;import android.graphics.BitmapFactory;import android.graphics.Canvas;import android.graphics.Matrix;import android.graphics.Paint;public class Utils {     /***     * 載入本地圖片     * @param context:主運行函數執行個體     * @param bitAdress:圖片地址,一般指向R下的drawable目錄     * @return     */    public final Bitmap CreatImage(Context context, int bitAdress) {        Bitmap bitmaptemp = null;        bitmaptemp = BitmapFactory.decodeResource(context.getResources(),bitAdress);        return bitmaptemp;    }//2.圖片平均分割方法,將大圖平均分割為N行N列,方便使用者使用    /***     * 圖片分割     * @param g     * :畫布     * @param paint     *:畫筆     * @param imgBit     *:圖片     x     *:X軸起點座標     * @param y     *:Y軸起點座標     * @param w     * :單一圖片的寬度     * @param h     *:單一圖片的高度     * @param line     *:第幾列     * @param row     * :第幾行     */    public final void cuteImage(Canvas g, Paint paint, Bitmap imgBit, int x,            int y, int w, int h, int line, int row) {        g.clipRect(x, y, x + w, h + y);        g.drawBitmap(imgBit, x - line * w, y - row * h, paint);        g.restore();    }//3.圖片縮放,對當前圖片進行縮放處理    /***     * 圖片的縮放方法     * * @param bgimage     *:源圖片資源     * @param newWidth     *:縮放後寬度     * @param newHeight     *:縮放後高度     * @return     */    public Bitmap zoomImage(Bitmap bgimage, int newWidth, int newHeight) {        // 擷取這個圖片的寬和高        int width = bgimage.getWidth();        int height = bgimage.getHeight();        // 建立操作圖片用的matrix對象        Matrix matrix = new Matrix();        // 計算縮放率,新尺寸除原始大小        float scaleWidth = ((float) newWidth) / width;        float scaleHeight = ((float) newHeight) / height;        // 縮放圖片動作        matrix.postScale(scaleWidth, scaleHeight);        Bitmap bitmap = Bitmap.createBitmap(bgimage, 0, 0, width, height,                matrix, true);        return bitmap;    }//4.繪製帶有邊框的文字,一般在遊戲中起文字的美化作用    /**     * 繪製帶有邊框的文字     * @param strMsg     * :繪製內容     * @param g     *:畫布     * @param paint     *:畫筆     * @param setx     *:X軸起始座標     * @param sety     *:Y軸的起始座標     * @param fg     *:前景色彩     * @param bg     * :背景色     */    public void drawText(String strMsg, Canvas g, Paint paint, int setx,            int sety, int fg, int bg) {        paint.setColor(bg);        g.drawText(strMsg, setx + 1, sety, paint);        g.drawText(strMsg, setx, sety - 1, paint);        g.drawText(strMsg, setx, sety + 1, paint);        g.drawText(strMsg, setx - 1, sety, paint);        paint.setColor(fg);        g.drawText(strMsg, setx, sety, paint);        g.restore();    }}

更多關於Android相關內容感興趣的讀者可查看本站專題:《Android圖形與影像處理技巧總結》、《Android開發入門與進階教程》、《Android調試技巧與常見問題解決方案匯總》、《Android多媒體操作技巧匯總(音頻,視頻,錄音等)》、《Android基本組件用法總結》、《Android視圖View技巧總結》、《Android布局layout技巧總結》及《Android控制項用法總結》

希望本文所述對大家Android程式設計有所協助。

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.