Android教程網
  1. 首頁
  2. Android 技術
  3. Android 手機
  4. Android 系統教程
  5. Android 游戲
 Android教程網 >> Android技術 >> 關於Android編程 >> Android中特殊圖形的生成例子

Android中特殊圖形的生成例子

編輯:關於Android編程

import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.LinearGradient;
import android.graphics.Paint;
import android.graphics.Path;
import android.graphics.PorterDuff;
import android.graphics.PorterDuffXfermode;
import android.graphics.Rect;
import android.graphics.RectF;
import android.graphics.Shader;
import android.graphics.Bitmap.Config;
import android.graphics.Paint.Align;
import android.graphics.PorterDuff.Mode;
import android.graphics.drawable.Drawable;

public class DrawUtil
{
	/**
	 * 畫出一個這樣一個圖形:中間帶標題文字的半圓,半圓可以設置陰影和頂部的矩形投影
	 * 
	 * @param text
	 *            標題文字 (此處設置為兩個字的居中效果)
	 * @param radio
	 *            半圓的半徑
	 * @param shadowWidth
	 *            陰影的寬度
	 * @param isWithRectf
	 *            是否有矩形投影
	 * @param rectfColor
	 * 			      矩形投影的顏色
	 * @param paint
	 *            畫筆對象
	 * @return
	 */
	public static Bitmap drawHalfCircleWithText(String text, int radio,
			int shadowWidth, boolean isWithRectf, int rectfColor, Paint paint)
	{
		Bitmap bitmap = Bitmap.createBitmap(radio * 2,
			 radio * 2 + shadowWidth, Config.ARGB_8888);
		Canvas canvas = new Canvas(bitmap);
		// 畫一個帶圓心的扇形,此處為半圓
		canvas.drawArc(new RectF(0, 0, radio * 2, radio * 2), 360, 180, true,
				paint);

		// 是否需要在半圓頂部畫出一個矩形投影
		
		if (isWithRectf)
		{
			paint = reset(paint);
			paint.setColor(rectfColor);
			Bitmap bmp = drawRectf(radio * 2, radio/8, paint);
			
			paint = reset(paint);
			paint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.SRC_ATOP));//去掉矩形投影和半圓不相交的部分
			canvas.drawBitmap(bmp, 0, radio, paint);
			
		}else
		{
			LinearGradient lg=new LinearGradient(0,0,0,radio/8,0x44000000,0x00000000,Shader.TileMode.MIRROR);
			paint = reset(paint);
			paint.setShader(lg);
			Bitmap bmp = drawRectf(radio * 2, radio/8, paint);
			
			paint = reset(paint);
			paint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.SRC_ATOP));//去掉矩形投影和半圓不相交的部分
			canvas.drawBitmap(bmp, 0, radio, paint);
		}
		
		
		//在半圓上面畫上文字
		paint = reset(paint);
		paint.setColor(Color.WHITE);
		paint.setTextSize(radio * 5 / 12);
		paint.setTextAlign(Align.CENTER);
		paint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.SRC_OVER));
		canvas.drawText(text, radio, radio * 5 / 3, paint);
		bitmap = Bitmap.createBitmap(bitmap, 0, radio, radio * 2, radio
				+ shadowWidth);
		return bitmap;
	}
	
	/**
	 * 根據寬度畫出一條指定三角形數量的鋸齒
	 * 
	 * @param screenWidth
	 *            寬度
	 * @param triangleHeight
	 *            鋸齒的高度
	 * @param triangleNum
	 *            三角形的數量
	 * @param isDown
	 * 			  三角形頂點的朝向,true表示向下,false表示向上           
	 * @param paint
	 *            畫筆對象
	 * @return 返回bitmap對象
	 */
	public static Bitmap drawtriangle(float screenWidth, float triangleHeight,
			int triangleNum, boolean isDown,Paint paint)
	{
		float triangleWidth = screenWidth / triangleNum;
		Bitmap bitmap = Bitmap.createBitmap((int) screenWidth,
				(int) triangleHeight, Config.ARGB_8888);
		Canvas canvas = new Canvas(bitmap);
		Path path = new Path();
		if(isDown)
		{
			path.moveTo(0, 0);
			for (int i = 0; i < triangleNum * 2; i++)
			{
				if (i % 2 == 0)
				{
					path.lineTo((triangleWidth / 2) * (i + 1), triangleHeight);
				} else if (i % 2 == 1)
				{
					path.lineTo((triangleWidth / 2) * (i + 1), 0);
				}
			}
		}else
		{
			path.moveTo(0, triangleHeight);
			for(int i = 0; i < triangleNum * 2; i++)
			{
				if(i % 2 == 0)
				{
					path.lineTo((triangleWidth / 2) * (i + 1), 0);
				} else if(i % 2 == 1)
				{
					path.lineTo((triangleWidth / 2) * (i + 1), triangleHeight);
				}
			}
		}
		path.close();
		canvas.drawPath(path, paint);
		return bitmap;
	}
	
	/**
	 * 畫出一條帶有指定半圓數量的花邊
	 * @param width 花邊的長度
	 * @param num 半圓的數量
	 * @param paint 畫筆對象
	 * @return
	 */
	public static Bitmap drawMultHalfCircle(int width, int num,Paint paint)
	{
		int radio = (width/num)/2;
		Bitmap bitmap = Bitmap.createBitmap(width,
			 radio, Config.ARGB_8888);
		Canvas canvas = new Canvas(bitmap);
		for(int i = 0; i < num; i++)
		{
			canvas.drawBitmap(drawHalfCircle(radio, paint), radio*2*i, 0, paint);
		}
		return bitmap;
	}
	
	/**
	 * 畫一個矩形
	 * @param width
	 * @param height
	 * @param color
	 * @param paint
	 * @return
	 */
	public static Bitmap drawRectf(float width, float height, Paint paint)
	{
		Bitmap bitmap = Bitmap.createBitmap((int)width, (int)height, Config.ARGB_8888);
		Canvas canvas = new Canvas(bitmap);
		canvas.drawRect(0, 0, width, height, paint);
		return bitmap;
	}
	
	/**
	 * 畫出一個半圓
	 * 
	 * @param radio
	 *            半徑
	 * @param paint
	 * @return
	 */
	public static Bitmap drawHalfCircle(int radio, Paint paint)
	{
		Bitmap bitmap = Bitmap.createBitmap((int) (radio * 2),
				(int) (radio * 2), Config.ARGB_8888);
		Canvas canvas = new Canvas(bitmap);
		canvas.drawArc(new RectF(0, 0, radio * 2, radio * 2), 360, 180, true,
				paint);
		bitmap = Bitmap.createBitmap(bitmap, 0, radio, radio * 2, radio);
		return bitmap;
	}
	
	/**
	 * 根據圖片的短邊裁剪成圓形並在周圍畫上一定寬度的白邊
	 * @param bitmap
	 * @param ringWidth
	 * @param radio
	 * @param paint
	 * @return
	 */
	public static Bitmap drawBitmapWithRing(Bitmap bitmap,int ringWidth,Paint paint)
	{
		Bitmap roundBitmap = toRoundBitmap(bitmap);
		int radio = roundBitmap.getWidth() / 2;
		Bitmap _bitmap = Bitmap.createBitmap((radio+ringWidth) * 2,
				(radio+ringWidth) * 2, Config.ARGB_8888);
		Canvas canvas = new Canvas(_bitmap);
		canvas.drawCircle((radio+ringWidth), (radio+ringWidth), (radio+ringWidth), paint);
		
		paint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.SRC_OVER));
		canvas.drawBitmap(roundBitmap, ringWidth, ringWidth, paint);
		return _bitmap;
	}
	
	   /**
     * 將圖像裁剪成圓形
     * 
     * @param bitmap
     * @return
     */
    public static Bitmap toRoundBitmap(Bitmap bitmap)
    {
    	if(bitmap == null)
    	{
    		return null;
    	}
    	
        int width = bitmap.getWidth();
        int height = bitmap.getHeight();
        float roundPx;
        float left, top, right, bottom, dst_left, dst_top, dst_right, dst_bottom;
        if (width <= height)
        {
            roundPx = width / 2;
            top = 0;
            bottom = width;
            left = 0;
            right = width;
            height = width;
            dst_left = 0;
            dst_top = 0;
            dst_right = width;
            dst_bottom = width;
        } else
        {
            roundPx = height / 2;
            float clip = (width - height) / 2;
            left = clip;
            right = width - clip;
            top = 0;
            bottom = height;
            width = height;
            dst_left = 0;
            dst_top = 0;
            dst_right = height;
            dst_bottom = height;
        }

        Bitmap output = Bitmap.createBitmap(width, height, Config.ARGB_8888);
        Canvas canvas = new Canvas(output);

        final int color = 0xff424242;
        final Paint paint = new Paint();
        final Rect src = new Rect((int) left, (int) top, (int) right,
                (int) bottom);
        final Rect dst = new Rect((int) dst_left, (int) dst_top,
                (int) dst_right, (int) dst_bottom);
        final RectF rectF = new RectF(dst);

        paint.setAntiAlias(true);

        canvas.drawARGB(0, 0, 0, 0);
        paint.setColor(color);
        canvas.drawRoundRect(rectF, roundPx, roundPx, paint);

        paint.setXfermode(new PorterDuffXfermode(Mode.SRC_IN));
        canvas.drawBitmap(bitmap, src, dst, paint);
        return output;
    }
    
    public static Bitmap darwShadow(Bitmap bitmap,int color,int padding)
    {
        int width = bitmap.getWidth() + padding;
        int height = bitmap.getHeight() + padding;
        Bitmap output = Bitmap.createBitmap(width, height, Config.ARGB_8888);
        Canvas canvas = new Canvas(output);
        Paint paint = new Paint();
        paint = reset(paint);
        paint.setColor(color);
        canvas.drawBitmap(bitmap, padding/2, padding/2, null);
        canvas.drawRoundRect(new RectF(0, 0, width, height),padding/2,padding/2,paint);
        return output;
    }
    
	private static Paint reset(Paint paint)
	{
		paint.reset();
		paint.setAntiAlias(true);
		paint.setStyle(Paint.Style.FILL);
		return paint;
	}
}

  1. 上一頁:
  2. 下一頁:
熱門文章
閱讀排行版
Copyright © Android教程網 All Rights Reserved