Android 自定义进度图形

来源:互联网 发布:un服务贸易数据库 编辑:程序博客网 时间:2024/06/06 01:07

只要给两张大小相同的图片(一张地图--无,一张进度的)就可以做一个想要的进度




废话就不多说了,直接上代码


 @Override    protected void onDraw(Canvas canvas) {        /*         * drawBitmap(Bitmap bitmap, Rect src, RectF dst, Paint paint);         * Rect src: 是对图片进行裁截,若是空null则显示整个图片         * RectF dst:是图片在Canvas画布中显示的区域,         * 大于src则把src的裁截区放大,         * 小于src则把src的裁截区缩小。         * 当想要让图片以画卷方式展现的话,主要是设置src大小,这边是默认从左到右显示,所以每次只要修改src中right的大小就好         */         Bitmap bitmap;         Bitmap bitmapEmpty;        if ( getWidth() == 0 || getHeight() == 0) {            return;        } else {            float scale=0.8f;            bitmap = BitmapFactory.decodeResource(context.getResources(), R.drawable.home_tree_full);            scale=1.0f*getWidth()/bitmap.getWidth();            bitmap=BitmapUtils.bitMapScale(bitmap,scale);            bitmapEmpty = BitmapFactory.decodeResource(context.getResources(), R.drawable.home_tree_empty);            bitmapEmpty=BitmapUtils.bitMapScale(bitmapEmpty,scale);        }        Rect src = new Rect();        src.left = 0;        src.top = (int) (bitmap.getHeight() * progress);        src.right = bitmap.getWidth();        src.bottom = bitmap.getHeight();        Paint mPaint = new Paint();        mPaint.setAntiAlias(true);// 设置画笔的锯齿效果。 true是去除,        canvas.drawBitmap(bitmapEmpty, null, new RectF(0, 0, src.right, src.bottom), mPaint);        canvas.drawBitmap(bitmap, src, new RectF(0, src.top, src.right, src.bottom), mPaint);        mPaint.setColor(context.getResources().getColor(R.color.color_pumice));// 设置字体颜色        mPaint.setTextSize(textSize);        canvas.drawText(maxMileage, src.right * 0.33f, textSize, mPaint);        canvas.drawText(otherMileage, src.right * 0.75f, src.bottom / 2 + textSize * 1.5f, mPaint);    }


0 0
原创粉丝点击