編輯:關於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; } }
本文實例講述了Android開發之在程序中時時獲取logcat日志信息的方法。分享給大家供大家參考,具體如下:今天分享一個在軟件開發中很實用的例子,也是這幾天在通宵加班中
前面我們簡單的介紹過CoordinatorLayout這個控件,這一篇我們詳細地介紹這個ASD庫中最重要的控件。概述我們在NestedScrollView這個控件中有設置
大家可能在做app的時候,或多或少需要使用聯系人,而根據google提供的api,你需要編寫大量的代碼,例如首先需要查詢數據庫,涉及到數據庫表和字段以及對應的SQL語句。
這裡是使用 TOC 生成的目錄:•Layout文件定義◦ListView定義◦item 模板定義 •代碼a